feat: update handlers in posts
All checks were successful
Backend ci / build (push) Successful in 3m29s
All checks were successful
Backend ci / build (push) Successful in 3m29s
This commit is contained in:
@@ -33,6 +33,9 @@ func (h *PostHandlers) GetPosts(c *gin.Context) {
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
if result == nil {
|
||||
c.JSON(200, gin.H{"Status": "No Post found"})
|
||||
}
|
||||
h.logger.Info("200 OK GET /posts")
|
||||
c.JSON(200, result)
|
||||
}
|
||||
@@ -46,15 +49,25 @@ func (h *PostHandlers) GetPosts(c *gin.Context) {
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.PostReq
|
||||
// @Failure 400 {object} map[string]string "Invalid ID format"
|
||||
// @Failure 404 {object} map[string]string "Post not found"
|
||||
// @Failure 500 {object} map[string]string "Internal server error"
|
||||
// @Router /posts/{id} [get]
|
||||
func (h *PostHandlers) GetPost(c *gin.Context) {
|
||||
var result storage.PostReq
|
||||
last_id, err := h.repo.GetLastID(c.Request.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
id_p := c.Param("id")
|
||||
id, err := strconv.Atoi(id_p)
|
||||
if err != nil {
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
c.JSON(400, gin.H{"error": "Invalid ID format"})
|
||||
}
|
||||
if id > last_id {
|
||||
c.JSON(404, gin.H{"error": "Post not found"})
|
||||
return
|
||||
}
|
||||
result, err = h.repo.GetByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
@@ -63,7 +76,7 @@ func (h *PostHandlers) GetPost(c *gin.Context) {
|
||||
}
|
||||
h.logger.Info("200 OK GET /posts/" + id_p)
|
||||
c.JSON(200, result)
|
||||
// TODO: added validaton for 400 response
|
||||
|
||||
}
|
||||
|
||||
// CreatePost godoc
|
||||
@@ -133,9 +146,29 @@ func (h *PostHandlers) UpdatePost(c *gin.Context) {
|
||||
// @Summary Delete post
|
||||
// @Description Delete post
|
||||
// @Tags posts
|
||||
// @Param id path int true "Post ID"
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /posts/{id} [delete]
|
||||
func DeletePost(c *gin.Context) {
|
||||
func (h *PostHandlers) DeletePost(c *gin.Context) {
|
||||
id_p := c.Param("id")
|
||||
id, err := strconv.Atoi(id_p)
|
||||
if err != nil {
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.JSON(400, gin.H{"error": "Invalid ID format"})
|
||||
}
|
||||
exsist := h.repo.IsExist(c.Request.Context(), id)
|
||||
if !exsist {
|
||||
c.JSON(404, gin.H{"error": "Post not found"})
|
||||
return
|
||||
}
|
||||
err = h.repo.Delete(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
h.logger.Info("200 OK DELETE /posts/" + id_p)
|
||||
c.JSON(200, gin.H{"Status": "Post deleted"})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user