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

@ -5,7 +5,7 @@ package docs
import "github.com/swaggo/swag/v2" import "github.com/swaggo/swag/v2"
const docTemplate = `{ const docTemplate = `{
"schemes": {{ marshal .Schemes }},"swagger":"2.0","info":{"description":"{{escape .Description}}","title":"{{.Title}}","termsOfService":"http://swagger.io/terms/","contact":{"name":"API Support","url":"http://www.swagger.io/support","email":"support@swagger.io"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"version":"{{.Version}}"},"host":"{{.Host}}","basePath":"{{.BasePath}}","paths":{"/images/{path}":{"get":{"description":"get image by path","produces":["image/png","image/jpeg"],"summary":"Get an image by path","parameters":[{"type":"string","description":"Path to image","name":"path","in":"query","required":true}],"responses":{"200":{"description":"OK"}}}},"/post":{"get":{"description":"Return first 5 posts","produces":["application/json"],"tags":["post"],"summary":"Get all posts","responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}}}},"post":{"description":"Create new post in blog","consumes":["application/json"],"produces":["application/json"],"tags":["post"],"summary":"Create new post","parameters":[{"description":"Post data","name":"request","in":"body","required":true,"schema":{"$ref":"#/definitions/requests.CreatePostRequest"}}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}},"/post/{id}":{"get":{"description":"get post by id","produces":["application/json"],"tags":["post"],"summary":"Get post by id","parameters":[{"type":"string","description":"Id of post","name":"id","in":"query","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.PostResponse"}}}}},"put":{"description":"update post content","produces":["application/json"],"tags":["post"],"summary":"Update post content","parameters":[{"type":"string","description":"Id of post","name":"id","in":"query","required":true}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}}}}},"/post/{offset}":{"get":{"description":"return 5 posts after first offset posts","produces":["application/json"],"tags":["post"],"summary":"Get posts after offset","parameters":[{"type":"integer","description":"Offset of posts","name":"offset","in":"query","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}}}}}},"definitions":{"requests.CreatePostRequest":{"type":"object","required":["content","description","title","userId"],"properties":{"content":{"type":"string","minLength":36},"description":{"type":"string","maxLength":255,"minLength":8},"title":{"type":"string","maxLength":255,"minLength":8},"userId":{"type":"string"}}},"responses.ErrorResponse":{"type":"object","properties":{"error_code":{"type":"integer"},"message":{"type":"string"}}},"responses.GetListPostResponseItem":{"type":"object","properties":{"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"}}},"responses.PostResponse":{"type":"object","properties":{"content":{"type":"string"},"createdAt":{"type":"string"},"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"},"updatedAt":{"type":"string"},"userId":{"type":"string"}}}},"securityDefinitions":{"BasicAuth":{"type":"basic"}}}` "schemes": {{ marshal .Schemes }},"swagger":"2.0","info":{"description":"{{escape .Description}}","title":"{{.Title}}","termsOfService":"http://swagger.io/terms/","contact":{"name":"API Support","url":"http://www.swagger.io/support","email":"support@swagger.io"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"version":"{{.Version}}"},"host":"{{.Host}}","basePath":"{{.BasePath}}","paths":{"/images/{path}":{"get":{"description":"Creates new user in system","produces":["application/json"],"summary":"Create new user","parameters":[{"type":"string","description":"Path to image","name":"path","in":"query","required":true}],"responses":{"200":{"description":"OK"}}}},"/post":{"get":{"description":"Return first 5 posts","produces":["application/json"],"tags":["post"],"summary":"Get all posts","responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}},"500":{"description":"Internal Server Error","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}},"post":{"description":"Create new post in blog","consumes":["application/json"],"produces":["application/json"],"tags":["post"],"summary":"Create new post","parameters":[{"description":"Post data","name":"request","in":"body","required":true,"schema":{"$ref":"#/definitions/requests.CreatePostRequest"}}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}},"500":{"description":"Internal Server Error","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}},"/post/offset/{offset}":{"get":{"description":"return 5 posts after first offset posts","produces":["application/json"],"tags":["post"],"summary":"Get posts after offset","parameters":[{"type":"integer","description":"Offset of posts","name":"offset","in":"path","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}},"/post/{id}":{"get":{"description":"get post by id","produces":["application/json"],"tags":["post"],"summary":"Get post by id","parameters":[{"type":"string","description":"Id of post","name":"id","in":"path","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.PostResponse"}}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}},"put":{"description":"update post content","produces":["application/json"],"tags":["post"],"summary":"Update post content","parameters":[{"type":"string","description":"Id of post","name":"id","in":"path","required":true},{"description":"Post data","name":"request","in":"body","required":true,"schema":{"$ref":"#/definitions/requests.PutPostRequest"}}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}},"delete":{"description":"Delete post by id","produces":["application/json"],"tags":["post"],"summary":"Delete post","parameters":[{"type":"string","description":"Id of post","name":"id","in":"path","required":true}],"responses":{"200":{"description":"OK"},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}}},"definitions":{"requests.CreatePostRequest":{"type":"object","required":["content","description","title","userId"],"properties":{"content":{"type":"string","minLength":36},"description":{"type":"string","maxLength":255,"minLength":8},"title":{"type":"string","maxLength":255,"minLength":8},"userId":{"type":"string"}}},"requests.PutPostRequest":{"type":"object","properties":{"content":{"type":"string"},"description":{"type":"string"},"title":{"type":"string"}}},"responses.ErrorResponse":{"type":"object","properties":{"error_code":{"type":"integer"},"message":{"type":"string"}}},"responses.GetListPostResponseItem":{"type":"object","properties":{"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"}}},"responses.PostResponse":{"type":"object","properties":{"content":{"type":"string"},"createdAt":{"type":"string"},"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"},"updatedAt":{"type":"string"},"userId":{"type":"string"}}}},"securityDefinitions":{"BasicAuth":{"type":"basic"}}}`
// SwaggerInfo holds exported Swagger Info so clients can modify it // SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{ var SwaggerInfo = &swag.Spec{

View file

@ -1 +1 @@
{"swagger":"2.0","info":{"description":"58team blog's backend","title":"58team blog backend","termsOfService":"http://swagger.io/terms/","contact":{"name":"API Support","url":"http://www.swagger.io/support","email":"support@swagger.io"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"version":"1.0"},"host":"localhost:8080","basePath":"/api/v1","paths":{"/images/{path}":{"get":{"description":"get image by path","produces":["image/png","image/jpeg"],"summary":"Get an image by path","parameters":[{"type":"string","description":"Path to image","name":"path","in":"query","required":true}],"responses":{"200":{"description":"OK"}}}},"/post":{"get":{"description":"Return first 5 posts","produces":["application/json"],"tags":["post"],"summary":"Get all posts","responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}}}},"post":{"description":"Create new post in blog","consumes":["application/json"],"produces":["application/json"],"tags":["post"],"summary":"Create new post","parameters":[{"description":"Post data","name":"request","in":"body","required":true,"schema":{"$ref":"#/definitions/requests.CreatePostRequest"}}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}},"/post/{id}":{"get":{"description":"get post by id","produces":["application/json"],"tags":["post"],"summary":"Get post by id","parameters":[{"type":"string","description":"Id of post","name":"id","in":"query","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.PostResponse"}}}}},"put":{"description":"update post content","produces":["application/json"],"tags":["post"],"summary":"Update post content","parameters":[{"type":"string","description":"Id of post","name":"id","in":"query","required":true}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}}}}},"/post/{offset}":{"get":{"description":"return 5 posts after first offset posts","produces":["application/json"],"tags":["post"],"summary":"Get posts after offset","parameters":[{"type":"integer","description":"Offset of posts","name":"offset","in":"query","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}}}}}},"definitions":{"requests.CreatePostRequest":{"type":"object","required":["content","description","title","userId"],"properties":{"content":{"type":"string","minLength":36},"description":{"type":"string","maxLength":255,"minLength":8},"title":{"type":"string","maxLength":255,"minLength":8},"userId":{"type":"string"}}},"responses.ErrorResponse":{"type":"object","properties":{"error_code":{"type":"integer"},"message":{"type":"string"}}},"responses.GetListPostResponseItem":{"type":"object","properties":{"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"}}},"responses.PostResponse":{"type":"object","properties":{"content":{"type":"string"},"createdAt":{"type":"string"},"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"},"updatedAt":{"type":"string"},"userId":{"type":"string"}}}},"securityDefinitions":{"BasicAuth":{"type":"basic"}}} {"swagger":"2.0","info":{"description":"58team blog's backend","title":"58team blog backend","termsOfService":"http://swagger.io/terms/","contact":{"name":"API Support","url":"http://www.swagger.io/support","email":"support@swagger.io"},"license":{"name":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"},"version":"1.0"},"host":"localhost:8080","basePath":"/api/v1","paths":{"/images/{path}":{"get":{"description":"Creates new user in system","produces":["application/json"],"summary":"Create new user","parameters":[{"type":"string","description":"Path to image","name":"path","in":"query","required":true}],"responses":{"200":{"description":"OK"}}}},"/post":{"get":{"description":"Return first 5 posts","produces":["application/json"],"tags":["post"],"summary":"Get all posts","responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}},"500":{"description":"Internal Server Error","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}},"post":{"description":"Create new post in blog","consumes":["application/json"],"produces":["application/json"],"tags":["post"],"summary":"Create new post","parameters":[{"description":"Post data","name":"request","in":"body","required":true,"schema":{"$ref":"#/definitions/requests.CreatePostRequest"}}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}},"500":{"description":"Internal Server Error","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}},"/post/offset/{offset}":{"get":{"description":"return 5 posts after first offset posts","produces":["application/json"],"tags":["post"],"summary":"Get posts after offset","parameters":[{"type":"integer","description":"Offset of posts","name":"offset","in":"path","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.GetListPostResponseItem"}}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}},"/post/{id}":{"get":{"description":"get post by id","produces":["application/json"],"tags":["post"],"summary":"Get post by id","parameters":[{"type":"string","description":"Id of post","name":"id","in":"path","required":true}],"responses":{"200":{"description":"OK","schema":{"type":"array","items":{"$ref":"#/definitions/responses.PostResponse"}}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}},"put":{"description":"update post content","produces":["application/json"],"tags":["post"],"summary":"Update post content","parameters":[{"type":"string","description":"Id of post","name":"id","in":"path","required":true},{"description":"Post data","name":"request","in":"body","required":true,"schema":{"$ref":"#/definitions/requests.PutPostRequest"}}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/responses.PostResponse"}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}},"delete":{"description":"Delete post by id","produces":["application/json"],"tags":["post"],"summary":"Delete post","parameters":[{"type":"string","description":"Id of post","name":"id","in":"path","required":true}],"responses":{"200":{"description":"OK"},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/responses.ErrorResponse"}}}}}},"definitions":{"requests.CreatePostRequest":{"type":"object","required":["content","description","title","userId"],"properties":{"content":{"type":"string","minLength":36},"description":{"type":"string","maxLength":255,"minLength":8},"title":{"type":"string","maxLength":255,"minLength":8},"userId":{"type":"string"}}},"requests.PutPostRequest":{"type":"object","properties":{"content":{"type":"string"},"description":{"type":"string"},"title":{"type":"string"}}},"responses.ErrorResponse":{"type":"object","properties":{"error_code":{"type":"integer"},"message":{"type":"string"}}},"responses.GetListPostResponseItem":{"type":"object","properties":{"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"}}},"responses.PostResponse":{"type":"object","properties":{"content":{"type":"string"},"createdAt":{"type":"string"},"description":{"type":"string"},"id":{"type":"string"},"title":{"type":"string"},"updatedAt":{"type":"string"},"userId":{"type":"string"}}}},"securityDefinitions":{"BasicAuth":{"type":"basic"}}}

View file

@ -21,6 +21,15 @@ definitions:
- title - title
- userId - userId
type: object type: object
requests.PutPostRequest:
properties:
content:
type: string
description:
type: string
title:
type: string
type: object
responses.ErrorResponse: responses.ErrorResponse:
properties: properties:
error_code: error_code:
@ -70,7 +79,7 @@ info:
paths: paths:
/images/{path}: /images/{path}:
get: get:
description: get image by path description: Creates new user in system
parameters: parameters:
- description: Path to image - description: Path to image
in: query in: query
@ -78,12 +87,11 @@ paths:
required: true required: true
type: string type: string
produces: produces:
- image/png - application/json
- image/jpeg
responses: responses:
"200": "200":
description: OK description: OK
summary: Get an image by path summary: Create new user
/post: /post:
get: get:
description: Return first 5 posts description: Return first 5 posts
@ -96,6 +104,10 @@ paths:
items: items:
$ref: '#/definitions/responses.GetListPostResponseItem' $ref: '#/definitions/responses.GetListPostResponseItem'
type: array type: array
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/responses.ErrorResponse'
summary: Get all posts summary: Get all posts
tags: tags:
- post - post
@ -121,15 +133,39 @@ paths:
description: Bad Request description: Bad Request
schema: schema:
$ref: '#/definitions/responses.ErrorResponse' $ref: '#/definitions/responses.ErrorResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/responses.ErrorResponse'
summary: Create new post summary: Create new post
tags: tags:
- post - post
/post/{id}: /post/{id}:
delete:
description: Delete post by id
parameters:
- description: Id of post
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
"400":
description: Bad Request
schema:
$ref: '#/definitions/responses.ErrorResponse'
summary: Delete post
tags:
- post
get: get:
description: get post by id description: get post by id
parameters: parameters:
- description: Id of post - description: Id of post
in: query in: path
name: id name: id
required: true required: true
type: string type: string
@ -142,6 +178,10 @@ paths:
items: items:
$ref: '#/definitions/responses.PostResponse' $ref: '#/definitions/responses.PostResponse'
type: array type: array
"400":
description: Bad Request
schema:
$ref: '#/definitions/responses.ErrorResponse'
summary: Get post by id summary: Get post by id
tags: tags:
- post - post
@ -149,10 +189,16 @@ paths:
description: update post content description: update post content
parameters: parameters:
- description: Id of post - description: Id of post
in: query in: path
name: id name: id
required: true required: true
type: string type: string
- description: Post data
in: body
name: request
required: true
schema:
$ref: '#/definitions/requests.PutPostRequest'
produces: produces:
- application/json - application/json
responses: responses:
@ -160,15 +206,19 @@ paths:
description: OK description: OK
schema: schema:
$ref: '#/definitions/responses.PostResponse' $ref: '#/definitions/responses.PostResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/responses.ErrorResponse'
summary: Update post content summary: Update post content
tags: tags:
- post - post
/post/{offset}: /post/offset/{offset}:
get: get:
description: return 5 posts after first offset posts description: return 5 posts after first offset posts
parameters: parameters:
- description: Offset of posts - description: Offset of posts
in: query in: path
name: offset name: offset
required: true required: true
type: integer type: integer
@ -181,6 +231,10 @@ paths:
items: items:
$ref: '#/definitions/responses.GetListPostResponseItem' $ref: '#/definitions/responses.GetListPostResponseItem'
type: array type: array
"400":
description: Bad Request
schema:
$ref: '#/definitions/responses.ErrorResponse'
summary: Get posts after offset summary: Get posts after offset
tags: tags:
- post - post

View file

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

View file

@ -7,6 +7,7 @@ import (
"58team_blog/internal/application/queries" "58team_blog/internal/application/queries"
"58team_blog/internal/domain/entities" "58team_blog/internal/domain/entities"
"58team_blog/internal/domain/repository" "58team_blog/internal/domain/repository"
"errors"
"fmt" "fmt"
"time" "time"
) )
@ -86,6 +87,22 @@ func (s *PostService) GetAll() (*queries.PostGetAllResult, error) {
return result, nil 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) { func (s *PostService) Update(cmd commands.UpdatePostCommand) (*common.PostResult, error) {
post, err := s.repo.FindById(cmd.Id) post, err := s.repo.FindById(cmd.Id)
if err != nil { if err != nil {

View file

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

View file

@ -3,6 +3,7 @@ package repo
import ( import (
"58team_blog/internal/domain/entities" "58team_blog/internal/domain/entities"
"58team_blog/internal/infrastructure/db" "58team_blog/internal/infrastructure/db"
"strconv"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@ -65,6 +66,15 @@ func (r *PostRepository) GetAll() ([]*entities.Post, error) {
return entity_list, err 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 { func (r *PostRepository) Update(entity *entities.Post) error {
query := "UPDATE " + entities.PostTable + "SET title=:title, description=:description, content=:content, updatedat=:updatedat WHERE id=:id" 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" "58team_blog/internal/interfaces/api/responses"
"log" "log"
"net/http" "net/http"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
@ -42,7 +43,7 @@ func (r *PostController) Post(c *gin.Context) {
if err := c.BindJSON(&request); err != nil { if err := c.BindJSON(&request); err != nil {
log.Println(err) log.Println(err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "BadRequest") resp := responses.CreateErrorResponse(http.StatusBadRequest, "BadRequest")
c.IndentedJSON(resp.ErrorCode, resp) c.JSON(resp.ErrorCode, resp)
return return
} }
@ -50,7 +51,7 @@ func (r *PostController) Post(c *gin.Context) {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Incorrect user id") resp := responses.CreateErrorResponse(http.StatusBadRequest, "Incorrect user id")
c.IndentedJSON(resp.ErrorCode, resp) c.JSON(resp.ErrorCode, resp)
return return
} }
@ -65,13 +66,13 @@ func (r *PostController) Post(c *gin.Context) {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
resp := responses.CreateErrorResponse(http.StatusInternalServerError, "Internal server error") resp := responses.CreateErrorResponse(http.StatusInternalServerError, "Internal server error")
c.IndentedJSON(resp.ErrorCode, resp) c.JSON(resp.ErrorCode, resp)
return return
} }
response := dto.ResponseFromPostResult(res) response := dto.ResponseFromPostResult(res)
c.IndentedJSON(http.StatusOK, response) c.JSON(http.StatusOK, response)
} }
// GetAllPost godoc // GetAllPost godoc
@ -88,7 +89,7 @@ func (r *PostController) GetAll(c *gin.Context) {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
resp := responses.CreateErrorResponse(http.StatusInternalServerError, "Internal server error") resp := responses.CreateErrorResponse(http.StatusInternalServerError, "Internal server error")
c.IndentedJSON(resp.ErrorCode, resp) c.JSON(resp.ErrorCode, resp)
return return
} }
@ -98,22 +99,44 @@ func (r *PostController) GetAll(c *gin.Context) {
} }
// GetAllWithOffsetPost godoc // GetAllWithOffsetPost godoc
//
// @Summary Get posts after offset // @Summary Get posts after offset
// @Description return 5 posts after first offset posts // @Description return 5 posts after first offset posts
// @Tags post // @Tags post
// @Param offset query int true "Offset of posts" // @Param offset path int true "Offset of posts"
// @Produce json // @Produce json
// @Success 200 {array} responses.GetListPostResponseItem // @Success 200 {array} responses.GetListPostResponseItem
// @Router /post/{offset} [get] // @Failure 400 {object} responses.ErrorResponse
// @Router /post/offset/{offset} [get]
func (r *PostController) GetAllWithOffset(c *gin.Context) { 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 // GetByIdPost godoc
//
// @Summary Get post by id // @Summary Get post by id
// @Description get post by id // @Description get post by id
// @Tags post // @Tags post
// @Param id query string true "Id of post" // @Param id path string true "Id of post"
// @Produce json // @Produce json
// @Success 200 {array} responses.PostResponse // @Success 200 {array} responses.PostResponse
// @Failure 400 {object} responses.ErrorResponse // @Failure 400 {object} responses.ErrorResponse
@ -147,10 +170,11 @@ func (r *PostController) GetById(c *gin.Context) {
} }
// PutPost godoc // PutPost godoc
//
// @Summary Update post content // @Summary Update post content
// @Description update post content // @Description update post content
// @Tags post // @Tags post
// @Param id query string true "Id of post" // @Param id path string true "Id of post"
// //
// @Param request body requests.PutPostRequest true "Post data" // @Param request body requests.PutPostRequest true "Post data"
// //
@ -161,22 +185,71 @@ func (r *PostController) GetById(c *gin.Context) {
func (r *PostController) Put(c *gin.Context) { func (r *PostController) Put(c *gin.Context) {
var request requests.PutPostRequest 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 { if err := c.BindJSON(request); err != nil {
log.Println("Post request error: ", err) log.Println("Post request error: ", err)
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request") resp := responses.CreateErrorResponse(http.StatusBadRequest, "Bad request")
c.JSON(resp.ErrorCode, resp) 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 // Delete godoc
//
// @Summary Delete post // @Summary Delete post
// @Description Delete post by id // @Description Delete post by id
// @Tags post // @Tags post
// @Param id query string true "Id of post" // @Param id path string true "Id of post"
// @Produce json // @Produce json
// @Success 200 // @Success 200
// @Failure 400 {object} responses.ErrorResponse
// @Router /post/{id} [delete] // @Router /post/{id} [delete]
func (r *PostController) Delete(c *gin.Context) { 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,7 +19,7 @@ func CreateUserController(service *services.UserService) UserController {
// @Summary Create new user // @Summary Create new user
// @Description Creates new user in system // @Description Creates new user in system
// @Param path query string true "Path to image" // @Param path query string true "Path to image"
// @Produce // @Produce json
// @Success 200 // @Success 200
// @Router /images/{path} [get] // @Router /images/{path} [get]
func (r *UserController) Post(c *gin.Context) { func (r *UserController) Post(c *gin.Context) {

View file

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

BIN
migrate

Binary file not shown.