init project
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package studio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"furtuna-be/internal/common/logger"
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/entity"
|
||||
"furtuna-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
|
||||
}
|
||||
Reference in New Issue
Block a user