Files
d3m0k1d.ru/backend/internal/handlers/post_handlers.go
d3m0k1d 2c198e56eb
All checks were successful
Backend ci / build (push) Successful in 3m29s
feat: update handlers in posts
2026-02-13 14:46:44 +03:00

175 lines
4.3 KiB
Go

package handlers
import (
"strconv"
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
"github.com/gin-gonic/gin"
)
type PostHandlers struct {
repo repositories.PostRepository
logger *logger.Logger
}
func NewPostHandlers(repo repositories.PostRepository) *PostHandlers {
return &PostHandlers{repo: repo, logger: logger.New(false)}
}
// GetPosts godoc
// @Summary Get all posts
// @Description Get all posts
// @Tags posts
// @Accept json
// @Produce json
// @Success 200 {object} []storage.PostReq
// @Router /posts [get]
func (h *PostHandlers) GetPosts(c *gin.Context) {
var result []storage.PostReq
result, err := h.repo.GetAll(c.Request.Context())
if err != nil {
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)
}
// GetPost godoc
// @Summary Get post by id
// @Description Get post by id
// @Tags posts
// @Accept json
// @Param id path int true "Post ID"
// @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.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 {
h.logger.Error("error request: " + err.Error())
c.Status(500)
}
h.logger.Info("200 OK GET /posts/" + id_p)
c.JSON(200, result)
}
// CreatePost godoc
// @Summary Create post
// @Description Create new post
// @Tags posts
// @Accept json
// @Produce json
// @Param post body storage.PostCreate true "Post data"
// @Success 200 {object} storage.PostReq
// @Failure 400 {object} gin.H
// @Router /posts [post]
func (h *PostHandlers) CreatePost(c *gin.Context) {
var req storage.PostCreate
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
post := storage.Post{
Title: req.Title,
Content: req.Content,
}
if err := h.repo.Create(c.Request.Context(), post); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, post)
}
// UpdatePost godoc
// @Summary Update post
// @Description Update post
// @Tags posts
// @Accept json
// @Param id path int true "Post ID"
// @Param post body storage.PostCreate true "Post data"
// @Produce json
// @Success 200 {object} storage.PostCreate
// @Router /posts/{id} [put]
func (h *PostHandlers) UpdatePost(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.Status(500)
}
var req storage.Post
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
err = h.repo.Update(c.Request.Context(), id, req)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, req)
h.logger.Info("200 OK PUT /posts/" + id_p)
}
// DeletePost godoc
// @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 (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"})
}