72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package service
|
|
|
|
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 LicenseService struct {
|
|
repo repository.License
|
|
}
|
|
|
|
func NewLicenseService(repo repository.License) *LicenseService {
|
|
return &LicenseService{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (s *LicenseService) Create(ctx mycontext.Context, licenseReq *entity.License) (*entity.License, error) {
|
|
licenseDB := licenseReq.ToLicenseDB()
|
|
licenseDB.CreatedBy = ctx.RequestedBy()
|
|
licenseDB.UpdatedBy = ctx.RequestedBy()
|
|
|
|
licenseDB, err := s.repo.Create(ctx, licenseDB)
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when create license", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return licenseDB.ToLicense(), nil
|
|
}
|
|
|
|
func (s *LicenseService) Update(ctx mycontext.Context, id string, licenseReq *entity.License) (*entity.License, error) {
|
|
existingLicense, err := s.repo.FindByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
existingLicense.ToUpdatedLicense(ctx.RequestedBy(), *licenseReq)
|
|
|
|
updatedLicenseDB, err := s.repo.Update(ctx, existingLicense)
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when update license", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return updatedLicenseDB.ToLicense(), nil
|
|
}
|
|
|
|
func (s *LicenseService) GetByID(ctx context.Context, id string) (*entity.License, error) {
|
|
licenseDB, err := s.repo.FindByID(ctx, id)
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when get license by id", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return licenseDB.ToLicense(), nil
|
|
}
|
|
|
|
func (s *LicenseService) GetAll(ctx context.Context, limit, offset int, status string) ([]*entity.LicenseGetAll, int64, error) {
|
|
licenses, total, err := s.repo.GetAll(ctx, limit, offset, status)
|
|
if err != nil {
|
|
logger.ContextLogger(ctx).Error("error when getting all licenses", zap.Error(err))
|
|
return nil, 0, err
|
|
}
|
|
return licenses, total, nil
|
|
}
|