feat: update handlers in posts
All checks were successful
Backend ci / build (push) Successful in 3m29s
All checks were successful
Backend ci / build (push) Successful in 3m29s
This commit is contained in:
@@ -33,6 +33,9 @@ func (h *PostHandlers) GetPosts(c *gin.Context) {
|
|||||||
h.logger.Error("error request: " + err.Error())
|
h.logger.Error("error request: " + err.Error())
|
||||||
c.Status(500)
|
c.Status(500)
|
||||||
}
|
}
|
||||||
|
if result == nil {
|
||||||
|
c.JSON(200, gin.H{"Status": "No Post found"})
|
||||||
|
}
|
||||||
h.logger.Info("200 OK GET /posts")
|
h.logger.Info("200 OK GET /posts")
|
||||||
c.JSON(200, result)
|
c.JSON(200, result)
|
||||||
}
|
}
|
||||||
@@ -46,15 +49,25 @@ func (h *PostHandlers) GetPosts(c *gin.Context) {
|
|||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {object} storage.PostReq
|
// @Success 200 {object} storage.PostReq
|
||||||
// @Failure 400 {object} map[string]string "Invalid ID format"
|
// @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"
|
// @Failure 500 {object} map[string]string "Internal server error"
|
||||||
// @Router /posts/{id} [get]
|
// @Router /posts/{id} [get]
|
||||||
func (h *PostHandlers) GetPost(c *gin.Context) {
|
func (h *PostHandlers) GetPost(c *gin.Context) {
|
||||||
var result storage.PostReq
|
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_p := c.Param("id")
|
||||||
id, err := strconv.Atoi(id_p)
|
id, err := strconv.Atoi(id_p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logger.Error("error request: " + err.Error())
|
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)
|
result, err = h.repo.GetByID(c.Request.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -63,7 +76,7 @@ func (h *PostHandlers) GetPost(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
h.logger.Info("200 OK GET /posts/" + id_p)
|
h.logger.Info("200 OK GET /posts/" + id_p)
|
||||||
c.JSON(200, result)
|
c.JSON(200, result)
|
||||||
// TODO: added validaton for 400 response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreatePost godoc
|
// CreatePost godoc
|
||||||
@@ -133,9 +146,29 @@ func (h *PostHandlers) UpdatePost(c *gin.Context) {
|
|||||||
// @Summary Delete post
|
// @Summary Delete post
|
||||||
// @Description Delete post
|
// @Description Delete post
|
||||||
// @Tags posts
|
// @Tags posts
|
||||||
|
// @Param id path int true "Post ID"
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {object} storage.Post
|
// @Success 200 {object} storage.Post
|
||||||
// @Router /posts/{id} [delete]
|
// @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"})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
|
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/auth"
|
||||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -16,10 +17,11 @@ func Register(router *gin.Engine, db *sql.DB) {
|
|||||||
v1.GET("/auth/github", handler_auth.LoginGithub)
|
v1.GET("/auth/github", handler_auth.LoginGithub)
|
||||||
posts := v1.Group("posts")
|
posts := v1.Group("posts")
|
||||||
{
|
{
|
||||||
|
|
||||||
posts.GET("/", handler_posts.GetPosts)
|
posts.GET("/", handler_posts.GetPosts)
|
||||||
posts.GET("/:id", handler_posts.GetPost)
|
posts.GET("/:id", handler_posts.GetPost)
|
||||||
posts.POST("/", handler_posts.CreatePost)
|
posts.POST("/", handler_posts.CreatePost)
|
||||||
posts.PUT("/:id", handler_posts.UpdatePost)
|
posts.PUT("/:id", handler_posts.UpdatePost)
|
||||||
posts.DELETE("/:id", DeletePost)
|
posts.DELETE("/:id", handler_posts.DeletePost).Use(auth.JWTMiddleware())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import (
|
|||||||
type PostRepository interface {
|
type PostRepository interface {
|
||||||
GetAll(ctx context.Context) ([]storage.PostReq, error)
|
GetAll(ctx context.Context) ([]storage.PostReq, error)
|
||||||
GetByID(ctx context.Context, id int) (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
|
Create(ctx context.Context, post storage.Post) error
|
||||||
Update(ctx context.Context, id int, post storage.Post) error
|
Update(ctx context.Context, id int, post storage.Post) error
|
||||||
Delete(ctx context.Context, id int) error
|
Delete(ctx context.Context, id int) error
|
||||||
|
|||||||
@@ -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 {
|
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
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user