95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/common/mycontext"
|
|
"enaklo-pos-be/internal/entity"
|
|
"enaklo-pos-be/internal/repository/models"
|
|
"github.com/pkg/errors"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type CategoryRepository interface {
|
|
Create(ctx mycontext.Context, category *entity.Category) (*entity.Category, error)
|
|
GetByPartnerID(ctx mycontext.Context, partnerID int64) ([]*entity.Category, error)
|
|
GetByID(ctx mycontext.Context, id int64) (*entity.Category, error)
|
|
Update(ctx mycontext.Context, category *entity.Category) error
|
|
Delete(ctx mycontext.Context, id int64) error
|
|
}
|
|
|
|
type categoryRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewCategoryRepository(db *gorm.DB) CategoryRepository {
|
|
return &categoryRepository{db: db}
|
|
}
|
|
|
|
func (r *categoryRepository) Create(ctx mycontext.Context, category *entity.Category) (*entity.Category, error) {
|
|
dbModel := &models.CategoryDB{
|
|
PartnerID: category.PartnerID,
|
|
Name: category.Name,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
if err := r.db.WithContext(ctx).Create(dbModel).Error; err != nil {
|
|
return nil, errors.Wrap(err, "failed to create category")
|
|
}
|
|
category.ID = dbModel.ID
|
|
return category, nil
|
|
}
|
|
|
|
func (r *categoryRepository) GetByPartnerID(ctx mycontext.Context, partnerID int64) ([]*entity.Category, error) {
|
|
var dbModels []models.CategoryDB
|
|
if err := r.db.WithContext(ctx).
|
|
Where("partner_id = ? AND deleted_at IS NULL", partnerID).
|
|
Find(&dbModels).Error; err != nil {
|
|
return nil, errors.Wrap(err, "failed to fetch categories by partner ID")
|
|
}
|
|
|
|
var result []*entity.Category
|
|
for _, db := range dbModels {
|
|
result = append(result, r.toEntity(&db))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (r *categoryRepository) GetByID(ctx mycontext.Context, id int64) (*entity.Category, error) {
|
|
var db models.CategoryDB
|
|
if err := r.db.WithContext(ctx).
|
|
Where("id = ? AND deleted_at IS NULL", id).
|
|
First(&db).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, errors.Wrap(err, "failed to get category by ID")
|
|
}
|
|
return r.toEntity(&db), nil
|
|
}
|
|
|
|
func (r *categoryRepository) Update(ctx mycontext.Context, category *entity.Category) error {
|
|
return r.db.WithContext(ctx).Model(&models.CategoryDB{}).
|
|
Where("id = ?", category.ID).
|
|
Updates(map[string]interface{}{
|
|
"name": category.Name,
|
|
"updated_at": time.Now(),
|
|
}).Error
|
|
}
|
|
|
|
func (r *categoryRepository) Delete(ctx mycontext.Context, id int64) error {
|
|
return r.db.WithContext(ctx).
|
|
Model(&models.CategoryDB{}).
|
|
Where("id = ?", id).
|
|
Update("deleted_at", time.Now()).Error
|
|
}
|
|
|
|
func (r *categoryRepository) toEntity(db *models.CategoryDB) *entity.Category {
|
|
return &entity.Category{
|
|
ID: db.ID,
|
|
PartnerID: db.PartnerID,
|
|
Name: db.Name,
|
|
CreatedAt: db.CreatedAt.Unix(),
|
|
UpdatedAt: db.UpdatedAt.Unix(),
|
|
}
|
|
}
|