Added authorization

This commit is contained in:
KamilM1205 2025-09-25 09:01:00 +04:00
parent c3c3d65d32
commit b96dd39795
50 changed files with 685 additions and 410 deletions

View file

@ -17,12 +17,14 @@ import (
)
type PostController struct {
service *services.PostService
service *services.PostService
userService *services.UserService
}
func CreatePostController(service *services.PostService) PostController {
func CreatePostController(service *services.PostService, userService *services.UserService) PostController {
return PostController{
service: service,
service: service,
userService: userService,
}
}
@ -61,6 +63,8 @@ func (r *PostController) Post(c *gin.Context) {
Title: request.Title,
Description: request.Description,
Content: request.Content,
Category: request.Category,
Tags: request.Tags,
}
res, err := r.service.Create(cmd)
@ -70,7 +74,16 @@ func (r *PostController) Post(c *gin.Context) {
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)
}
@ -94,6 +107,17 @@ func (r *PostController) GetAll(c *gin.Context) {
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)
}
@ -127,6 +151,17 @@ func (r *PostController) GetAllWithOffset(c *gin.Context) {
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)
}
@ -166,6 +201,14 @@ func (r *PostController) GetById(c *gin.Context) {
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)
}