Beispiel #1
0
// just change status
// @router /del/:id [get]
func (c *CommentController) Del() {
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	res := &common.Response{}
	defer func() {
		c.Data["json"] = res
		c.ServeJSON()
	}()

	if id < 1 {
		res.Code = 1
		res.Msg = "comment id must not be empty"
		return
	}
	comment := models.Comment{Id: id}
	if comment.Update("status") != nil {
		res.Code = 2
		res.Msg = "comment delete failed"
		return
	}
	res.Code = 0
	res.Msg = "delete msg success"
	return
}
Beispiel #2
0
// @router /create [post]
func (c *CommentController) Add() {
	res := &common.CommentCreateResponse{}
	defer func() {
		c.Data["json"] = res
		c.ServeJSON()
	}()

	var comment models.Comment

	id, _ := c.GetInt64("id")
	// TODO check post is exits
	if id > 0 {
		comment.Id = id
		if comment.Read() != nil {
			fmt.Println("comment not exist, will create one")
			comment.Id = 0
		}
	}
	comment.PostId, _ = c.GetInt64("postId")
	comment.UserId, _ = c.GetInt64("userid")
	comment.Content = c.GetString("content")
	comment.ParentId, _ = c.GetInt64("parentId")
	// filter
	if comment.PostId == 0 {
		res.Code = 1
		res.Msg = "no post id"
		return
	}
	if id < 1 { // add
		comment.CreateDate = c.getTime()
		if _, err := comment.Add(); err == nil {
			res.Code = 0
			res.Msg = "create comment success"
			res.CommentId = comment.Id
		} else {
			res.Code = 2
			res.Msg = "create comment error"
			return
		}
	} else { // update
		if comment.Update("updatedate", "content",
			"postid", "userid", "parentid") == nil {
			res.Code = 0
			res.Msg = "update comment success"
			res.CommentId = comment.Id
		} else {
			res.Code = 2
			res.Msg = "update comment error"
			return
		}
	}
	return
}