71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package studio
|
|
|
|
import (
|
|
"context"
|
|
"enaklo-pos-be/internal/common/logger"
|
|
"enaklo-pos-be/internal/common/mycontext"
|
|
"enaklo-pos-be/internal/entity"
|
|
"enaklo-pos-be/internal/repository"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type StudioService struct {
|
|
repo repository.Studio
|
|
}
|
|
|
|
func NewStudioService(repo repository.Studio) *StudioService {
|
|
return &StudioService{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (s *StudioService) Create(ctx mycontext.Context, studioReq *entity.Studio) (*entity.Studio, error) {
|
|
newStudioDB := studioReq.NewStudiosDB()
|
|
newStudioDB.CreatedBy = ctx.RequestedBy()
|
|
|
|
newStudioDB, err := s.repo.CreateStudio(ctx, newStudioDB)
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when creating studio", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return newStudioDB.ToStudio(), nil
|
|
}
|
|
|
|
func (s *StudioService) Update(ctx mycontext.Context, id int64, studioReq *entity.Studio) (*entity.Studio, error) {
|
|
existingStudio, err := s.repo.GetStudioByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
existingStudio.ToUpdatedStudio(ctx.RequestedBy(), *studioReq)
|
|
|
|
updatedStudioDB, err := s.repo.UpdateStudio(ctx, existingStudio.ToStudioDB())
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when updating studio", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return updatedStudioDB.ToStudio(), nil
|
|
}
|
|
|
|
func (s *StudioService) GetByID(ctx context.Context, id int64) (*entity.Studio, error) {
|
|
studioDB, err := s.repo.GetStudioByID(ctx, id)
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when getting studio by id", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return studioDB.ToStudio(), nil
|
|
}
|
|
|
|
func (s *StudioService) Search(ctx context.Context, search entity.StudioSearch) ([]*entity.Studio, int, error) {
|
|
studios, total, err := s.repo.SearchStudios(ctx, search)
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when getting all studios", zap.Error(err))
|
|
return nil, 0, err
|
|
}
|
|
|
|
return studios.ToStudioList(), total, nil
|
|
}
|