func (p *postHandler) ListPagingByCategory(c *gin.Context) { var posts []models.Post var err error categoryParam := c.Query("category") if categoryParam == "" || !bson.IsObjectIdHex(categoryParam) { p.ListPaging(c) return } pageParam := c.DefaultQuery("page", "1") pageIndex, err := strconv.Atoi(pageParam) if err != nil { pageIndex = 1 } categoryId := bson.ObjectIdHex(categoryParam) if posts, err = postResource.ListPagingByCategory(categoryId, pageIndex, ITEMS_PER_PAGE); err != nil { if err != mgo.ErrNotFound { log.LogError(c.Request, err, "Error in ListPagingByCategory Post", logger) c.AbortWithError(500, err) return } c.AbortWithStatus(404) return } c.JSON(200, posts) }
func (p *categoryHandler) Create(c *gin.Context) { var category models.Category if err := c.Bind(&category); err != nil { errors := categoryResource.ParseError(err) if len(errors) > 0 { c.Error(errors[0]) return } } if err := categoryResource.Create(&category); err != nil { log.LogError(c.Request, err, "Error in Create Category", logger) c.Error(err) return } c.JSON(201, category) }
func (p *postHandler) Update(c *gin.Context) { var post models.Post if err := c.Bind(&post); err != nil { errors := postResource.ParseError(err) if len(errors) > 0 { c.Error(errors[0]) return } } if err := postResource.Update(&post); err != nil { log.LogError(c.Request, err, "Error in Create Post", logger) c.AbortWithError(500, err) return } c.JSON(201, post) }
func (p *categoryHandler) Update(c *gin.Context) { var category models.Category if err := c.Bind(&category); err != nil { errors := categoryResource.ParseError(err) if len(errors) > 0 { c.Error(errors[0]) return } } if err := categoryResource.Update(&category); err != nil { if err != mgo.ErrNotFound { log.LogError(c.Request, err, "Error in Update Category", logger) c.AbortWithError(500, err) return } c.AbortWithError(404, err) return } c.JSON(201, category) }
func (p *postHandler) ListPaging(c *gin.Context) { var posts []models.Post var err error pageParam := c.DefaultQuery("page", "1") pageIndex, err := strconv.Atoi(pageParam) if err != nil { pageIndex = 1 } if posts, err = postResource.ListPaging(pageIndex, ITEMS_PER_PAGE); err != nil { if err != mgo.ErrNotFound { log.LogError(c.Request, err, "Error in ListPaging Post", logger) c.AbortWithError(500, err) return } c.AbortWithStatus(404) return } c.JSON(200, posts) }
func (p *postHandler) GetById(c *gin.Context) { var post models.Post var err error id := c.Params.ByName("id") // Verify id is ObjectId, otherwise bail if !bson.IsObjectIdHex(id) { c.AbortWithStatus(404) return } oid := bson.ObjectIdHex(id) if post, err = postResource.GetById(oid); err != nil { if err != mgo.ErrNotFound { log.LogError(c.Request, err, "Error in GetById Post", logger) c.AbortWithError(500, err) return } c.AbortWithStatus(404) return } c.JSON(200, post) }