// curl -H "Content-Type: application/json" -X POST -d '{"userId":1,"title":"i dont get it","body":"no really i dont"}' http://localhost:9999/s/1/posts // PostCreate creates a new post func PostCreate(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 } sub, err := models.GetSubByID(id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"status": "not found"}) return } var json models.Post if c.BindJSON(&json) == nil { json.SubID = sub.ID // TODO: userId from session json.UserID = 1 err := models.CreatePost(json) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"status": "internal server error"}) } 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"}) } }
// PostIndex shows all post in a given sub func PostIndex(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 0, 64) sub := models.Sub{} if err != nil { // if it is not an id, maybe it is a slug slug := models.Slugify(c.Param("id")) sub, err = models.GetSubBySlug(slug) if err != nil { c.JSON(http.StatusNotFound, gin.H{"status": "not found"}) return } } else { sub, err = models.GetSubByID(id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"status": "not found"}) return } } posts, err := models.GetPosts(sub.ID) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"}) return } c.JSON(http.StatusOK, posts) }
// 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) }
// SubShow shows a sub by id func SubShow(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 0, 64) sub := models.Sub{} if err != nil { // if it is not an id, maybe it is a slug slug := models.Slugify(c.Param("id")) sub, err = models.GetSubBySlug(slug) if err != nil { c.JSON(http.StatusNotFound, gin.H{"status": "not found"}) return } } else { sub, err = models.GetSubByID(id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"status": "not found"}) return } } c.JSON(http.StatusOK, sub) }
// SubDelete deletes a sub by id func SubDelete(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.GetSubByID(id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"status": "not found"}) return } err = models.DeleteSub(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"}) }
// curl -H "Content-Type: application/json" -X PATCH -d '{"name":"random", "description":"something"}' http://localhost:9999/s/:id // SubUpdate updates a sub by id func SubUpdate(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 } sub, err := models.GetSubByID(id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"status": "not found"}) return } if c.BindJSON(&sub) == nil { // TODO: maybe catch if there are keys in the json that dont match the struct? err := models.UpdateSub(sub) 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"}) } }