300 lines
7.6 KiB
Go
300 lines
7.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"58team_blog/internal/application/commands"
|
|
"58team_blog/internal/application/queries"
|
|
"58team_blog/internal/application/services"
|
|
"58team_blog/internal/interfaces/api/mapper"
|
|
"58team_blog/internal/interfaces/api/requests"
|
|
"58team_blog/internal/interfaces/api/responses"
|
|
"58team_blog/internal/utils"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type PostController struct {
|
|
service *services.PostService
|
|
userService *services.UserService
|
|
}
|
|
|
|
func CreatePostController(service *services.PostService, userService *services.UserService) PostController {
|
|
return PostController{
|
|
service: service,
|
|
userService: userService,
|
|
}
|
|
}
|
|
|
|
// PostPost godoc
|
|
//
|
|
// @Summary Create new post
|
|
// @Description Create new post in blog
|
|
// @Tags post
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body requests.CreatePostRequest true "Post data"
|
|
// @Success 201 {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
|
|
|
|
if err := c.BindJSON(&request); err != nil {
|
|
log.Println(err)
|
|
resp := responses.CreateErrorResponse(http.StatusBadRequest, err.Error())
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
userId, err := uuid.Parse(request.UserId)
|
|
if err != nil {
|
|
log.Println(err)
|
|
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Incorrect user id")
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
cmd := commands.CreatePostCommand{
|
|
UserId: userId,
|
|
Title: request.Title,
|
|
Description: request.Description,
|
|
Content: request.Content,
|
|
Category: request.Category,
|
|
Tags: request.Tags,
|
|
}
|
|
|
|
res, err := r.service.Create(cmd)
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
// Get username by userid
|
|
user, err := r.userService.FindById(queries.UserFindByIdQuery{Id: userId})
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
response := mapper.ResponseFromPostResult(res)
|
|
response.Username = user.Result.UserName
|
|
|
|
c.JSON(http.StatusCreated, response)
|
|
}
|
|
|
|
// GetAllPost godoc
|
|
//
|
|
// @Summary Get all posts
|
|
// @Description Return first 5 posts
|
|
// @Tags post
|
|
// @Produce json
|
|
// @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 {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
res := mapper.ResponseFromPostGetAllResult(result)
|
|
|
|
for e, i := range res {
|
|
// Get username by userid
|
|
user, err := r.userService.FindById(queries.UserFindByIdQuery{Id: uuid.MustParse(i.UserId)})
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
res[e].Username = user.Result.UserName
|
|
}
|
|
|
|
c.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// GetAllWithOffsetPost godoc
|
|
//
|
|
// @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
|
|
// @Failure 500 {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, "Invalid offset value")
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
result, err := r.service.GetAllOffset(offset)
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
res := mapper.ResponseFromPostGetAllResult(result)
|
|
|
|
for e, i := range res {
|
|
// Get username by userid
|
|
user, err := r.userService.FindById(queries.UserFindByIdQuery{Id: uuid.MustParse(i.UserId)})
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
res[e].Username = user.Result.UserName
|
|
}
|
|
|
|
c.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// GetByIdPost godoc
|
|
//
|
|
// @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
|
|
// @Failure 404 {object} responses.ErrorResponse
|
|
// @Failure 500 {object} responses.ErrorResponse
|
|
// @Router /post/{id} [get]
|
|
func (r *PostController) GetById(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
id_valid, err := uuid.Parse(id)
|
|
if err != nil {
|
|
log.Println("User get by id error: ", err)
|
|
resp := responses.CreateErrorResponse(http.StatusBadRequest, "Invalid user id")
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
query := queries.PostFindByIdQuery{
|
|
Id: id_valid,
|
|
}
|
|
|
|
posts, err := r.service.FindById(query)
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
result := mapper.ResponseFormPostFindByIdResult(posts)
|
|
|
|
user, err := r.userService.FindById(queries.UserFindByIdQuery{Id: uuid.MustParse(result.UserId)})
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
result.Username = user.Result.UserName
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// PutPost godoc
|
|
//
|
|
// @Summary Update post content
|
|
// @Description update post content
|
|
// @Tags post
|
|
// @Param id path string true "Id of post"
|
|
//
|
|
// @Param request body requests.PutPostRequest true "Post data"
|
|
//
|
|
// @Produce json
|
|
// @Success 200 {object} responses.PostResponse
|
|
// @Failure 400 {object} responses.ErrorResponse
|
|
// @Failure 404 {object} responses.ErrorResponse
|
|
// @Failure 500 {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, err.Error())
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
if err := c.BindJSON(request); err != nil {
|
|
log.Println("Post request error: ", err)
|
|
resp := responses.CreateErrorResponse(http.StatusBadRequest, err.Error())
|
|
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 {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
response := mapper.ResponseFromPostResult(post)
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// Delete godoc
|
|
//
|
|
// @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
|
|
// @Failure 404 {object} responses.ErrorResponse
|
|
// @Failure 500 {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, err.Error())
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
cmd := commands.DeletePostCommand{
|
|
Id: id_valid,
|
|
}
|
|
err = r.service.Delete(cmd)
|
|
if err != nil {
|
|
resp := utils.HandleError(err)
|
|
c.JSON(resp.ErrorCode, resp)
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|