feat: update handlers in posts
All checks were successful
Backend ci / build (push) Successful in 3m29s

This commit is contained in:
d3m0k1d
2026-02-13 14:46:44 +03:00
parent 15cf6cca8a
commit 2c198e56eb
4 changed files with 69 additions and 5 deletions

View File

@@ -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"})
}

View File

@@ -3,6 +3,7 @@ package handlers
import (
"database/sql"
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/auth"
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
"github.com/gin-gonic/gin"
)
@@ -16,10 +17,11 @@ func Register(router *gin.Engine, db *sql.DB) {
v1.GET("/auth/github", handler_auth.LoginGithub)
posts := v1.Group("posts")
{
posts.GET("/", handler_posts.GetPosts)
posts.GET("/:id", handler_posts.GetPost)
posts.POST("/", handler_posts.CreatePost)
posts.PUT("/:id", handler_posts.UpdatePost)
posts.DELETE("/:id", DeletePost)
posts.DELETE("/:id", handler_posts.DeletePost).Use(auth.JWTMiddleware())
}
}