Add category table
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"apskel-pos-be/internal/constants"
|
||||
"apskel-pos-be/internal/contract"
|
||||
)
|
||||
|
||||
type PurchaseCategoryValidator interface {
|
||||
ValidateCreatePurchaseCategoryRequest(req *contract.CreatePurchaseCategoryRequest) (error, string)
|
||||
ValidateUpdatePurchaseCategoryRequest(req *contract.UpdatePurchaseCategoryRequest) (error, string)
|
||||
ValidateListPurchaseCategoriesRequest(req *contract.ListPurchaseCategoriesRequest) (error, string)
|
||||
}
|
||||
|
||||
type PurchaseCategoryValidatorImpl struct{}
|
||||
|
||||
func NewPurchaseCategoryValidator() *PurchaseCategoryValidatorImpl {
|
||||
return &PurchaseCategoryValidatorImpl{}
|
||||
}
|
||||
|
||||
func (v *PurchaseCategoryValidatorImpl) ValidateCreatePurchaseCategoryRequest(req *contract.CreatePurchaseCategoryRequest) (error, string) {
|
||||
if req == nil {
|
||||
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.Name) == "" {
|
||||
return errors.New("name is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if len(req.Name) > 255 {
|
||||
return errors.New("name cannot exceed 255 characters"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if !isValidPurchaseCategoryType(req.Type) {
|
||||
return errors.New("type must be raw_material or non_inventory"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.Code != nil && len(strings.TrimSpace(*req.Code)) > 100 {
|
||||
return errors.New("code cannot exceed 100 characters"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func (v *PurchaseCategoryValidatorImpl) ValidateUpdatePurchaseCategoryRequest(req *contract.UpdatePurchaseCategoryRequest) (error, string) {
|
||||
if req == nil {
|
||||
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if req.ParentID == nil && req.Code == nil && req.Name == nil && req.Type == nil && req.SortOrder == nil && req.IsActive == nil {
|
||||
return errors.New("at least one field must be provided for update"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if req.Name != nil {
|
||||
if strings.TrimSpace(*req.Name) == "" {
|
||||
return errors.New("name cannot be empty"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
if len(*req.Name) > 255 {
|
||||
return errors.New("name cannot exceed 255 characters"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
}
|
||||
|
||||
if req.Type != nil && !isValidPurchaseCategoryType(*req.Type) {
|
||||
return errors.New("type must be raw_material or non_inventory"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.Code != nil && len(strings.TrimSpace(*req.Code)) > 100 {
|
||||
return errors.New("code cannot exceed 100 characters"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func (v *PurchaseCategoryValidatorImpl) ValidateListPurchaseCategoriesRequest(req *contract.ListPurchaseCategoriesRequest) (error, string) {
|
||||
if req == nil {
|
||||
return errors.New("request is required"), constants.MissingFieldErrorCode
|
||||
}
|
||||
|
||||
if req.Page < 1 {
|
||||
return errors.New("page must be at least 1"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.Limit < 1 || req.Limit > 100 {
|
||||
return errors.New("limit must be between 1 and 100"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if req.Type != "" && !isValidPurchaseCategoryType(req.Type) {
|
||||
return errors.New("type must be raw_material or non_inventory"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func isValidPurchaseCategoryType(categoryType string) bool {
|
||||
switch categoryType {
|
||||
case "raw_material", "non_inventory":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user