mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
[FEAT/BE] Add telur seeder and saparator category
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user