// Update a group // only for Tat admin func (g *GroupsController) Update(ctx *gin.Context) { var paramJSON groupUpdateJSON ctx.Bind(¶mJSON) group, err := GetParam(ctx, "group") if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Group in query"}) return } groupToUpdate, err := groupDB.FindByName(group) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Group"}) return } if paramJSON.Name != groupToUpdate.Name { groupnameExists := groupDB.IsGroupnameExists(paramJSON.Name) if groupnameExists { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Group Name already exists"}) return } } user, err := PreCheckUser(ctx) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while fetching user"}) return } err = groupDB.Update(groupToUpdate, paramJSON.Name, paramJSON.Description, &user) if err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while update group"}) return } if paramJSON.Name != groupToUpdate.Name { if err := topicDB.ChangeGroupnameOnTopics(groupToUpdate.Name, paramJSON.Name); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while update group on topics"}) return } } ctx.JSON(http.StatusCreated, "") }
// preCheckGroup checks if group exists and is admin on topic func (t *TopicsController) preCheckGroup(ctx *gin.Context, paramJSON *tat.ParamTopicGroupJSON) (*tat.Topic, error) { if groupExists := groupDB.IsGroupnameExists(paramJSON.Groupname); !groupExists { return nil, tat.NewError(http.StatusNotFound, "groupname %s does not exist", paramJSON.Groupname) } return t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic) }