From 2c198e56eb835d0b90bc1b164dd834b51636c1d5 Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Fri, 13 Feb 2026 14:46:44 +0300 Subject: [PATCH] feat: update handlers in posts --- backend/internal/handlers/post_handlers.go | 39 +++++++++++++++++-- .../internal/handlers/registry_handlers.go | 4 +- backend/internal/repositories/interface.go | 2 + .../internal/repositories/post_repository.go | 29 +++++++++++++- 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/backend/internal/handlers/post_handlers.go b/backend/internal/handlers/post_handlers.go index 0e074d4..f024dc3 100644 --- a/backend/internal/handlers/post_handlers.go +++ b/backend/internal/handlers/post_handlers.go @@ -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"}) + } diff --git a/backend/internal/handlers/registry_handlers.go b/backend/internal/handlers/registry_handlers.go index 2109afb..b03e6dd 100644 --- a/backend/internal/handlers/registry_handlers.go +++ b/backend/internal/handlers/registry_handlers.go @@ -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()) } } diff --git a/backend/internal/repositories/interface.go b/backend/internal/repositories/interface.go index 33819c2..d81e6ef 100644 --- a/backend/internal/repositories/interface.go +++ b/backend/internal/repositories/interface.go @@ -9,6 +9,8 @@ import ( type PostRepository interface { GetAll(ctx context.Context) ([]storage.PostReq, error) GetByID(ctx context.Context, id int) (storage.PostReq, error) + GetLastID(ctx context.Context) (int, error) + IsExist(ctx context.Context, id int) bool Create(ctx context.Context, post storage.Post) error Update(ctx context.Context, id int, post storage.Post) error Delete(ctx context.Context, id int) error diff --git a/backend/internal/repositories/post_repository.go b/backend/internal/repositories/post_repository.go index 7917b3f..d248b14 100644 --- a/backend/internal/repositories/post_repository.go +++ b/backend/internal/repositories/post_repository.go @@ -92,6 +92,33 @@ func (p *postRepository) Update(ctx context.Context, id int, post storage.Post) } func (p *postRepository) Delete(ctx context.Context, id int) error { - + _, err := p.db.Exec("DELETE FROM posts WHERE id = ?", id) + if err != nil { + return err + } + p.logger.Info("Post deleted:", "id", id) return nil } + +func (p *postRepository) GetLastID(ctx context.Context) (int, error) { + var id int + row := p.db.QueryRow("SELECT id FROM posts ORDER BY id DESC LIMIT 1") + err := row.Scan(&id) + if err != nil { + p.logger.Error("error scan: " + err.Error()) + } + return id, nil +} + +func (p *postRepository) IsExist(ctx context.Context, id int) bool { + var exists int + err := p.db.QueryRowContext(ctx, "SELECT 1 FROM posts WHERE id = ? LIMIT 1", id).Scan(&exists) + if err != nil { + if err == sql.ErrNoRows { + return false + } + p.logger.Error("error checking post existence: " + err.Error()) + return false + } + return true +}