chore: Add logic for put and get by id handler
Some checks failed
Backend ci / build (push) Has been cancelled
Some checks failed
Backend ci / build (push) Has been cancelled
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PostHandlers struct {
|
||||
@@ -41,11 +42,28 @@ func (h *PostHandlers) GetPosts(c *gin.Context) {
|
||||
// @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 500 {object} map[string]string "Internal server error"
|
||||
// @Router /posts/{id} [get]
|
||||
func GetPost(c *gin.Context) {
|
||||
log.Info("GetPost")
|
||||
func (h *PostHandlers) GetPost(c *gin.Context) {
|
||||
var result storage.PostReq
|
||||
id_p := c.Param("id")
|
||||
id, err := strconv.Atoi(id_p)
|
||||
if err != nil {
|
||||
log.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
result, err = h.repo.GetByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
log.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
log.Info("200 OK GET /posts/" + id_p)
|
||||
c.JSON(200, result)
|
||||
// TODO: added validaton for 400 response
|
||||
}
|
||||
|
||||
// CreatePost godoc
|
||||
@@ -84,11 +102,31 @@ func (h *PostHandlers) CreatePost(c *gin.Context) {
|
||||
// @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.Post
|
||||
// @Success 200 {object} storage.PostCreate
|
||||
// @Router /posts/{id} [put]
|
||||
func UpdatePost(c *gin.Context) {
|
||||
log.Info("UpdatePost")
|
||||
func (h *PostHandlers) UpdatePost(c *gin.Context) {
|
||||
id_p := c.Param("id")
|
||||
id, err := strconv.Atoi(id_p)
|
||||
if err != nil {
|
||||
log.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)
|
||||
log.Info("200 OK PUT /posts/" + id_p)
|
||||
}
|
||||
|
||||
// DeletePost godoc
|
||||
|
||||
Reference in New Issue
Block a user