Feat(BE-36,37,38,39): master area, customer, kandang, location, warehouse

This commit is contained in:
Hafizh A. Y
2025-10-02 10:51:15 +07:00
parent dbc1f79a36
commit e8905be856
79 changed files with 3745 additions and 169 deletions
@@ -0,0 +1,140 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type AreaController struct {
AreaService service.AreaService
}
func NewAreaController(areaService service.AreaService) *AreaController {
return &AreaController{
AreaService: areaService,
}
}
func (u *AreaController) 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.AreaService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.AreaListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all areas successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToAreaListDTOs(result),
})
}
func (u *AreaController) 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.AreaService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get area successfully",
Data: dto.ToAreaListDTO(*result),
})
}
func (u *AreaController) 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.AreaService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create area successfully",
Data: dto.ToAreaListDTO(*result),
})
}
func (u *AreaController) 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.AreaService.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 area successfully",
Data: dto.ToAreaListDTO(*result),
})
}
func (u *AreaController) 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.AreaService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete area successfully",
})
}
@@ -0,0 +1,58 @@
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 AreaBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type AreaListDTO struct {
AreaBaseDTO
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type AreaDetailDTO struct {
AreaListDTO
}
// === Mapper Functions ===
func ToAreaBaseDTO(e entity.Area) AreaBaseDTO {
return AreaBaseDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToAreaListDTO(e entity.Area) AreaListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return AreaListDTO{
AreaBaseDTO: ToAreaBaseDTO(e),
CreatedUser: createdUser,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
}
}
func ToAreaListDTOs(e []entity.Area) []AreaListDTO {
result := make([]AreaListDTO, len(e))
for i, r := range e {
result[i] = ToAreaListDTO(r)
}
return result
}
+26
View File
@@ -0,0 +1,26 @@
package areas
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rArea "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/repositories"
sArea "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type AreaModule struct{}
func (AreaModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
areaRepo := rArea.NewAreaRepository(db)
userRepo := rUser.NewUserRepository(db)
areaService := sArea.NewAreaService(areaRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
AreaRoutes(router, userService, areaService)
}
@@ -0,0 +1,31 @@
package repository
import (
"context"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
"gorm.io/gorm"
)
type AreaRepository interface {
repository.BaseRepository[entity.Area]
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type AreaRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Area]
db *gorm.DB
}
func NewAreaRepository(db *gorm.DB) AreaRepository {
return &AreaRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Area](db),
db: db,
}
}
func (r *AreaRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Area](ctx, r.db, name, excludeID)
}
+28
View File
@@ -0,0 +1,28 @@
package areas
import (
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/controllers"
area "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func AreaRoutes(v1 fiber.Router, u user.UserService, s area.AreaService) {
ctrl := controller.NewAreaController(s)
route := v1.Group("/areas")
// 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,145 @@
package service
import (
"errors"
"fmt"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/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 AreaService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Area, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Area, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Area, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Area, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type areaService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.AreaRepository
}
func NewAreaService(repo repository.AreaRepository, validate *validator.Validate) AreaService {
return &areaService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s areaService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser")
}
func (s areaService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Area, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
areas, 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 areas: %+v", err)
return nil, 0, err
}
return areas, total, nil
}
func (s areaService) GetOne(c *fiber.Ctx, id uint) (*entity.Area, error) {
area, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Area not found")
}
if err != nil {
s.Log.Errorf("Failed get area by id: %+v", err)
return nil, err
}
return area, nil
}
func (s *areaService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Area, 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 area name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check area name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Area with name %s already exists", req.Name))
}
//TODO: created by dummy
createBody := &entity.Area{
Name: req.Name,
CreatedBy: 1,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create area: %+v", err)
return nil, err
}
return s.Repository.GetByID(c.Context(), createBody.Id, s.withRelations)
}
func (s areaService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Area, 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 area name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check area name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Area with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
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, "Area not found")
}
s.Log.Errorf("Failed to update area: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s areaService) 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, "Area not found")
}
s.Log.Errorf("Failed to delete area: %+v", err)
return err
}
return nil
}
@@ -0,0 +1,15 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
}
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"`
}
@@ -0,0 +1,140 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type CustomerController struct {
CustomerService service.CustomerService
}
func NewCustomerController(customerService service.CustomerService) *CustomerController {
return &CustomerController{
CustomerService: customerService,
}
}
func (u *CustomerController) 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.CustomerService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.CustomerListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all customers successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToCustomerListDTOs(result),
})
}
func (u *CustomerController) 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.CustomerService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get customer successfully",
Data: dto.ToCustomerListDTO(*result),
})
}
func (u *CustomerController) 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.CustomerService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create customer successfully",
Data: dto.ToCustomerListDTO(*result),
})
}
func (u *CustomerController) 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.CustomerService.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 customer successfully",
Data: dto.ToCustomerListDTO(*result),
})
}
func (u *CustomerController) 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.CustomerService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete customer successfully",
})
}
@@ -0,0 +1,79 @@
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 CustomerBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
PicId uint `json:"pic_id"`
Type string `json:"type"`
Address string `json:"address"`
Phone string `json:"phone"`
Email string `json:"email"`
AccountNumber string `json:"account_number"`
Pic *userDTO.UserBaseDTO `json:"pic"`
}
type CustomerListDTO struct {
CustomerBaseDTO
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type CustomerDetailDTO struct {
CustomerListDTO
}
// === Mapper Functions ===
func ToCustomerBaseDTO(e entity.Customer) CustomerBaseDTO {
var pic *userDTO.UserBaseDTO
if e.Pic.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.Pic)
pic = &mapped
}
return CustomerBaseDTO{
Id: e.Id,
Name: e.Name,
PicId: e.PicId,
Type: e.Type,
Address: e.Address,
Phone: e.Phone,
Email: e.Email,
AccountNumber: e.AccountNumber,
Pic: pic,
}
}
func ToCustomerListDTO(e entity.Customer) CustomerListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return CustomerListDTO{
CustomerBaseDTO: ToCustomerBaseDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
func ToCustomerListDTOs(e []entity.Customer) []CustomerListDTO {
result := make([]CustomerListDTO, len(e))
for i, r := range e {
result[i] = ToCustomerListDTO(r)
}
return result
}
@@ -0,0 +1,26 @@
package customers
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rCustomer "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
sCustomer "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type CustomerModule struct{}
func (CustomerModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
customerRepo := rCustomer.NewCustomerRepository(db)
userRepo := rUser.NewUserRepository(db)
customerService := sCustomer.NewCustomerService(customerRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
CustomerRoutes(router, userService, customerService)
}
@@ -0,0 +1,35 @@
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 CustomerRepository interface {
repository.BaseRepository[entity.Customer]
PicExists(ctx context.Context, areaId uint) (bool, error)
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type CustomerRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Customer]
db *gorm.DB
}
func NewCustomerRepository(db *gorm.DB) CustomerRepository {
return &CustomerRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Customer](db),
db: db,
}
}
func (r *CustomerRepositoryImpl) PicExists(ctx context.Context, picId uint) (bool, error) {
return repository.Exists[entity.User](ctx, r.db, picId)
}
func (r *CustomerRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Customer](ctx, r.db, name, excludeID)
}
@@ -0,0 +1,28 @@
package customers
import (
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/controllers"
customer "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func CustomerRoutes(v1 fiber.Router, u user.UserService, s customer.CustomerService) {
ctrl := controller.NewCustomerController(s)
route := v1.Group("/customers")
// 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,179 @@
package service
import (
"errors"
"fmt"
"strings"
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/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 CustomerService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Customer, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Customer, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Customer, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Customer, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type customerService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.CustomerRepository
}
func NewCustomerService(repo repository.CustomerRepository, validate *validator.Validate) CustomerService {
return &customerService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s customerService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser").Preload("Pic")
}
func (s customerService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Customer, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
customers, 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 customers: %+v", err)
return nil, 0, err
}
return customers, total, nil
}
func (s customerService) GetOne(c *fiber.Ctx, id uint) (*entity.Customer, error) {
customer, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Customer not found")
}
if err != nil {
s.Log.Errorf("Failed get customer by id: %+v", err)
return nil, err
}
return customer, nil
}
func (s *customerService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Customer, 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 customer name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check customer name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Customer with name %s already exists", req.Name))
}
typ := strings.ToUpper(req.Type)
if !utils.IsValidCustomerSupplierType(typ) {
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid customer type")
}
if err := common.EnsureRelations(c.Context(),
common.RelationCheck{Name: "Pic", ID: &req.PicId, Exists: s.Repository.PicExists},
); err != nil {
return nil, err
}
//TODO: created by dummy
createBody := &entity.Customer{
Name: req.Name,
PicId: req.PicId,
Type: typ,
Address: req.Address,
Phone: req.Phone,
Email: req.Email,
AccountNumber: req.AccountNumber,
CreatedBy: 1,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create customer: %+v", err)
return nil, err
}
return createBody, nil
}
func (s customerService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Customer, 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 customer name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check customer name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Customer with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
if req.PicId != nil {
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Pic", ID: req.PicId, Exists: s.Repository.PicExists}); err != nil {
return nil, err
}
updateBody["pic_id"] = *req.PicId
}
if req.Type != nil {
typ := strings.ToUpper(*req.Type)
if !utils.IsValidCustomerSupplierType(typ) {
return nil, fiber.NewError(fiber.StatusBadRequest, "invalid customer type")
}
updateBody["type"] = typ
}
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, "Customer not found")
}
s.Log.Errorf("Failed to update customer: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s customerService) 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, "Customer not found")
}
s.Log.Errorf("Failed to delete customer: %+v", err)
return err
}
return nil
}
@@ -0,0 +1,27 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
PicId uint `json:"pic_id" validate:"required_strict,number,gt=0"`
Type string `json:"type" validate:"required_strict"`
Address string `json:"address" validate:"required_strict"`
Phone string `json:"phone" validate:"required_strict,max=20"`
Email string `json:"email" validate:"required_strict,email"`
AccountNumber string `json:"account_number" validate:"required_strict"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
PicId *uint `json:"pic_id,omitempty" validate:"omitempty,number,gt=0"`
Type *string `json:"type,omitempty" validate:"omitempty"`
Address *string `json:"address,omitempty" validate:"omitempty"`
Phone *string `json:"phone,omitempty" validate:"omitempty"`
Email *string `json:"email,omitempty" validate:"omitempty"`
AccountNumber *string `json:"account_number,omitempty" validate:"omitempty"`
}
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"`
}
@@ -0,0 +1,140 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type KandangController struct {
KandangService service.KandangService
}
func NewKandangController(kandangService service.KandangService) *KandangController {
return &KandangController{
KandangService: kandangService,
}
}
func (u *KandangController) 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.KandangService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.KandangListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all kandangs successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToKandangListDTOs(result),
})
}
func (u *KandangController) 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.KandangService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get kandang successfully",
Data: dto.ToKandangListDTO(*result),
})
}
func (u *KandangController) 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.KandangService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create kandang successfully",
Data: dto.ToKandangListDTO(*result),
})
}
func (u *KandangController) 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.KandangService.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 kandang successfully",
Data: dto.ToKandangListDTO(*result),
})
}
func (u *KandangController) 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.KandangService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete kandang successfully",
})
}
@@ -0,0 +1,75 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type KandangBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Location *locationDTO.LocationBaseDTO `json:"location"`
Pic *userDTO.UserBaseDTO `json:"pic"`
}
type KandangListDTO struct {
KandangBaseDTO
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type KandangDetailDTO struct {
KandangListDTO
}
// === Mapper Functions ===
func ToKandangBaseDTO(e entity.Kandang) KandangBaseDTO {
var location *locationDTO.LocationBaseDTO
if e.Location.Id != 0 {
mapped := locationDTO.ToLocationBaseDTO(e.Location)
location = &mapped
}
var pic *userDTO.UserBaseDTO
if e.Pic.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.Pic)
pic = &mapped
}
return KandangBaseDTO{
Id: e.Id,
Name: e.Name,
Location: location,
Pic: pic,
}
}
func ToKandangListDTO(e entity.Kandang) KandangListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return KandangListDTO{
KandangBaseDTO: ToKandangBaseDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
func ToKandangListDTOs(e []entity.Kandang) []KandangListDTO {
result := make([]KandangListDTO, len(e))
for i, r := range e {
result[i] = ToKandangListDTO(r)
}
return result
}
@@ -0,0 +1,26 @@
package kandangs
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
sKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type KandangModule struct{}
func (KandangModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
kandangRepo := rKandang.NewKandangRepository(db)
userRepo := rUser.NewUserRepository(db)
kandangService := sKandang.NewKandangService(kandangRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
KandangRoutes(router, userService, kandangService)
}
@@ -0,0 +1,40 @@
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 KandangRepository interface {
repository.BaseRepository[entity.Kandang]
LocationExists(ctx context.Context, areaId uint) (bool, error)
PicExists(ctx context.Context, areaId uint) (bool, error)
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type KandangRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Kandang]
db *gorm.DB
}
func NewKandangRepository(db *gorm.DB) KandangRepository {
return &KandangRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Kandang](db),
db: db,
}
}
func (r *KandangRepositoryImpl) LocationExists(ctx context.Context, locationId uint) (bool, error) {
return repository.Exists[entity.Location](ctx, r.db, locationId)
}
func (r *KandangRepositoryImpl) PicExists(ctx context.Context, picId uint) (bool, error) {
return repository.Exists[entity.User](ctx, r.db, picId)
}
func (r *KandangRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Kandang](ctx, r.db, name, excludeID)
}
+28
View File
@@ -0,0 +1,28 @@
package kandangs
import (
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/controllers"
kandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func KandangRoutes(v1 fiber.Router, u user.UserService, s kandang.KandangService) {
ctrl := controller.NewKandangController(s)
route := v1.Group("/kandangs")
// 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,169 @@
package service
import (
"errors"
"fmt"
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/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 KandangService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Kandang, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Kandang, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Kandang, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Kandang, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type kandangService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.KandangRepository
}
func NewKandangService(repo repository.KandangRepository, validate *validator.Validate) KandangService {
return &kandangService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s kandangService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser").Preload("Location").Preload("Pic")
}
func (s kandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Kandang, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
kandangs, 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 kandangs: %+v", err)
return nil, 0, err
}
return kandangs, total, nil
}
func (s kandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Kandang, error) {
kandang, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Kandang not found")
}
if err != nil {
s.Log.Errorf("Failed get kandang by id: %+v", err)
return nil, err
}
return kandang, nil
}
func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Kandang, 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 kandang name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check kandang name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Kandang with name %s already exists", req.Name))
}
if err := common.EnsureRelations(c.Context(),
common.RelationCheck{Name: "Location", ID: &req.LocationId, Exists: s.Repository.LocationExists},
common.RelationCheck{Name: "Pic", ID: &req.PicId, Exists: s.Repository.PicExists},
); err != nil {
return nil, err
}
//TODO: created by dummy
createBody := &entity.Kandang{
Name: req.Name,
LocationId: req.LocationId,
PicId: req.PicId,
CreatedBy: 1,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create kandang: %+v", err)
return nil, err
}
return s.Repository.GetByID(c.Context(), createBody.Id, s.withRelations)
}
func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Kandang, 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 kandang name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check kandang name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Kandang with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
if req.LocationId != nil {
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists}); err != nil {
return nil, err
}
updateBody["location_id"] = *req.LocationId
}
if req.PicId != nil {
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Pic", ID: req.PicId, Exists: s.Repository.PicExists}); err != nil {
return nil, err
}
updateBody["pic_id"] = *req.PicId
}
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, "Kandang not found")
}
s.Log.Errorf("Failed to update kandang: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s kandangService) 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, "Kandang not found")
}
s.Log.Errorf("Failed to delete kandang: %+v", err)
return err
}
return nil
}
@@ -0,0 +1,19 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
LocationId uint `json:"location_id" validate:"required_strict,number,gt=0"`
PicId uint `json:"pic_id" validate:"required_strict,number,gt=0"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
PicId *uint `json:"pic_id,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"`
}
@@ -0,0 +1,140 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type LocationController struct {
LocationService service.LocationService
}
func NewLocationController(locationService service.LocationService) *LocationController {
return &LocationController{
LocationService: locationService,
}
}
func (u *LocationController) 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.LocationService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.LocationListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all locations successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToLocationListDTOs(result),
})
}
func (u *LocationController) 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.LocationService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get location successfully",
Data: dto.ToLocationListDTO(*result),
})
}
func (u *LocationController) 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.LocationService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create location successfully",
Data: dto.ToLocationListDTO(*result),
})
}
func (u *LocationController) 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.LocationService.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 location successfully",
Data: dto.ToLocationListDTO(*result),
})
}
func (u *LocationController) 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.LocationService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete location successfully",
})
}
@@ -0,0 +1,69 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type LocationBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
Area *areaDTO.AreaBaseDTO `json:"area"`
}
type LocationListDTO struct {
LocationBaseDTO
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type LocationDetailDTO struct {
LocationListDTO
}
// === Mapper Functions ===
func ToLocationBaseDTO(e entity.Location) LocationBaseDTO {
var area *areaDTO.AreaBaseDTO
if e.Area.Id != 0 {
mapped := areaDTO.ToAreaBaseDTO(e.Area)
area = &mapped
}
return LocationBaseDTO{
Id: e.Id,
Name: e.Name,
Address: e.Address,
Area: area,
}
}
func ToLocationListDTO(e entity.Location) LocationListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return LocationListDTO{
LocationBaseDTO: ToLocationBaseDTO(e),
CreatedUser: createdUser,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
}
}
func ToLocationListDTOs(e []entity.Location) []LocationListDTO {
result := make([]LocationListDTO, len(e))
for i, r := range e {
result[i] = ToLocationListDTO(r)
}
return result
}
@@ -0,0 +1,26 @@
package locations
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rLocation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/repositories"
sLocation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type LocationModule struct{}
func (LocationModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
locationRepo := rLocation.NewLocationRepository(db)
userRepo := rUser.NewUserRepository(db)
locationService := sLocation.NewLocationService(locationRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
LocationRoutes(router, userService, locationService)
}
@@ -0,0 +1,35 @@
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 LocationRepository interface {
repository.BaseRepository[entity.Location]
AreaExists(ctx context.Context, areaId uint) (bool, error)
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type LocationRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Location]
db *gorm.DB
}
func NewLocationRepository(db *gorm.DB) LocationRepository {
return &LocationRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Location](db),
db: db,
}
}
func (r *LocationRepositoryImpl) AreaExists(ctx context.Context, areaId uint) (bool, error) {
return repository.Exists[entity.Area](ctx, r.db, areaId)
}
func (r *LocationRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Location](ctx, r.db, name, excludeID)
}
@@ -0,0 +1,28 @@
package locations
import (
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/controllers"
location "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func LocationRoutes(v1 fiber.Router, u user.UserService, s location.LocationService) {
ctrl := controller.NewLocationController(s)
route := v1.Group("/locations")
// 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,171 @@
package service
import (
"errors"
"fmt"
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/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 LocationService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Location, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Location, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Location, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Location, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type locationService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.LocationRepository
}
func NewLocationService(repo repository.LocationRepository, validate *validator.Validate) LocationService {
return &locationService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s locationService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("Area").Preload("CreatedUser")
}
func (s locationService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Location, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
locations, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
db = s.withRelations(db)
if params.Search != "" {
db = 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 locations: %+v", err)
return nil, 0, err
}
return locations, total, nil
}
func (s locationService) GetOne(c *fiber.Ctx, id uint) (*entity.Location, error) {
location, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Location not found")
}
if err != nil {
s.Log.Errorf("Failed get location by id: %+v", err)
return nil, err
}
return location, nil
}
func (s *locationService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Location, 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 location name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check location name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Location with name %s already exists", req.Name))
}
if err := common.EnsureRelations(c.Context(),
common.RelationCheck{Name: "Area", ID: &req.AreaId, Exists: s.Repository.AreaExists},
); err != nil {
return nil, err
}
//TODO: created by dummy
createBody := &entity.Location{
Name: req.Name,
Address: req.Address,
AreaId: req.AreaId,
CreatedBy: 1,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create location: %+v", err)
return nil, err
}
created, err := s.Repository.GetByID(c.Context(), createBody.Id, s.withRelations)
if err != nil {
s.Log.Errorf("Failed to reload created location: %+v", err)
return nil, err
}
return created, nil
}
func (s locationService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Location, 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 location name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check location name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Location with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
if req.Address != nil {
updateBody["address"] = *req.Address
}
if req.AreaId != nil {
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Area", ID: req.AreaId, Exists: s.Repository.AreaExists}); err != nil {
return nil, err
}
updateBody["area_id"] = *req.AreaId
}
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, "Location not found")
}
s.Log.Errorf("Failed to update location: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s locationService) 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, "Location not found")
}
s.Log.Errorf("Failed to delete location: %+v", err)
return err
}
return nil
}
@@ -0,0 +1,19 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
Address string `json:"address" validate:"required_strict"`
AreaId uint `json:"area_id" validate:"required_strict,number,gt=0"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
Address *string `json:"address,omitempty" validate:"omitempty"`
AreaId *uint `json:"area_id,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"`
}
+11 -1
View File
@@ -7,15 +7,25 @@ import (
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
areas "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas"
customers "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers"
kandangs "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs"
locations "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations"
uoms "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms"
warehouses "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses"
// MODULE IMPORTS
)
func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
group := router.Group("/master")
group := router.Group("/master-data")
allModules := []modules.Module{
uoms.UomModule{},
areas.AreaModule{},
locations.LocationModule{},
kandangs.KandangModule{},
warehouses.WarehouseModule{},
customers.CustomerModule{},
// MODULE REGISTRY
}
+24 -15
View File
@@ -3,20 +3,22 @@ package dto
import (
"time"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/models"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type UomBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Id uint `json:"id"`
Name string `json:"name"`
}
type UomListDTO struct {
UomBaseDTO
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type UomDetailDTO struct {
@@ -25,24 +27,31 @@ type UomDetailDTO struct {
// === Mapper Functions ===
func ToUomBaseDTO(m model.Uom) UomBaseDTO {
func ToUomBaseDTO(e entity.Uom) UomBaseDTO {
return UomBaseDTO{
Id: m.Id,
Name: m.Name,
Id: e.Id,
Name: e.Name,
}
}
func ToUomListDTO(m model.Uom) UomListDTO {
func ToUomListDTO(e entity.Uom) UomListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return UomListDTO{
UomBaseDTO: ToUomBaseDTO(m),
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
UomBaseDTO: ToUomBaseDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
func ToUomListDTOs(m []model.Uom) []UomListDTO {
result := make([]UomListDTO, len(m))
for i, r := range m {
func ToUomListDTOs(e []entity.Uom) []UomListDTO {
result := make([]UomListDTO, len(e))
for i, r := range e {
result[i] = ToUomListDTO(r)
}
return result
@@ -1,19 +0,0 @@
package model
import (
"time"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/users/models"
"gorm.io/gorm"
)
type Uom struct {
Id uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
CreatedBy int64 `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
CreatedUser model.User `gorm:"foreignKey:CreatedBy;references:Id"`
}
@@ -1,21 +1,30 @@
package repository
import (
model "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/models"
"gitlab.com/mbugroup/lti-api.git/internal/repository"
"context"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gorm.io/gorm"
)
type UomRepository interface {
repository.BaseRepository[model.Uom]
repository.BaseRepository[entity.Uom]
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type UomRepositoryImpl struct {
*repository.BaseRepositoryImpl[model.Uom]
*repository.BaseRepositoryImpl[entity.Uom]
db *gorm.DB
}
func NewUomRepository(db *gorm.DB) UomRepository {
return &UomRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[model.Uom](db),
BaseRepositoryImpl: repository.NewBaseRepository[entity.Uom](db),
db: db,
}
}
func (r *UomRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Uom](ctx, r.db, name, excludeID)
}
@@ -2,8 +2,9 @@ package service
import (
"errors"
"fmt"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/models"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
@@ -15,10 +16,10 @@ import (
)
type UomService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]model.Uom, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*model.Uom, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*model.Uom, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*model.Uom, error)
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Uom, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Uom, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Uom, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Uom, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
@@ -35,7 +36,12 @@ func NewUomService(repo repository.UomRepository, validate *validator.Validate)
Repository: repo,
}
}
func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.Uom, int64, error) {
func (s uomService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser")
}
func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Uom, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
@@ -43,6 +49,7 @@ func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.Uom,
offset := (params.Page - 1) * params.Limit
uoms, 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+"%")
}
@@ -56,8 +63,8 @@ func (s uomService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.Uom,
return uoms, total, nil
}
func (s uomService) GetOne(c *fiber.Ctx, id uint) (*model.Uom, error) {
uom, err := s.Repository.GetByID(c.Context(), id, nil)
func (s uomService) GetOne(c *fiber.Ctx, id uint) (*entity.Uom, error) {
uom, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Uom not found")
}
@@ -68,12 +75,20 @@ func (s uomService) GetOne(c *fiber.Ctx, id uint) (*model.Uom, error) {
return uom, nil
}
func (s *uomService) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.Uom, error) {
func (s *uomService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Uom, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
createBody := &model.Uom{
if exists, err := s.Repository.NameExists(c.Context(), req.Name, nil); err != nil {
s.Log.Errorf("Failed to check uom name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check uom name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Uom with name %s already exists", req.Name))
}
//TODO: created by dummy
createBody := &entity.Uom{
Name: req.Name,
CreatedBy: 1,
}
@@ -83,10 +98,10 @@ func (s *uomService) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.Uom
return nil, err
}
return createBody, nil
return s.Repository.GetByID(c.Context(), createBody.Id, s.withRelations)
}
func (s uomService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*model.Uom, error) {
func (s uomService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Uom, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
@@ -94,9 +109,19 @@ func (s uomService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*m
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 uom name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check uom name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Uom with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
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, "Uom not found")
@@ -0,0 +1,140 @@
package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type WarehouseController struct {
WarehouseService service.WarehouseService
}
func NewWarehouseController(warehouseService service.WarehouseService) *WarehouseController {
return &WarehouseController{
WarehouseService: warehouseService,
}
}
func (u *WarehouseController) 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.WarehouseService.GetAll(c, query)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.WarehouseListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all warehouses successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToWarehouseListDTOs(result),
})
}
func (u *WarehouseController) 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.WarehouseService.GetOne(c, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get warehouse successfully",
Data: dto.ToWarehouseListDTO(*result),
})
}
func (u *WarehouseController) 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.WarehouseService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create warehouse successfully",
Data: dto.ToWarehouseListDTO(*result),
})
}
func (u *WarehouseController) 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.WarehouseService.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 warehouse successfully",
Data: dto.ToWarehouseListDTO(*result),
})
}
func (u *WarehouseController) 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.WarehouseService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete warehouse successfully",
})
}
@@ -0,0 +1,87 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)
// === DTO Structs ===
type WarehouseBaseDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Area *areaDTO.AreaBaseDTO `json:"area"`
Location *locationDTO.LocationBaseDTO `json:"location"`
Kandang *kandangDTO.KandangBaseDTO `json:"kandang"`
}
type WarehouseListDTO struct {
WarehouseBaseDTO
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type WarehouseDetailDTO struct {
WarehouseListDTO
}
// === Mapper Functions ===
func ToWarehouseBaseDTO(e entity.Warehouse) WarehouseBaseDTO {
var area *areaDTO.AreaBaseDTO
if e.Area.Id != 0 {
mapped := areaDTO.ToAreaBaseDTO(e.Area)
area = &mapped
}
var location *locationDTO.LocationBaseDTO
if e.Location != nil && e.Location.Id != 0 {
mapped := locationDTO.ToLocationBaseDTO(*e.Location)
location = &mapped
}
var kandang *kandangDTO.KandangBaseDTO
if e.Kandang != nil && e.Kandang.Id != 0 {
mapped := kandangDTO.ToKandangBaseDTO(*e.Kandang)
kandang = &mapped
}
return WarehouseBaseDTO{
Id: e.Id,
Name: e.Name,
Type: e.Type,
Area: area,
Location: location,
Kandang: kandang,
}
}
func ToWarehouseListDTO(e entity.Warehouse) WarehouseListDTO {
var createdUser *userDTO.UserBaseDTO
if e.CreatedUser.Id != 0 {
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped
}
return WarehouseListDTO{
WarehouseBaseDTO: ToWarehouseBaseDTO(e),
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
CreatedUser: createdUser,
}
}
func ToWarehouseListDTOs(e []entity.Warehouse) []WarehouseListDTO {
result := make([]WarehouseListDTO, len(e))
for i, r := range e {
result[i] = ToWarehouseListDTO(r)
}
return result
}
@@ -0,0 +1,26 @@
package warehouses
import (
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
sWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/services"
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
)
type WarehouseModule struct{}
func (WarehouseModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
userRepo := rUser.NewUserRepository(db)
warehouseService := sWarehouse.NewWarehouseService(warehouseRepo, validate)
userService := sUser.NewUserService(userRepo, validate)
WarehouseRoutes(router, userService, warehouseService)
}
@@ -0,0 +1,45 @@
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 WarehouseRepository interface {
repository.BaseRepository[entity.Warehouse]
AreaExists(ctx context.Context, areaId uint) (bool, error)
LocationExists(ctx context.Context, locationId uint) (bool, error)
KandangExists(ctx context.Context, kandangId uint) (bool, error)
NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
}
type WarehouseRepositoryImpl struct {
*repository.BaseRepositoryImpl[entity.Warehouse]
db *gorm.DB
}
func NewWarehouseRepository(db *gorm.DB) WarehouseRepository {
return &WarehouseRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[entity.Warehouse](db),
db: db,
}
}
func (r *WarehouseRepositoryImpl) AreaExists(ctx context.Context, areaId uint) (bool, error) {
return repository.Exists[entity.Area](ctx, r.db, areaId)
}
func (r *WarehouseRepositoryImpl) LocationExists(ctx context.Context, locationId uint) (bool, error) {
return repository.Exists[entity.Location](ctx, r.db, locationId)
}
func (r *WarehouseRepositoryImpl) KandangExists(ctx context.Context, kandangId uint) (bool, error) {
return repository.Exists[entity.Kandang](ctx, r.db, kandangId)
}
func (r *WarehouseRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
return repository.ExistsByName[entity.Warehouse](ctx, r.db, name, excludeID)
}
@@ -0,0 +1,28 @@
package warehouses
import (
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/controllers"
warehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/services"
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
"github.com/gofiber/fiber/v2"
)
func WarehouseRoutes(v1 fiber.Router, u user.UserService, s warehouse.WarehouseService) {
ctrl := controller.NewWarehouseController(s)
route := v1.Group("/warehouses")
// 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,268 @@
package service
import (
"errors"
"fmt"
"strings"
common "gitlab.com/mbugroup/lti-api.git/internal/common/service"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/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 WarehouseService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.Warehouse, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.Warehouse, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.Warehouse, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.Warehouse, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
type warehouseService struct {
Log *logrus.Logger
Validate *validator.Validate
Repository repository.WarehouseRepository
}
func NewWarehouseService(repo repository.WarehouseRepository, validate *validator.Validate) WarehouseService {
return &warehouseService{
Log: utils.Log,
Validate: validate,
Repository: repo,
}
}
func (s warehouseService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("CreatedUser").Preload("Area").Preload("Location").Preload("Kandang")
}
func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Warehouse, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
offset := (params.Page - 1) * params.Limit
warehouses, 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 warehouses: %+v", err)
return nil, 0, err
}
return warehouses, total, nil
}
func (s warehouseService) GetOne(c *fiber.Ctx, id uint) (*entity.Warehouse, error) {
warehouse, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
}
if err != nil {
s.Log.Errorf("Failed get warehouse by id: %+v", err)
return nil, err
}
return warehouse, nil
}
func (s *warehouseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Warehouse, 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 warehouse name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check warehouse name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Warehouse with name %s already exists", req.Name))
}
typ := strings.ToUpper(req.Type)
if err := validateWarehouseTypeRequirements(typ, &req.AreaId, req.LocationId, req.KandangId); err != nil {
return nil, err
}
//? Check relation area, location, and kandang
if err := common.EnsureRelations(c.Context(),
common.RelationCheck{Name: "Area", ID: &req.AreaId, Exists: s.Repository.AreaExists},
common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists},
common.RelationCheck{Name: "Kandang", ID: req.KandangId, Exists: s.Repository.KandangExists},
); err != nil {
return nil, err
}
//TODO: created by dummy
createBody := &entity.Warehouse{
Name: req.Name,
Type: typ,
AreaId: req.AreaId,
CreatedBy: 1,
}
if req.LocationId != nil {
createBody.LocationId = req.LocationId
}
if req.KandangId != nil {
createBody.KandangId = req.KandangId
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
s.Log.Errorf("Failed to create warehouse: %+v", err)
return nil, err
}
return s.Repository.GetByID(c.Context(), createBody.Id, s.withRelations)
}
func (s warehouseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Warehouse, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
existing, err := s.GetOne(c, id)
if err != nil {
s.Log.Errorf("Failed to get warehouse for update: %+v", err)
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 warehouse name: %+v", err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check warehouse name")
} else if exists {
return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Warehouse with name %s already exists", *req.Name))
}
updateBody["name"] = *req.Name
}
if req.Type != nil {
normalizedType := strings.ToUpper(*req.Type)
updateBody["type"] = normalizedType
req.Type = &normalizedType
}
if req.AreaId != nil {
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Area", ID: req.AreaId, Exists: s.Repository.AreaExists}); err != nil {
return nil, err
}
updateBody["area_id"] = *req.AreaId
}
if req.LocationId != nil {
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Location", ID: req.LocationId, Exists: s.Repository.LocationExists}); err != nil {
return nil, err
}
updateBody["location_id"] = req.LocationId
}
if req.KandangId != nil {
if err := common.EnsureRelations(c.Context(), common.RelationCheck{Name: "Kandang", ID: req.KandangId, Exists: s.Repository.KandangExists}); err != nil {
return nil, err
}
updateBody["kandang_id"] = req.KandangId
}
finalType := strings.ToUpper(existing.Type)
if req.Type != nil {
finalType = *req.Type
}
finalAreaId := existing.AreaId
if req.AreaId != nil {
finalAreaId = *req.AreaId
}
finalLocationId := existing.LocationId
if req.LocationId != nil {
finalLocationId = req.LocationId
}
finalKandangId := existing.KandangId
if req.KandangId != nil {
finalKandangId = req.KandangId
}
if err := validateWarehouseTypeRequirements(finalType, &finalAreaId, finalLocationId, finalKandangId); err != nil {
return nil, err
}
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, "Warehouse not found")
}
s.Log.Errorf("Failed to update warehouse: %+v", err)
return nil, err
}
return s.GetOne(c, id)
}
func (s warehouseService) 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, "Warehouse not found")
}
s.Log.Errorf("Failed to delete warehouse: %+v", err)
return err
}
return nil
}
func validateWarehouseTypeRequirements(typ string, areaID *uint, locationID *uint, kandangID *uint) error {
switch utils.WarehouseType(typ) {
case utils.WarehouseTypeArea:
if areaID == nil || *areaID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is AREA")
}
if locationID != nil {
return fiber.NewError(fiber.StatusBadRequest, "location_id must not be provided when type is AREA")
}
if kandangID != nil {
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is AREA")
}
return nil
case utils.WarehouseTypeLokasi:
if areaID == nil || *areaID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is LOCATION")
}
if locationID == nil {
return fiber.NewError(fiber.StatusBadRequest, "location_id is required when type is LOCATION")
}
if *locationID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "location_id must be greater than 0 when type is LOCATION")
}
if kandangID != nil {
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must not be provided when type is LOCATION")
}
return nil
case utils.WarehouseTypeKandang:
if areaID == nil || *areaID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "area_id is required when type is KANDANG")
}
if locationID == nil {
return fiber.NewError(fiber.StatusBadRequest, "location_id is required when type is KANDANG")
}
if *locationID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "location_id must be greater than 0 when type is KANDANG")
}
if kandangID == nil {
return fiber.NewError(fiber.StatusBadRequest, "kandang_id is required when type is KANDANG")
}
if *kandangID == 0 {
return fiber.NewError(fiber.StatusBadRequest, "kandang_id must be greater than 0 when type is KANDANG")
}
return nil
default:
return fiber.NewError(fiber.StatusBadRequest, "Invalid warehouse type")
}
}
@@ -0,0 +1,23 @@
package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
Type string `json:"type" validate:"required_strict"`
AreaId uint `json:"area_id" validate:"required_strict,number,gt=0"`
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
KandangId *uint `json:"kandang_id,omitempty" validate:"omitempty,number,gt=0"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
Type *string `json:"type,omitempty" validate:"omitempty"`
AreaId *uint `json:"area_id,omitempty" validate:"omitempty,number,gt=0"`
LocationId *uint `json:"location_id,omitempty" validate:"omitempty,number,gt=0"`
KandangId *uint `json:"kandang_id,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"`
}
+8 -6
View File
@@ -3,7 +3,7 @@ package dto
import (
"time"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/users/models"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
)
// === DTO Structs ===
@@ -27,14 +27,16 @@ type UserDetailDTO struct {
// === Mapper Functions ===
func ToUserBaseDTO(m model.User) UserBaseDTO {
func ToUserBaseDTO(m entity.User) UserBaseDTO {
return UserBaseDTO{
Id: m.Id,
Name: m.Name,
Id: m.Id,
IdUser: m.IdUser,
Email: m.Email,
Name: m.Name,
}
}
func ToUserListDTO(m model.User) UserListDTO {
func ToUserListDTO(m entity.User) UserListDTO {
return UserListDTO{
UserBaseDTO: ToUserBaseDTO(m),
CreatedAt: m.CreatedAt,
@@ -42,7 +44,7 @@ func ToUserListDTO(m model.User) UserListDTO {
}
}
func ToUserListDTOs(m []model.User) []UserListDTO {
func ToUserListDTOs(m []entity.User) []UserListDTO {
result := make([]UserListDTO, len(m))
for i, r := range m {
result[i] = ToUserListDTO(r)
@@ -1,17 +0,0 @@
package model
import (
"time"
"gorm.io/gorm"
)
type User struct {
Id uint `gorm:"primaryKey"`
IdUser int64 `gorm:"uniqueIndex"`
Email string `gorm:"uniqueIndex"`
Name string `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
@@ -1,21 +1,21 @@
package repository
import (
model "gitlab.com/mbugroup/lti-api.git/internal/modules/users/models"
"gitlab.com/mbugroup/lti-api.git/internal/repository"
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
"gorm.io/gorm"
)
type UserRepository interface {
repository.BaseRepository[model.User]
repository.BaseRepository[entity.User]
}
type UserRepositoryImpl struct {
*repository.BaseRepositoryImpl[model.User]
*repository.BaseRepositoryImpl[entity.User]
}
func NewUserRepository(db *gorm.DB) UserRepository {
return &UserRepositoryImpl{
BaseRepositoryImpl: repository.NewBaseRepository[model.User](db),
BaseRepositoryImpl: repository.NewBaseRepository[entity.User](db),
}
}
+15 -11
View File
@@ -3,7 +3,7 @@ package service
import (
"errors"
model "gitlab.com/mbugroup/lti-api.git/internal/modules/users/models"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/users/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
@@ -15,10 +15,10 @@ import (
)
type UserService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]model.User, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*model.User, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*model.User, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*model.User, error)
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.User, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*entity.User, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.User, error)
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.User, error)
DeleteOne(ctx *fiber.Ctx, id uint) error
}
@@ -35,7 +35,7 @@ func NewUserService(repo repository.UserRepository, validate *validator.Validate
Repository: repo,
}
}
func (s userService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.User, int64, error) {
func (s userService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.User, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
@@ -56,7 +56,7 @@ func (s userService) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.Use
return users, total, nil
}
func (s userService) GetOne(c *fiber.Ctx, id uint) (*model.User, error) {
func (s userService) GetOne(c *fiber.Ctx, id uint) (*entity.User, error) {
user, err := s.Repository.GetByID(c.Context(), id, nil)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "User not found")
@@ -68,13 +68,13 @@ func (s userService) GetOne(c *fiber.Ctx, id uint) (*model.User, error) {
return user, nil
}
func (s *userService) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.User, error) {
func (s *userService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.User, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
createBody := &model.User{
Name: req.Name,
createBody := &entity.User{
Name: req.Name,
}
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
@@ -85,7 +85,7 @@ func (s *userService) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.Us
return createBody, nil
}
func (s userService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*model.User, error) {
func (s userService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.User, error) {
if err := s.Validate.Struct(req); err != nil {
return nil, err
}
@@ -96,6 +96,10 @@ func (s userService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*
updateBody["name"] = *req.Name
}
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, "User not found")