narasiahli-be/app/module/article_comments/controller/article_comments.controller.go

241 lines
8.0 KiB
Go
Raw Normal View History

2025-09-19 04:08:42 +00:00
package controller
import (
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog"
"narasi-ahli-be/app/module/article_comments/request"
"narasi-ahli-be/app/module/article_comments/service"
"narasi-ahli-be/utils/paginator"
utilRes "narasi-ahli-be/utils/response"
utilVal "narasi-ahli-be/utils/validator"
"strconv"
)
type articleCommentsController struct {
articleCommentsService service.ArticleCommentsService
Log zerolog.Logger
}
type ArticleCommentsController interface {
All(c *fiber.Ctx) error
Show(c *fiber.Ctx) error
Save(c *fiber.Ctx) error
Update(c *fiber.Ctx) error
Delete(c *fiber.Ctx) error
Approval(c *fiber.Ctx) error
}
func NewArticleCommentsController(articleCommentsService service.ArticleCommentsService, log zerolog.Logger) ArticleCommentsController {
return &articleCommentsController{
articleCommentsService: articleCommentsService,
Log: log,
}
}
// All get all ArticleComments
// @Summary Get all ArticleComments
// @Description API for getting all ArticleComments
// @Tags ArticleComments
// @Security Bearer
// @Param X-Client-Key header string false "Insert the X-Client-Key"
// @Param req query request.ArticleCommentsQueryRequest false "query parameters"
// @Param req query paginator.Pagination false "pagination parameters"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError
// @Failure 500 {object} response.InternalServerError
// @Router /article-comments [get]
func (_i *articleCommentsController) All(c *fiber.Ctx) error {
// Get from context
paginate, err := paginator.Paginate(c)
if err != nil {
return err
}
reqContext := request.ArticleCommentsQueryRequestContext{
Message: c.Query("message"),
ArticleId: c.Query("articleId"),
CommentFrom: c.Query("commentFrom"),
ParentId: c.Query("parentId"),
IsPublic: c.Query("isPublic"),
}
req := reqContext.ToParamRequest()
req.Pagination = paginate
articleCommentsData, paging, err := _i.articleCommentsService.All(req)
if err != nil {
return err
}
return utilRes.Resp(c, utilRes.Response{
Success: true,
Messages: utilRes.Messages{"ArticleComments list successfully retrieved"},
Data: articleCommentsData,
Meta: paging,
})
}
// Show get one ArticleComments
// @Summary Get one ArticleComments
// @Description API for getting one ArticleComments
// @Tags ArticleComments
// @Security Bearer
// @Param X-Client-Key header string false "Insert the X-Client-Key"
// @Param id path int true "ArticleComments ID"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError
// @Failure 500 {object} response.InternalServerError
// @Router /article-comments/{id} [get]
func (_i *articleCommentsController) Show(c *fiber.Ctx) error {
// Get from context
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
if err != nil {
return err
}
articleCommentsData, err := _i.articleCommentsService.Show(uint(id))
if err != nil {
return err
}
return utilRes.Resp(c, utilRes.Response{
Success: true,
Messages: utilRes.Messages{"ArticleComments successfully retrieved"},
Data: articleCommentsData,
})
}
// Save create ArticleComments
// @Summary Create ArticleComments
// @Description API for create ArticleComments
// @Tags ArticleComments
// @Security Bearer
// @Param X-Client-Key header string false "Insert the X-Client-Key"
// @Param X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param payload body request.ArticleCommentsCreateRequest true "Required payload"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError
// @Failure 500 {object} response.InternalServerError
// @Router /article-comments [post]
func (_i *articleCommentsController) Save(c *fiber.Ctx) error {
// Get from context
req := new(request.ArticleCommentsCreateRequest)
if err := utilVal.ParseAndValidate(c, req); err != nil {
return err
}
authToken := c.Get("Authorization")
dataResult, err := _i.articleCommentsService.Save(*req, authToken)
if err != nil {
return err
}
return utilRes.Resp(c, utilRes.Response{
Success: true,
Messages: utilRes.Messages{"ArticleComments successfully created"},
Data: dataResult,
})
}
// Update update ArticleComments
// @Summary update ArticleComments
// @Description API for update ArticleComments
// @Tags ArticleComments
// @Security Bearer
// @Param X-Client-Key header string false "Insert the X-Client-Key"
// @Param X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param payload body request.ArticleCommentsUpdateRequest true "Required payload"
// @Param id path int true "ArticleComments ID"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError
// @Failure 500 {object} response.InternalServerError
// @Router /article-comments/{id} [put]
func (_i *articleCommentsController) Update(c *fiber.Ctx) error {
// Get from context
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
if err != nil {
return err
}
req := new(request.ArticleCommentsUpdateRequest)
if err := utilVal.ParseAndValidate(c, req); err != nil {
return err
}
err = _i.articleCommentsService.Update(uint(id), *req)
if err != nil {
return err
}
return utilRes.Resp(c, utilRes.Response{
Success: true,
Messages: utilRes.Messages{"ArticleComments successfully updated"},
})
}
// Delete delete ArticleComments
// @Summary delete ArticleComments
// @Description API for delete ArticleComments
// @Tags ArticleComments
// @Security Bearer
// @Param X-Client-Key header string false "Insert the X-Client-Key"
// @Param X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param id path int true "ArticleComments ID"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError
// @Failure 500 {object} response.InternalServerError
// @Router /article-comments/{id} [delete]
func (_i *articleCommentsController) Delete(c *fiber.Ctx) error {
// Get from context
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
if err != nil {
return err
}
err = _i.articleCommentsService.Delete(uint(id))
if err != nil {
return err
}
return utilRes.Resp(c, utilRes.Response{
Success: true,
Messages: utilRes.Messages{"ArticleComments successfully deleted"},
})
}
// Approval ArticleComments
// @Summary Approval ArticleComments
// @Description API for Approval ArticleComments
// @Tags ArticleComments
// @Security Bearer
// @Param X-Client-Key header string false "Insert the X-Client-Key"
// @Param X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param payload body request.ArticleCommentsApprovalRequest true "Required payload"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError
// @Failure 500 {object} response.InternalServerError
// @Router /article-comments/approval [post]
func (_i *articleCommentsController) Approval(c *fiber.Ctx) error {
// Get from context
req := new(request.ArticleCommentsApprovalRequest)
if err := utilVal.ParseAndValidate(c, req); err != nil {
return err
}
err := _i.articleCommentsService.Approval(req.ID, *req)
if err != nil {
return err
}
return utilRes.Resp(c, utilRes.Response{
Success: true,
Messages: utilRes.Messages{"ArticleComments successfully reviewed"},
})
}