diff --git a/internal/common/repository/common.exists.repository.go b/internal/common/repository/common.exists.repository.go index ef371330..c6bc11f0 100644 --- a/internal/common/repository/common.exists.repository.go +++ b/internal/common/repository/common.exists.repository.go @@ -2,6 +2,7 @@ package repository import ( "context" + "fmt" "gorm.io/gorm" ) @@ -32,3 +33,21 @@ func ExistsByName[T any](ctx context.Context, db *gorm.DB, name string, excludeI } return count > 0, nil } + +func ExistsByField[T any](ctx context.Context, db *gorm.DB, field string, value any, excludeID *uint) (bool, error) { + if field == "" { + return false, fmt.Errorf("field is required") + } + var count int64 + q := db.WithContext(ctx). + Model(new(T)). + Where(fmt.Sprintf("%s = ?", field), value). + Where("deleted_at IS NULL") + if excludeID != nil { + q = q.Where("id <> ?", *excludeID) + } + if err := q.Count(&count).Error; err != nil { + return false, err + } + return count > 0, nil +} diff --git a/internal/modules/master/areas/controllers/area.controller.go b/internal/modules/master/areas/controllers/area.controller.go index e08dba7d..252bc769 100644 --- a/internal/modules/master/areas/controllers/area.controller.go +++ b/internal/modules/master/areas/controllers/area.controller.go @@ -29,6 +29,10 @@ func (u *AreaController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.AreaService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/banks/controllers/bank.controller.go b/internal/modules/master/banks/controllers/bank.controller.go index 7625d078..ffe61cea 100644 --- a/internal/modules/master/banks/controllers/bank.controller.go +++ b/internal/modules/master/banks/controllers/bank.controller.go @@ -29,6 +29,10 @@ func (u *BankController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.BankService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/banks/repositories/bank.repository.go b/internal/modules/master/banks/repositories/bank.repository.go index 53d27713..d309d3c1 100644 --- a/internal/modules/master/banks/repositories/bank.repository.go +++ b/internal/modules/master/banks/repositories/bank.repository.go @@ -11,6 +11,7 @@ import ( type BankRepository interface { repository.BaseRepository[entity.Bank] NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) + AccountNumberExists(ctx context.Context, accountNumber string, excludeID *uint) (bool, error) } type BankRepositoryImpl struct { @@ -28,3 +29,7 @@ func NewBankRepository(db *gorm.DB) BankRepository { func (r *BankRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) { return repository.ExistsByName[entity.Bank](ctx, r.db, name, excludeID) } + +func (r *BankRepositoryImpl) AccountNumberExists(ctx context.Context, accountNumber string, excludeID *uint) (bool, error) { + return repository.ExistsByField[entity.Bank](ctx, r.db, "account_number", accountNumber, excludeID) +} diff --git a/internal/modules/master/banks/services/bank.service.go b/internal/modules/master/banks/services/bank.service.go index b62bf864..83d3029d 100644 --- a/internal/modules/master/banks/services/bank.service.go +++ b/internal/modules/master/banks/services/bank.service.go @@ -87,6 +87,13 @@ func (s *bankService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.B return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Bank with name %s already exists", req.Name)) } + if exists, err := s.Repository.AccountNumberExists(c.Context(), req.AccountNumber, nil); err != nil { + s.Log.Errorf("Failed to check bank account number: %+v", err) + return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check bank account number") + } else if exists { + return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Bank with account number %s already exists", req.AccountNumber)) + } + createBody := &entity.Bank{ Name: req.Name, Alias: req.Alias, diff --git a/internal/modules/master/customers/controllers/customer.controller.go b/internal/modules/master/customers/controllers/customer.controller.go index 2f9c0ed4..02805f6f 100644 --- a/internal/modules/master/customers/controllers/customer.controller.go +++ b/internal/modules/master/customers/controllers/customer.controller.go @@ -29,6 +29,10 @@ func (u *CustomerController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.CustomerService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/fcrs/controllers/fcr.controller.go b/internal/modules/master/fcrs/controllers/fcr.controller.go index 33353ffa..52db463d 100644 --- a/internal/modules/master/fcrs/controllers/fcr.controller.go +++ b/internal/modules/master/fcrs/controllers/fcr.controller.go @@ -29,6 +29,10 @@ func (u *FcrController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.FcrService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/flocks/controllers/flock.controller.go b/internal/modules/master/flocks/controllers/flock.controller.go index 8265f3e4..f8df0587 100644 --- a/internal/modules/master/flocks/controllers/flock.controller.go +++ b/internal/modules/master/flocks/controllers/flock.controller.go @@ -29,6 +29,10 @@ func (u *FlockController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.FlockService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/kandangs/controllers/kandang.controller.go b/internal/modules/master/kandangs/controllers/kandang.controller.go index 23d22334..b1d016df 100644 --- a/internal/modules/master/kandangs/controllers/kandang.controller.go +++ b/internal/modules/master/kandangs/controllers/kandang.controller.go @@ -31,6 +31,10 @@ func (u *KandangController) GetAll(c *fiber.Ctx) error { PicId: c.QueryInt("pic_id", 0), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.KandangService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/locations/controllers/location.controller.go b/internal/modules/master/locations/controllers/location.controller.go index 8f8211d7..f360a9c9 100644 --- a/internal/modules/master/locations/controllers/location.controller.go +++ b/internal/modules/master/locations/controllers/location.controller.go @@ -30,6 +30,10 @@ func (u *LocationController) GetAll(c *fiber.Ctx) error { AreaId: c.QueryInt("area_id", 0), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.LocationService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/nonstocks/controllers/nonstock.controller.go b/internal/modules/master/nonstocks/controllers/nonstock.controller.go index d8b688b7..d991c4da 100644 --- a/internal/modules/master/nonstocks/controllers/nonstock.controller.go +++ b/internal/modules/master/nonstocks/controllers/nonstock.controller.go @@ -29,6 +29,10 @@ func (u *NonstockController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.NonstockService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/product-categories/controllers/product-category.controller.go b/internal/modules/master/product-categories/controllers/product-category.controller.go index 778a3188..e4531a1f 100644 --- a/internal/modules/master/product-categories/controllers/product-category.controller.go +++ b/internal/modules/master/product-categories/controllers/product-category.controller.go @@ -29,6 +29,10 @@ func (u *ProductCategoryController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.ProductCategoryService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/products/controllers/product.controller.go b/internal/modules/master/products/controllers/product.controller.go index ee2c95f8..197a6b5f 100644 --- a/internal/modules/master/products/controllers/product.controller.go +++ b/internal/modules/master/products/controllers/product.controller.go @@ -30,6 +30,10 @@ func (u *ProductController) GetAll(c *fiber.Ctx) error { ProductCategoryID: c.QueryInt("product_category_id", 0), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.ProductService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/suppliers/controllers/supplier.controller.go b/internal/modules/master/suppliers/controllers/supplier.controller.go index a76904a9..5d70e43e 100644 --- a/internal/modules/master/suppliers/controllers/supplier.controller.go +++ b/internal/modules/master/suppliers/controllers/supplier.controller.go @@ -29,6 +29,10 @@ func (u *SupplierController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.SupplierService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/suppliers/repositories/supplier.repository.go b/internal/modules/master/suppliers/repositories/supplier.repository.go index 46fb2983..6b5a0ae2 100644 --- a/internal/modules/master/suppliers/repositories/supplier.repository.go +++ b/internal/modules/master/suppliers/repositories/supplier.repository.go @@ -11,7 +11,7 @@ import ( type SupplierRepository interface { repository.BaseRepository[entity.Supplier] NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) - + AliasExists(ctx context.Context, alias string, excludeID *uint) (bool, error) } type SupplierRepositoryImpl struct { @@ -29,3 +29,7 @@ func NewSupplierRepository(db *gorm.DB) SupplierRepository { func (r *SupplierRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) { return repository.ExistsByName[entity.Supplier](ctx, r.db, name, excludeID) } + +func (r *SupplierRepositoryImpl) AliasExists(ctx context.Context, alias string, excludeID *uint) (bool, error) { + return repository.ExistsByField[entity.Supplier](ctx, r.db, "alias", alias, excludeID) +} diff --git a/internal/modules/master/suppliers/services/supplier.service.go b/internal/modules/master/suppliers/services/supplier.service.go index f8422350..99e15b29 100644 --- a/internal/modules/master/suppliers/services/supplier.service.go +++ b/internal/modules/master/suppliers/services/supplier.service.go @@ -88,6 +88,13 @@ func (s *supplierService) CreateOne(c *fiber.Ctx, req *validation.Create) (*enti return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Supplier with name %s already exists", req.Name)) } + if exists, err := s.Repository.AliasExists(c.Context(), strings.TrimSpace(strings.ToUpper(req.Alias)), nil); err != nil { + s.Log.Errorf("Failed to check supplier alias: %+v", err) + return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check supplier alias") + } else if exists { + return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Supplier with alias %s already exists", strings.TrimSpace(strings.ToUpper(req.Alias)))) + } + typ := strings.ToUpper(req.Type) if !utils.IsValidCustomerSupplierType(typ) { return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid supplier type") @@ -143,6 +150,12 @@ func (s supplierService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint } if req.Alias != nil { + if exists, err := s.Repository.AliasExists(c.Context(), strings.TrimSpace(strings.ToUpper(*req.Alias)), &id); err != nil { + s.Log.Errorf("Failed to check supplier alias: %+v", err) + return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to check supplier alias") + } else if exists { + return nil, fiber.NewError(fiber.StatusConflict, fmt.Sprintf("Supplier with alias %s already exists", strings.TrimSpace(strings.ToUpper(*req.Alias)))) + } updateBody["alias"] = strings.TrimSpace(strings.ToUpper(*req.Alias)) } diff --git a/internal/modules/master/uoms/controllers/uom.controller.go b/internal/modules/master/uoms/controllers/uom.controller.go index 0bd3a382..ecef1f69 100644 --- a/internal/modules/master/uoms/controllers/uom.controller.go +++ b/internal/modules/master/uoms/controllers/uom.controller.go @@ -29,6 +29,10 @@ func (u *UomController) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.UomService.GetAll(c, query) if err != nil { return err diff --git a/internal/modules/master/warehouses/controllers/warehouse.controller.go b/internal/modules/master/warehouses/controllers/warehouse.controller.go index b841d4ef..afa90660 100644 --- a/internal/modules/master/warehouses/controllers/warehouse.controller.go +++ b/internal/modules/master/warehouses/controllers/warehouse.controller.go @@ -30,6 +30,10 @@ func (u *WarehouseController) GetAll(c *fiber.Ctx) error { AreaId: c.QueryInt("area_id", 0), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.WarehouseService.GetAll(c, query) if err != nil { return err diff --git a/tools/templates/controller.tmpl b/tools/templates/controller.tmpl index 9fcf6d9b..f2eb615e 100644 --- a/tools/templates/controller.tmpl +++ b/tools/templates/controller.tmpl @@ -29,6 +29,10 @@ func (u *{{Pascal .Entity}}Controller) GetAll(c *fiber.Ctx) error { Search: c.Query("search", ""), } + if query.Page < 1 || query.Limit < 1 { + return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") + } + result, totalResults, err := u.{{Pascal .Entity}}Service.GetAll(c, query) if err != nil { return err