示例#1
0
文件: groups.go 项目: ovh/tat
// Delete deletes requested group
// only for Tat admin
func (g *GroupsController) Delete(ctx *gin.Context) {
	groupName, err := GetParam(ctx, "group")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Group in query"})
		return
	}

	groupToDelete, err := groupDB.FindByName(groupName)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": "Invalid Group"})
		return
	}

	user, err := PreCheckUser(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while fetching user"})
		return
	}

	c := tat.TopicCriteria{}
	c.Skip = 0
	c.Limit = 10
	c.Group = groupToDelete.Name

	count, topics, err := topicDB.ListTopics(&c, &user, false, false, false)
	if err != nil {
		log.Errorf("Error while getting topics associated to group %s:%s", groupToDelete.Name, err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Errorf("Error while getting topics associated to group")})
		return
	}

	if len(topics) > 0 {
		e := fmt.Sprintf("Group %s associated to %d topic, you can't delete it", groupToDelete.Name, count)
		log.Errorf(e)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Errorf(e)})
		return
	}

	if err = groupDB.Delete(groupToDelete, &user); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error while deleting Group: %s", err.Error())})
		return
	}
	ctx.JSON(http.StatusOK, "")
}
示例#2
0
文件: topics.go 项目: ovh/tat
func (*TopicsController) buildCriteria(ctx *gin.Context, user *tat.User) *tat.TopicCriteria {
	c := tat.TopicCriteria{}
	skip, e := strconv.Atoi(ctx.DefaultQuery("skip", "0"))
	if e != nil {
		skip = 0
	}
	c.Skip = skip
	limit, e2 := strconv.Atoi(ctx.DefaultQuery("limit", "500"))
	if e2 != nil {
		limit = 500
	}
	c.Limit = limit
	c.IDTopic = ctx.Query("idTopic")
	c.Topic = ctx.Query("topic")
	if c.Topic != "" && !strings.HasPrefix(c.Topic, "/") {
		c.Topic = "/" + c.Topic
	}
	c.Description = ctx.Query("description")
	c.DateMinCreation = ctx.Query("dateMinCreation")
	c.DateMaxCreation = ctx.Query("dateMaxCreation")
	c.GetNbMsgUnread = ctx.Query("getNbMsgUnread")
	c.OnlyFavorites = ctx.Query("onlyFavorites")
	c.GetForTatAdmin = ctx.Query("getForTatAdmin")
	c.TopicPath = ctx.Query("topicPath")

	if c.OnlyFavorites == "true" {
		c.Topic = strings.Join(user.FavoritesTopics, ",")
	}

	if c.SortBy == "" {
		c.SortBy = "topic"
	}
	return &c
}