188 lines
6.0 KiB
Go
188 lines
6.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/client/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/client/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/client/service"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/do"
|
|
|
|
// "github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type (
|
|
ClientController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
AssignMenusToClient(ctx *gin.Context)
|
|
RemoveMenusFromClient(ctx *gin.Context)
|
|
}
|
|
|
|
clientController struct {
|
|
clientService service.ClientService
|
|
db *gorm.DB
|
|
}
|
|
)
|
|
|
|
func NewClientController(i *do.Injector, clientService service.ClientService) ClientController {
|
|
db := do.MustInvokeNamed[*gorm.DB](i, constants.DB)
|
|
return &clientController{
|
|
clientService: clientService,
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (c *clientController) Create(ctx *gin.Context) {
|
|
var req dto.ClientCreateRequest
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
// Ambil file logo (opsional)
|
|
file, _, err := ctx.Request.FormFile("logo")
|
|
if err == nil && file != nil {
|
|
fileBytes, _ := io.ReadAll(file)
|
|
base64Str := base64.StdEncoding.EncodeToString(fileBytes)
|
|
req.Logo = base64Str
|
|
}
|
|
|
|
created, err := c.clientService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_CLIENT, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *clientController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.ClientUpdateRequest
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
// Ambil file logo (opsional, untuk update)
|
|
file, _, err := ctx.Request.FormFile("logo")
|
|
if err == nil && file != nil {
|
|
fileBytes, _ := io.ReadAll(file)
|
|
base64Str := base64.StdEncoding.EncodeToString(fileBytes)
|
|
req.Logo = &base64Str
|
|
}
|
|
|
|
client, err := c.clientService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_CLIENT, client)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *clientController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.clientService.Delete(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_CLIENT, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *clientController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
client, err := c.clientService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusNotFound, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_CLIENT, client)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
func (c *clientController) GetAll(ctx *gin.Context) {
|
|
// Ambil filter dari query param
|
|
var filter query.ClientFilter
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
// Ambil limit & offset dari query param (default: limit=10, offset=0)
|
|
perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10"))
|
|
page := utils.ParseInt(ctx.DefaultQuery("page", "1"))
|
|
filter.PerPage = perPage
|
|
filter.Page = (page - 1) * perPage
|
|
|
|
clients, total, err := c.clientService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
paginationResponse := utils.BuildPaginationResponse(perPage, page, total)
|
|
response := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_CLIENT, clients, paginationResponse)
|
|
ctx.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// AssignMenusToClient implements MenuController.
|
|
func (c *clientController) AssignMenusToClient(ctx *gin.Context) {
|
|
clientId := ctx.Param("id")
|
|
var req dto.AssignMenusToClientRequest
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
if err := c.clientService.AssignMenusToClient(ctx.Request.Context(), clientId, req.MenuIds); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_ASSIGN_MENUS_TO_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
resp := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_ASSIGN_MENUS_TO_CLIENT, nil)
|
|
ctx.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
// RemoveMenusFromClient implements MenuController.
|
|
func (c *clientController) RemoveMenusFromClient(ctx *gin.Context) {
|
|
clientId := ctx.Param("id")
|
|
var req dto.RemoveMenusFromClientRequest
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
if err := c.clientService.RemoveMenusFromClient(ctx.Request.Context(), clientId, req.MenuIds); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_REMOVE_MENUS_FROM_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
resp := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_REMOVE_MENUS_FROM_CLIENT, nil)
|
|
ctx.JSON(http.StatusOK, resp)
|
|
}
|