Beispiel #1
0
// curl -H "Content-Type: application/json" -X POST -d '{"commentableId":3,"commentableType":"post","body":"the body of a comment"}' http://localhost:9999/comments
// CommentCreate creates a comment, validates that it belongs to something commentable
func CommentCreate(c *gin.Context) {
	var comment models.Comment
	if c.BindJSON(&comment) == nil {
		comment.UserID = 1

		if comment.CommentableType == "post" {
			_, err := models.GetPost(comment.CommentableID)
			if err != nil {
				c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
				return
			}
		} else if comment.CommentableType == "comment" {
			_, err := models.GetComment(comment.CommentableID)
			if err != nil {
				c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
				return
			}
		} else {
			c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
			return
		}

		// TODO: userID should come from session, commentable id/type maybe from route?
		err := models.CreateComment(comment)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		} else {
			// TODO: add location header with new resource url
			c.JSON(http.StatusCreated, gin.H{"status": "created"})
		}
	} else {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
	}
}
Beispiel #2
0
// CommentShow shows a comment by Id
func CommentShow(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}
	comment, err := models.GetComment(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
		return
	}

	c.JSON(http.StatusOK, comment)
}
Beispiel #3
0
// curl -H "Content-Type: application/json" -X POST -d '{"value":0}' http://localhost:9999/comments/20/vote
// CommentVoteCreate creates or updates a vote for the user on a given comment
func CommentVoteCreate(c *gin.Context) {
	// a user can only have 1 vote per votable item
	// make sure those items exist
	// check if a vote exists, if so update it
	// otherwise create

	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}
	_, err = models.GetComment(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
		return
	}

	var vote models.Vote

	if c.BindJSON(&vote) == nil {
		vote.UserID = 1 // TODO: userID should come from session
		vote.VotableID = id
		vote.VotableType = "comment"
		nvote, err := models.GetVoteForUser(vote)
		if err != nil {
			err = models.CreateVote(vote)
			if err != nil {
				c.JSON(http.StatusInternalServerError, gin.H{"status": "internal server error"})
			} else {
				c.JSON(http.StatusCreated, gin.H{"status": "created"})
			}
		} else {
			// TODO: error here doesn't necessarily mean not found
			vote.ID = nvote.ID
			err = models.UpdateVote(vote)
			if err != nil {
				c.JSON(http.StatusInternalServerError, gin.H{"status": "internal server error"})
				return
			} else {
				c.JSON(http.StatusOK, gin.H{"status": "updated"})
				return
			}
		}
	}
}
Beispiel #4
0
// CommentIndex shows all comments belonging to a post or comment
func CommentIndex(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}

	// assume it is a post
	commentableType := "post"
	if c.Param("postId") != "" {
		postId, err := strconv.ParseUint(c.Param("postId"), 0, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
			return
		}
		_, err = models.GetSubByID(id)
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
			return
		}
		_, err = models.GetPost(postId)
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
			return
		}
		id = postId // wish i didnt have to do this and could reuse id, but routes
	} else {
		_, err = models.GetComment(id)
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
			return
		}
		commentableType = "comment"
	}

	comments, err := models.GetComments(id, commentableType)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
		return
	}

	c.JSON(http.StatusOK, comments)
}
Beispiel #5
0
// curl -H "Content-Type: application/json" -X DELETE http://localhost:9999/comments/1
// CommentDelete deletes a comment by id
func CommentDelete(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}
	_, err = models.GetComment(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
		return
	}

	err = models.DeleteComment(id)
	if err != nil {
		// could be a database error, but probably not
		c.JSON(http.StatusInternalServerError, gin.H{"status": "internal server error"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": "deleted"})
}
Beispiel #6
0
// curl -H "Content-Type: application/json" -X PATCH -d '{"body":"the body of a sss comment"}' http://localhost:9999/comments/20
// CommentUpdate updates a comment by id
func CommentUpdate(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 0, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}
	comment, err := models.GetComment(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
		return
	}

	if c.BindJSON(&comment) == nil {
		// TODO: maybe catch if there are keys in the json that dont match the struct?
		err := models.UpdateComment(comment)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"status": "internal server error"})
		} else {
			c.JSON(http.StatusOK, gin.H{"status": "updated"})
		}
	} else {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
	}
}