Feat(BE-36,37,38,39): finish master data management api

This commit is contained in:
Hafizh A. Y
2025-10-03 21:04:21 +07:00
parent e8905be856
commit 2d49ffe4cd
103 changed files with 6974 additions and 117 deletions
@@ -0,0 +1,140 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type SupplierController struct {
SupplierService service.SupplierService
}
func NewSupplierController(supplierService service.SupplierService) *SupplierController {
return &SupplierController{
SupplierService: supplierService,
}
}
func (u *SupplierController) GetAll(c *fiber.Ctx) error {
query := &validation.Query{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
}
result, totalResults, err := u.SupplierService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.SupplierListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all suppliers successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToSupplierListDTOs(result),
})
}
func (u *SupplierController) GetOne(c *fiber.Ctx) error {
param := c.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
result, err := u.SupplierService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get supplier successfully",
Data: dto.ToSupplierListDTO(*result),
})
}
func (u *SupplierController) CreateOne(c *fiber.Ctx) error {
req := new(validation.Create)
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
result, err := u.SupplierService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create supplier successfully",
Data: dto.ToSupplierListDTO(*result),
})
}
func (u *SupplierController) UpdateOne(c *fiber.Ctx) error {
req := new(validation.Update)
param := c.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
result, err := u.SupplierService.UpdateOne(c, req, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Update supplier successfully",
Data: dto.ToSupplierListDTO(*result),
})
}
func (u *SupplierController) DeleteOne(c *fiber.Ctx) error {
param := c.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
if err := u.SupplierService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete supplier successfully",
})
}
@@ -0,0 +1,88 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type SupplierBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Alias string `json:"alias"`
Category string `json:"category"`
}
type SupplierListDTO struct {
SupplierBaseDTO
Pic string `json:"pic"`
Type string `json:"type"`
Hatchery *string `json:"hatchery,omitempty"`
Phone string `json:"phone"`
Email string `json:"email"`
Address string `json:"address"`
Npwp *string `json:"npwp,omitempty"`
AccountNumber *string `json:"account_number,omitempty"`
Balance float64 `json:"balance"`
DueDate int `json:"due_date"`
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type SupplierDetailDTO struct {
SupplierListDTO
}
// === Mapper Functions ===
func ToSupplierBaseDTO(e entity.Supplier) SupplierBaseDTO {
return SupplierBaseDTO{
Id: e.Id,
Name: e.Name,
Alias: e.Alias,
Category: e.Category,
}
}
func ToSupplierListDTO(e entity.Supplier) SupplierListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return SupplierListDTO{
Pic: e.Pic,
Type: e.Type,
Hatchery: e.Hatchery,
Phone: e.Phone,
Email: e.Email,
Address: e.Address,
Npwp: e.Npwp,
AccountNumber: e.AccountNumber,
Balance: e.Balance,
DueDate: e.DueDate,
SupplierBaseDTO: ToSupplierBaseDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
func ToSupplierListDTOs(e []entity.Supplier) []SupplierListDTO {
result := make([]SupplierListDTO, len(e))
for i, r := range e {
result[i] = ToSupplierListDTO(r)
}
return result
}
func ToSupplierDetailDTO(e entity.Supplier) SupplierDetailDTO {
return SupplierDetailDTO{
SupplierListDTO: ToSupplierListDTO(e),
}
}
@@ -0,0 +1,26 @@
package suppliers
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rSupplier "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/repositories"
sSupplier "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type SupplierModule struct{}
func (SupplierModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
supplierRepo := rSupplier.NewSupplierRepository(db)
userRepo := rUser.NewUserRepository(db)
supplierService := sSupplier.NewSupplierService(supplierRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
SupplierRoutes(router, userService, supplierService)
}
@@ -0,0 +1,30 @@
package repository
import (
"context"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gorm.io/gorm"
)
type SupplierRepository interface {
repository.BaseRepository[entity.Supplier]
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type SupplierRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Supplier]
db *gorm.DB
}
func NewSupplierRepository(db *gorm.DB) SupplierRepository {
return &SupplierRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Supplier](db),
db: db,
}
}
func (r *SupplierRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Supplier](ctx, r.db, name, excludeID)
}
@@ -0,0 +1,28 @@
package suppliers
import (
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/controllers"
supplier "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func SupplierRoutes(v1 fiber.Router, u user.UserService, s supplier.SupplierService) {
ctrl := controller.NewSupplierController(s)
route := v1.Group("/suppliers")
// route.Get("/", m.Auth(u), ctrl.GetAll)
// route.Post("/", m.Auth(u), ctrl.CreateOne)
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
route.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
route.Get("/:id", ctrl.GetOne)
route.Patch("/:id", ctrl.UpdateOne)
route.Delete("/:id", ctrl.DeleteOne)
}
@@ -0,0 +1,221 @@
package service
import (
"errors"
"fmt"
"strings"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type SupplierService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Supplier, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Supplier, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Supplier, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Supplier, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type supplierService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.SupplierRepository
}
func NewSupplierService(repo repository.SupplierRepository, validate *validator.Validate) SupplierService {
return &supplierService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s supplierService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser")
}
func (s supplierService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Supplier, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
suppliers, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
if params.Search != "" {
return db.Where("name LIKE ?", "%"+params.Search+"%")
}
return db.Order("created_at DESC").Order("updated_at DESC")
})
if err != nil {
s.Log.Errorf("Failed to get suppliers: %+v", err)
return nil, 0, err
}
return suppliers, total, nil
}
func (s supplierService) GetOne(c *fiber.Ctx, id uint) (*entity.Supplier, error) {
supplier, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Supplier not found")
}
if err != nil {
s.Log.Errorf("Failed get supplier by id: %+v", err)
return nil, err
}
return supplier, nil
}
func (s *supplierService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Supplier, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
if exists, err := s.Repository.NameExists(c.Context(), req.Name, nil); err != nil {
s.Log.Errorf("Failed to check supplier name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check supplier name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Supplier with name %s already exists", req.Name))
}
typ := strings.ToUpper(req.Type)
if !utils.IsValidCustomerSupplierType(typ) {
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid supplier type")
}
category := strings.ToUpper(req.Category)
if !utils.IsValidSupplierCategory(category) {
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid supplier category")
}
alias := strings.TrimSpace(strings.ToUpper(req.Alias))
//TODO: created by dummy
createBody := &entity.Supplier{
Name: req.Name,
Alias: alias,
Pic: req.Pic,
Type: typ,
Category: category,
Hatchery: req.Hatchery,
Phone: req.Phone,
Email: req.Email,
Address: req.Address,
Npwp: req.Npwp,
AccountNumber: req.AccountNumber,
DueDate: req.DueDate,
CreatedBy: 1,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create supplier: %+v", err)
return nil, err
}
return s.GetOne(c, createBody.Id)
}
func (s supplierService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Supplier, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
updateBody := make(map[string]any)
if req.Name != nil {
if exists, err := s.Repository.NameExists(c.Context(), *req.Name, &id); err != nil {
s.Log.Errorf("Failed to check supplier name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check supplier name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Supplier with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
if req.Alias != nil {
updateBody["alias"] = strings.TrimSpace(strings.ToUpper(*req.Alias))
}
if req.Pic != nil {
updateBody["pic"] = *req.Pic
}
if req.Type != nil {
typ := strings.ToUpper(*req.Type)
if !utils.IsValidCustomerSupplierType(typ) {
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid supplier type")
}
updateBody["type"] = typ
}
if req.Category != nil {
category := strings.ToUpper(*req.Category)
if !utils.IsValidSupplierCategory(category) {
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid supplier category")
}
updateBody["category"] = category
}
if req.Hatchery != nil {
updateBody["hatchery"] = *req.Hatchery
}
if req.Phone != nil {
updateBody["phone"] = *req.Phone
}
if req.Email != nil {
updateBody["email"] = *req.Email
}
if req.Address != nil {
updateBody["address"] = *req.Address
}
if req.Npwp != nil {
updateBody["npwp"] = *req.Npwp
}
if req.AccountNumber != nil {
updateBody["account_number"] = *req.AccountNumber
}
if req.DueDate != nil {
updateBody["due_date"] = *req.DueDate
}
if len(updateBody) == 0 {
return s.GetOne(c, id)
}
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Supplier not found")
}
s.Log.Errorf("Failed to update supplier: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s supplierService) DeleteOne(c *fiber.Ctx, id uint) error {
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Supplier not found")
}
s.Log.Errorf("Failed to delete supplier: %+v", err)
return err
}
return nil
}
@@ -0,0 +1,37 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
Alias string `json:"alias" validate:"required_strict,max=5"`
Pic string `json:"pic" validate:"required_strict"`
Type string `json:"type" validate:"required_strict"`
Category string `json:"category" validate:"required_strict"`
Hatchery *string `json:"hatchery,omitempty" validate:"omitempty"`
Phone string `json:"phone" validate:"required_strict,max=20"`
Email string `json:"email" validate:"required_strict,email"`
Address string `json:"address" validate:"required_strict"`
Npwp *string `json:"npwp,omitempty" validate:"omitempty,max=50"`
AccountNumber *string `json:"account_number,omitempty" validate:"omitempty,max=50"`
DueDate int `json:"due_date" validate:"required_strict,number,gt=0"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty,min=3"`
Alias *string `json:"alias,omitempty" validate:"omitempty,max=5"`
Pic *string `json:"pic,omitempty" validate:"omitempty"`
Type *string `json:"type,omitempty" validate:"omitempty"`
Category *string `json:"category,omitempty" validate:"omitempty"`
Hatchery *string `json:"hatchery,omitempty" validate:"omitempty"`
Phone *string `json:"phone,omitempty" validate:"omitempty,max=20"`
Email *string `json:"email,omitempty" validate:"omitempty,email"`
Address *string `json:"address,omitempty" validate:"omitempty"`
Npwp *string `json:"npwp,omitempty" validate:"omitempty,max=50"`
AccountNumber *string `json:"account_number,omitempty" validate:"omitempty,max=50"`
DueDate *int `json:"due_date,omitempty" validate:"omitempty,number,gt=0"`
}
type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
Search string `query:"search" validate:"omitempty,max=50"`
}