Added GetAllOffset. Post controller completed.

This commit is contained in:
KamilM1205 2025-09-20 19:34:17 +04:00
parent 5b18009658
commit 41944884b4
12 changed files with 229 additions and 71 deletions

View file

@ -11,6 +11,7 @@ type PostService interface {
FindById(queries.PostFindByIdQuery) (*queries.PostFindByIdResult, error)
FindAllByUserName(queries.PostFindAllByUserNameQuery) (*queries.PostFindAllByUserNameResult, error)
GetAll() (*queries.PostGetAllResult, error)
GetAllOffset(int) (*queries.PostGetAllResult, error)
Update(commands.UpdatePostCommand) (*common.PostResult, error)
Delete(commands.DeletePostCommand) error
}

View file

@ -7,6 +7,7 @@ import (
"58team_blog/internal/application/queries"
"58team_blog/internal/domain/entities"
"58team_blog/internal/domain/repository"
"errors"
"fmt"
"time"
)
@ -86,6 +87,22 @@ func (s *PostService) GetAll() (*queries.PostGetAllResult, error) {
return result, nil
}
func (s *PostService) GetAllOffset(offset int) (*queries.PostGetAllResult, error) {
if offset < 0 {
return nil, errors.New("offset is less than 0")
}
posts, err := s.repo.GetAllOffset(offset)
if err != nil {
return nil, err
}
result := mapper.CreatePostGetAllResult(posts)
return result, nil
}
func (s *PostService) Update(cmd commands.UpdatePostCommand) (*common.PostResult, error) {
post, err := s.repo.FindById(cmd.Id)
if err != nil {

View file

@ -11,6 +11,7 @@ type PostRepository interface {
FindById(uuid.UUID) (*entities.Post, error)
FindAllByUserName(string) ([]*entities.Post, error)
GetAll() ([]*entities.Post, error)
GetAllOffset(int) ([]*entities.Post, error)
Update(*entities.Post) error
Delete(uuid.UUID) error
}

View file

@ -3,6 +3,7 @@ package repo
import (
"58team_blog/internal/domain/entities"
"58team_blog/internal/infrastructure/db"
"strconv"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
@ -65,6 +66,15 @@ func (r *PostRepository) GetAll() ([]*entities.Post, error) {
return entity_list, err
}
func (r *PostRepository) GetAllOffset(offset int) ([]*entities.Post, error) {
var entity_list []*entities.Post
query := "SELECT * FROM " + entities.PostTable + " ORDER BY createdat, updatedat OFFSET " + strconv.Itoa(offset) + " LIMIT 5"
err := r.conn.Conn.Select(&entity_list, query)
return entity_list, err
}
func (r *PostRepository) Update(entity *entities.Post) error {
query := "UPDATE " + entities.PostTable + "SET title=:title, description=:description, content=:content, updatedat=:updatedat WHERE id=:id"

View file

@ -9,6 +9,7 @@ import (
"58team_blog/internal/interfaces/api/responses"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
@ -31,10 +32,10 @@ func CreatePostController(service *services.PostService) PostController {
// @Tags post
// @Accept json
// @Produce json
// @Param request body requests.CreatePostRequest true "Post data"
// @Success 200 {object} responses.PostResponse
// @Failure 400 {object} responses.ErrorResponse
// @Failure 500 {object} responses.ErrorResponse
// @Param request body requests.CreatePostRequest true "Post data"
// @Success 200 {object} responses.PostResponse
// @Failure 400 {object} responses.ErrorResponse
// @Failure 500 {object} responses.ErrorResponse
// @Router /post [post]
func (r *PostController) Post(c *gin.Context) {
var request requests.CreatePostRequest
@ -42,7 +43,7 @@ func (r *PostController) Post(c *gin.Context) {
if err := c.BindJSON(&request); err != nil {
log.Println(err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "BadRequest")
c.IndentedJSON(resp.ErrorCode, resp)
c.JSON(resp.ErrorCode, resp)
return
}
@ -50,7 +51,7 @@ func (r *PostController) Post(c *gin.Context) {
if err != nil {
log.Println(err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Incorrect user id")
c.IndentedJSON(resp.ErrorCode, resp)
c.JSON(resp.ErrorCode, resp)
return
}
@ -65,13 +66,13 @@ func (r *PostController) Post(c *gin.Context) {
if err != nil {
log.Println(err)
resp := responses.CreateErrorResponse(http.StatusInternalServerError, "Internal server error")
c.IndentedJSON(resp.ErrorCode, resp)
c.JSON(resp.ErrorCode, resp)
return
}
response := dto.ResponseFromPostResult(res)
c.IndentedJSON(http.StatusOK, response)
c.JSON(http.StatusOK, response)
}
// GetAllPost godoc
@ -80,15 +81,15 @@ func (r *PostController) Post(c *gin.Context) {
// @Description Return first 5 posts
// @Tags post
// @Produce json
// @Success 200 {array} responses.GetListPostResponseItem
// @Failure 500 {object} responses.ErrorResponse
// @Success 200 {array} responses.GetListPostResponseItem
// @Failure 500 {object} responses.ErrorResponse
// @Router /post [get]
func (r *PostController) GetAll(c *gin.Context) {
result, err := r.service.GetAll()
if err != nil {
log.Println(err)
resp := responses.CreateErrorResponse(http.StatusInternalServerError, "Internal server error")
c.IndentedJSON(resp.ErrorCode, resp)
c.JSON(resp.ErrorCode, resp)
return
}
@ -98,26 +99,48 @@ func (r *PostController) GetAll(c *gin.Context) {
}
// GetAllWithOffsetPost godoc
// @Summary Get posts after offset
// @Description return 5 posts after first offset posts
// @Tags post
// @Param offset query int true "Offset of posts"
// @Produce json
// @Success 200 {array} responses.GetListPostResponseItem
// @Router /post/{offset} [get]
//
// @Summary Get posts after offset
// @Description return 5 posts after first offset posts
// @Tags post
// @Param offset path int true "Offset of posts"
// @Produce json
// @Success 200 {array} responses.GetListPostResponseItem
// @Failure 400 {object} responses.ErrorResponse
// @Router /post/offset/{offset} [get]
func (r *PostController) GetAllWithOffset(c *gin.Context) {
offset_param := c.Param("offset")
offset, err := strconv.Atoi(offset_param)
if err != nil {
log.Println("Post get all with offset error: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp)
return
}
result, err := r.service.GetAllOffset(offset)
if err != nil {
log.Println("Post get all with offset error: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp)
return
}
res := dto.ResponseFromPostGetAllResult(result)
c.JSON(http.StatusOK, res)
}
// GetByIdPost godoc
// @Summary Get post by id
// @Description get post by id
// @Tags post
// @Param id query string true "Id of post"
// @Produce json
// @Success 200 {array} responses.PostResponse
// @Failure 400 {object} responses.ErrorResponse
// @Router /post/{id} [get]
//
// @Summary Get post by id
// @Description get post by id
// @Tags post
// @Param id path string true "Id of post"
// @Produce json
// @Success 200 {array} responses.PostResponse
// @Failure 400 {object} responses.ErrorResponse
// @Router /post/{id} [get]
func (r *PostController) GetById(c *gin.Context) {
id := c.Param("id")
@ -147,36 +170,86 @@ func (r *PostController) GetById(c *gin.Context) {
}
// PutPost godoc
// @Summary Update post content
// @Description update post content
// @Tags post
// @Param id query string true "Id of post"
//
// @Param request body requests.PutPostRequest true "Post data"
// @Summary Update post content
// @Description update post content
// @Tags post
// @Param id path string true "Id of post"
//
// @Produce json
// @Success 200 {object} responses.PostResponse
// @Failure 400 {object} responses.ErrorResponse
// @Router /post/{id} [put]
// @Param request body requests.PutPostRequest true "Post data"
//
// @Produce json
// @Success 200 {object} responses.PostResponse
// @Failure 400 {object} responses.ErrorResponse
// @Router /post/{id} [put]
func (r *PostController) Put(c *gin.Context) {
var request requests.PutPostRequest
id := c.Param("id")
id_valid, err := uuid.Parse(id)
if err != nil {
log.Println("Post: invalid post id: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp)
return
}
if err := c.BindJSON(request); err != nil {
log.Println("Post request error: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp)
return
}
cmd := commands.UpdatePostCommand{
Id: id_valid,
Title: request.Title,
Description: request.Description,
Content: request.Content,
}
post, err := r.service.Update(cmd)
if err != nil {
log.Println("Post service error: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp)
return
}
response := dto.ResponseFromPostResult(post)
c.JSON(http.StatusOK, response)
}
// Delete godoc
// @Summary Delete post
// @Description Delete post by id
// @Tags post
// @Param id query string true "Id of post"
// @Produce json
// @Success 200
// @Router /post/{id} [delete]
//
// @Summary Delete post
// @Description Delete post by id
// @Tags post
// @Param id path string true "Id of post"
// @Produce json
// @Success 200
// @Failure 400 {object} responses.ErrorResponse
// @Router /post/{id} [delete]
func (r *PostController) Delete(c *gin.Context) {
id := c.Param("id")
id_valid, err := uuid.Parse(id)
if err != nil {
log.Println("Post: invalid post id: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp)
return
}
cmd := commands.DeletePostCommand{
Id: id_valid,
}
err = r.service.Delete(cmd)
if err != nil {
log.Println("Post delete error: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp)
return
}
c.Status(http.StatusOK)
}

View file

@ -19,9 +19,9 @@ func CreateUserController(service *services.UserService) UserController {
// @Summary Create new user
// @Description Creates new user in system
// @Param path query string true "Path to image"
// @Produce
// @Success 200
// @Router /images/{path} [get]
// @Produce json
// @Success 200
// @Router /images/{path} [get]
func (r *UserController) Post(c *gin.Context) {
// TODO: return image
panic("Not implemented")

View file

@ -11,8 +11,10 @@ func BindPostAdmin(service *services.PostService, group *gin.RouterGroup) {
post := controllers.CreatePostController(service)
g := group.Group("/post")
g.GET("/", post.GetAllPost)
g.GET("/:id", post.GetByIdPost)
g.POST("/", post.PostPost)
g.PUT("/:id", post.PutPost)
g.GET("/", post.GetAll)
g.GET("/offset/:offset", post.GetAllWithOffset)
g.GET("/:id", post.GetById)
g.POST("/", post.Post)
g.PUT("/:id", post.Put)
g.DELETE("/:id", post.Delete)
}