90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package studios
|
|
|
|
import (
|
|
"context"
|
|
"enaklo-pos-be/internal/common/logger"
|
|
"enaklo-pos-be/internal/entity"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type StudioRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewStudioRepository(db *gorm.DB) *StudioRepository {
|
|
return &StudioRepository{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (s *StudioRepository) CreateStudio(ctx context.Context, studio *entity.StudioDB) (*entity.StudioDB, error) {
|
|
err := s.db.Omit("ID").Create(studio).Error
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when creating studio", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return studio, nil
|
|
}
|
|
|
|
func (s *StudioRepository) UpdateStudio(ctx context.Context, studio *entity.StudioDB) (*entity.StudioDB, error) {
|
|
if err := s.db.Save(studio).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when updating studio", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return studio, nil
|
|
}
|
|
|
|
func (s *StudioRepository) GetStudioByID(ctx context.Context, id int64) (*entity.StudioDB, error) {
|
|
studio := new(entity.StudioDB)
|
|
if err := s.db.First(studio, id).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when getting studio by ID", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return studio, nil
|
|
}
|
|
|
|
func (s *StudioRepository) SearchStudios(ctx context.Context, req entity.StudioSearch) (entity.StudioList, int, error) {
|
|
var studios []*entity.StudioDB
|
|
var total int64
|
|
|
|
query := s.db
|
|
|
|
if req.Id > 0 {
|
|
query = query.Where("id = ?", req.Id)
|
|
}
|
|
|
|
if req.Name != "" {
|
|
query = query.Where("name ILIKE ?", "%"+req.Name+"%")
|
|
}
|
|
|
|
if req.Status != "" {
|
|
query = query.Where("status = ?", req.Status)
|
|
}
|
|
|
|
if req.BranchId > 0 {
|
|
query = query.Where("branch_id = ?", req.BranchId)
|
|
}
|
|
|
|
if req.Limit > 0 {
|
|
query = query.Limit(req.Limit)
|
|
}
|
|
|
|
if req.Offset > 0 {
|
|
query = query.Offset(req.Offset)
|
|
}
|
|
|
|
if err := query.Find(&studios).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when getting all studios", zap.Error(err))
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := s.db.Model(&entity.StudioDB{}).Where(query).Count(&total).Error; err != nil {
|
|
logger.ContextLogger(ctx).Error("error when counting studios", zap.Error(err))
|
|
return nil, 0, err
|
|
}
|
|
|
|
return studios, int(total), nil
|
|
}
|