Add category table

This commit is contained in:
2026-06-08 12:29:59 +07:00
parent 094e8b2a47
commit 69d8c8ce5e
19 changed files with 1159 additions and 1 deletions
@@ -0,0 +1,91 @@
package repository
import (
"context"
"apskel-pos-be/internal/entities"
"github.com/google/uuid"
"gorm.io/gorm"
)
type PurchaseCategoryRepositoryImpl struct {
db *gorm.DB
}
func NewPurchaseCategoryRepositoryImpl(db *gorm.DB) *PurchaseCategoryRepositoryImpl {
return &PurchaseCategoryRepositoryImpl{db: db}
}
func (r *PurchaseCategoryRepositoryImpl) Create(ctx context.Context, category *entities.PurchaseCategory) error {
return r.db.WithContext(ctx).Create(category).Error
}
func (r *PurchaseCategoryRepositoryImpl) GetByIDAndOrganizationID(ctx context.Context, id, organizationID uuid.UUID) (*entities.PurchaseCategory, error) {
var category entities.PurchaseCategory
err := r.db.WithContext(ctx).
First(&category, "id = ? AND organization_id = ?", id, organizationID).Error
if err != nil {
return nil, err
}
return &category, nil
}
func (r *PurchaseCategoryRepositoryImpl) Update(ctx context.Context, category *entities.PurchaseCategory) error {
return r.db.WithContext(ctx).Save(category).Error
}
func (r *PurchaseCategoryRepositoryImpl) SoftDelete(ctx context.Context, id, organizationID uuid.UUID) error {
return r.db.WithContext(ctx).
Model(&entities.PurchaseCategory{}).
Where("id = ? AND organization_id = ?", id, organizationID).
Update("is_active", false).Error
}
func (r *PurchaseCategoryRepositoryImpl) List(ctx context.Context, organizationID uuid.UUID, filters map[string]interface{}, limit, offset int) ([]*entities.PurchaseCategory, int64, error) {
var categories []*entities.PurchaseCategory
var total int64
query := r.db.WithContext(ctx).
Model(&entities.PurchaseCategory{}).
Where("organization_id = ?", organizationID)
for key, value := range filters {
switch key {
case "search":
searchValue := "%" + value.(string) + "%"
query = query.Where("name ILIKE ? OR code ILIKE ?", searchValue, searchValue)
case "parent_id":
query = query.Where("parent_id = ?", value)
case "type":
query = query.Where("type = ?", value)
case "is_active":
query = query.Where("is_active = ?", value)
}
}
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
err := query.
Order("parent_id NULLS FIRST, sort_order ASC, name ASC").
Limit(limit).
Offset(offset).
Find(&categories).Error
return categories, total, err
}
func (r *PurchaseCategoryRepositoryImpl) ExistsByCode(ctx context.Context, organizationID uuid.UUID, code string, excludeID *uuid.UUID) (bool, error) {
query := r.db.WithContext(ctx).
Model(&entities.PurchaseCategory{}).
Where("organization_id = ? AND code = ?", organizationID, code)
if excludeID != nil {
query = query.Where("id != ?", *excludeID)
}
var count int64
err := query.Count(&count).Error
return count > 0, err
}