From 707592195a59db471ff3191d01058275bfc5c926 Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar Date: Thu, 12 Aug 2021 00:51:59 +0400 Subject: [PATCH] added functionality to remove group --- client/GroupTrackContainer.go | 53 ++++++++++++++++++- client/GroupTrackContainer_test.go | 16 ++++++ .../trackcontainers/grouptrackcontainers.json | 4 ++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/client/GroupTrackContainer.go b/client/GroupTrackContainer.go index 05c35f7..7437464 100644 --- a/client/GroupTrackContainer.go +++ b/client/GroupTrackContainer.go @@ -2,6 +2,7 @@ package client import ( "encoding/json" + "errors" "git.sr.ht/~akilan1999/p2p-rendering-computation/config" "github.com/google/uuid" "io/ioutil" @@ -17,9 +18,12 @@ type Groups struct { type Group struct { ID string `json:"ID"` TrackContainerList []*TrackContainer `json:"TrackContainer"` + // Sneaky as required only when removing the element + // Set when GetGroup function is called + index int } -// CreateGroup Creates a new group to add a set of trackcontainers +// CreateGroup Creates a new group to add a set of track containers func CreateGroup() (*Group, error){ // Creating variable of type new group var NewGroup Group @@ -39,6 +43,53 @@ func CreateGroup() (*Group, error){ return &NewGroup,nil } +// RemoveGroup Removes group based on the group ID provided +func RemoveGroup(GroupID string) error { + // Read group information from the + //grouptrackcontainer json file + groups, err := ReadGroup() + if err != nil { + return err + } + // Gets Group struct based on group ID + // provided + group, err := GetGroup(GroupID) + if err != nil { + return err + } + // Remove Group struct from the groups variable + groups.GroupList = append(groups.GroupList[:group.index], groups.GroupList[group.index+1:]...) + + // Write new groups to the grouptrackcontainer json file + err = groups.WriteGroup() + if err != nil { + return err + } + + return nil +} + +// GetGroup Gets group information based on +// group id provided +func GetGroup(GroupID string) (*Group,error) { + // Read group information from the + //grouptrackcontainer json file + groups, err := ReadGroup() + if err != nil { + return nil, err + } + // Iterate through the set of groups and + // if the group ID matches then return it + for i, group := range groups.GroupList { + if group.ID == GroupID { + group.index = i + return group, nil + } + } + + return nil,errors.New("Group not found. ") +} + // AddGroupToFile Adds Group struct to the GroupTrackContainer File func (grp *Group) AddGroupToFile() error { // Gets all group information from the diff --git a/client/GroupTrackContainer_test.go b/client/GroupTrackContainer_test.go index a9de776..11f46bb 100644 --- a/client/GroupTrackContainer_test.go +++ b/client/GroupTrackContainer_test.go @@ -14,3 +14,19 @@ func TestCreateGroup(t *testing.T) { } PrettyPrint(group) } + +func TestRemoveGroup(t *testing.T) { + // Creates a new group + group, err := CreateGroup() + if err != nil { + fmt.Println(err) + t.Fail() + } + // Removes the new group + // creates + err = RemoveGroup(group.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } +} diff --git a/client/trackcontainers/grouptrackcontainers.json b/client/trackcontainers/grouptrackcontainers.json index 0401c79..3d4b0c6 100644 --- a/client/trackcontainers/grouptrackcontainers.json +++ b/client/trackcontainers/grouptrackcontainers.json @@ -3,6 +3,10 @@ { "ID": "grp72f16ae0-4f68-4245-b570-93fb31de887e", "TrackContainer": null + }, + { + "ID": "grpf184d181-68ac-4829-a801-a7a585421f68", + "TrackContainer": null } ] } \ No newline at end of file