chore: coded post req for create posts on db
All checks were successful
Backend ci / build (push) Successful in 7m43s
All checks were successful
Backend ci / build (push) Successful in 7m43s
This commit is contained in:
@@ -2,9 +2,19 @@ package handlers
|
||||
|
||||
import (
|
||||
"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
|
||||
}
|
||||
|
||||
func NewPostHandler(repo repositories.PostRepository) *PostHandlers {
|
||||
return &PostHandlers{repo: repo}
|
||||
}
|
||||
|
||||
var log = logger.New(false)
|
||||
|
||||
// GetPosts godoc
|
||||
@@ -14,9 +24,16 @@ var log = logger.New(false)
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} []storage.PostReq
|
||||
// @Router /api/v1/posts [get]
|
||||
func GetPosts(c *gin.Context) {
|
||||
log.Info("GetPosts")
|
||||
// @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 {
|
||||
log.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
log.Info("200 OK GET /posts")
|
||||
c.JSON(200, result)
|
||||
}
|
||||
|
||||
// GetPost godoc
|
||||
@@ -26,21 +43,40 @@ func GetPosts(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.PostReq
|
||||
// @Router /api/v1/posts/{id} [get]
|
||||
// @Router /posts/{id} [get]
|
||||
func GetPost(c *gin.Context) {
|
||||
log.Info("GetPost")
|
||||
}
|
||||
|
||||
// CreatePost godoc
|
||||
// @Summary Create post
|
||||
// @Description Create post
|
||||
// @Description Create new post
|
||||
// @Tags posts
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /api/v1/posts [post]
|
||||
func CreatePost(c *gin.Context) {
|
||||
log.Info("CreatePost")
|
||||
// @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
|
||||
@@ -50,7 +86,7 @@ func CreatePost(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /api/v1/posts/{id} [put]
|
||||
// @Router /posts/{id} [put]
|
||||
func UpdatePost(c *gin.Context) {
|
||||
log.Info("UpdatePost")
|
||||
}
|
||||
@@ -62,7 +98,7 @@ func UpdatePost(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /api/v1/posts/{id} [delete]
|
||||
// @Router /posts/{id} [delete]
|
||||
func DeletePost(c *gin.Context) {
|
||||
log.Info("DeletePost")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user