From 4340828fec8addc7758b0eb7b0298b1e156807d4 Mon Sep 17 00:00:00 2001 From: ragilap Date: Fri, 6 Feb 2026 19:26:00 +0700 Subject: [PATCH] [FEAT/BE] Add telur seeder and saparator category --- internal/database/seed/seeder.go | 30 +++++++++++++++--- .../services/product-category.service.go | 31 ++++++++++++++++++- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/internal/database/seed/seeder.go b/internal/database/seed/seeder.go index 4f666812..b4ccf36a 100644 --- a/internal/database/seed/seeder.go +++ b/internal/database/seed/seeder.go @@ -74,7 +74,7 @@ func seedUsers(tx *gorm.DB) (map[string]uint, error) { } func seedUoms(tx *gorm.DB, createdBy uint) (map[string]uint, error) { - names := []string{"Kilogram", "Gram", "Liter", "Unit", "Ekor"} + names := []string{"Kilogram", "Gram", "Liter", "Unit", "Ekor", "Butir"} result := make(map[string]uint, len(names)) for _, name := range names { @@ -235,7 +235,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories Name: "Telur Utuh", Brand: "-", Sku: "4", - Uom: "Gram", + Uom: "Butir", Category: "Telur", Price: 1, Flags: []utils.FlagType{utils.FlagTelurUtuh}, @@ -245,7 +245,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories Name: "Telur Pecah", Brand: "-", Sku: "5", - Uom: "Gram", + Uom: "Butir", Category: "Telur", Price: 1, Flags: []utils.FlagType{utils.FlagTelurPecah}, @@ -255,7 +255,7 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories Name: "Telur Putih", Brand: "-", Sku: "6", - Uom: "Gram", + Uom: "Butir", Category: "Telur", Price: 1, Flags: []utils.FlagType{utils.FlagTelurPutih}, @@ -265,12 +265,32 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories Name: "Telur Retak", Brand: "-", Sku: "7", - Uom: "Gram", + Uom: "Butir", Category: "Telur", Price: 1, Flags: []utils.FlagType{utils.FlagTelurRetak}, IsVisible: false, }, + { + Name: "Telur Papacal", + Brand: "-", + Sku: "8", + Uom: "Butir", + Category: "Telur", + Price: 1, + Flags: []utils.FlagType{utils.FlagTelur}, + IsVisible: false, + }, + { + Name: "Telur Jumbo", + Brand: "-", + Sku: "9", + Uom: "Butir", + Category: "Telur", + Price: 1, + Flags: []utils.FlagType{utils.FlagTelur}, + IsVisible: false, + }, } for _, seed := range seeds { diff --git a/internal/modules/master/product-categories/services/product-category.service.go b/internal/modules/master/product-categories/services/product-category.service.go index ae1577f1..757d5787 100644 --- a/internal/modules/master/product-categories/services/product-category.service.go +++ b/internal/modules/master/product-categories/services/product-category.service.go @@ -52,7 +52,22 @@ func (s productCategoryService) GetAll(c *fiber.Ctx, params *validation.Query) ( productCategories, 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 ILIKE ?", "%"+params.Search+"%") + terms := splitSearchTerms(params.Search) + if len(terms) == 0 { + return db + } + if len(terms) == 1 { + return db.Where("name ILIKE ?", "%"+terms[0]+"%") + } + for i, term := range terms { + like := "%" + term + "%" + if i == 0 { + db = db.Where("name ILIKE ?", like) + } else { + db = db.Or("name ILIKE ?", like) + } + } + return db } return db.Order("created_at DESC").Order("updated_at DESC") }) @@ -64,6 +79,20 @@ func (s productCategoryService) GetAll(c *fiber.Ctx, params *validation.Query) ( return productCategories, total, nil } +func splitSearchTerms(raw string) []string { + parts := strings.FieldsFunc(raw, func(r rune) bool { + return r == ',' || r == ';' || r == '|' + }) + terms := make([]string, 0, len(parts)) + for _, part := range parts { + trimmed := strings.TrimSpace(part) + if trimmed != "" { + terms = append(terms, trimmed) + } + } + return terms +} + func (s productCategoryService) GetOne(c *fiber.Ctx, id uint) (*entity.ProductCategory, error) { productCategory, err := s.Repository.GetByID(c.Context(), id, s.withRelations) if errors.Is(err, gorm.ErrRecordNotFound) {