Example #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"})
	}
}
Example #2
0
// PostShow shows a post by id
func PostShow(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("postId"), 0, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}

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

	c.JSON(http.StatusOK, post)
}
Example #3
0
// curl -H "Content-Type: application/json" -X POST -d '{"value":0}' http://localhost:9999/posts/3/vote
// PostVoteCreate creates or updates a vote for the user on a given post
func PostVoteCreate(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.GetPost(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 = "post"
		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
			}
		}
	}
}
Example #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)
}
Example #5
0
// curl -H "Content-Type: application/json" -X DELETE http://localhost:9999/posts/2
// PostDelete deletes a post by id
func PostDelete(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.GetPost(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
		return
	}

	err = models.DeletePost(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"})
}
Example #6
0
// curl -H "Content-Type: application/json" -X PATCH -d '{"body":"lets all ignore how dirty our country is"}' http://localhost:9999/posts/3
// PostUpdate updates a post by id
func PostUpdate(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
	}
	post, err := models.GetPost(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"status": "not found"})
		return
	}

	if c.BindJSON(&post) == nil {
		// TODO: maybe catch if there are keys in the json that dont match the struct?
		err := models.UpdatePost(post)
		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"})
	}
}