150 lines
4.4 KiB
Go
150 lines
4.4 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/Caknoooo/go-pagination"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/do"
|
|
"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)
|
|
}
|
|
|
|
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) {
|
|
// clientId := ctx.MustGet("client_id").(string)
|
|
var filter = &query.ClientFilter{
|
|
Name: ctx.Query("name"),
|
|
PIC: ctx.Query("pic"),
|
|
Phone: ctx.Query("phone"),
|
|
Email: ctx.Query("email"),
|
|
Address: ctx.Query("address"),
|
|
// ClientID: clientId,
|
|
Includes: ctx.QueryArray("includes"),
|
|
}
|
|
filter.BindPagination(ctx)
|
|
ctx.ShouldBindQuery(filter)
|
|
groups, total, err := pagination.PaginatedQueryWithIncludableAndOptions[query.M_Client](
|
|
c.db,
|
|
filter,
|
|
pagination.PaginatedQueryOptions{
|
|
EnableSoftDelete: true,
|
|
},
|
|
)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CLIENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
paginationResponse := pagination.CalculatePagination(filter.Pagination, total)
|
|
|
|
response := pagination.NewPaginatedResponse(http.StatusOK, dto.MESSAGE_SUCCESS_GET_CLIENT, groups, paginationResponse)
|
|
ctx.JSON(http.StatusOK, response)
|
|
}
|