Add License

This commit is contained in:
aditya.siregar
2024-07-28 13:00:01 +07:00
parent dfed117d90
commit 8966037024
11 changed files with 506 additions and 1 deletions
+71
View File
@@ -0,0 +1,71 @@
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) ([]*entity.License, int64, error) {
licenses, total, err := s.repo.GetAll(ctx, limit, offset)
if err != nil {
logger.ContextLogger(ctx).Error("error when getting all licenses", zap.Error(err))
return nil, 0, err
}
return licenses, total, nil
}
+11 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"furtuna-be/internal/common/mycontext"
"furtuna-be/internal/services/branch"
service "furtuna-be/internal/services/license"
"furtuna-be/internal/services/order"
"furtuna-be/internal/services/oss"
"furtuna-be/internal/services/partner"
@@ -32,6 +33,7 @@ type ServiceManagerImpl struct {
OSSSvc OSSService
PartnerSvc Partner
SiteSvc Site
LicenseSvc License
}
func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl) *ServiceManagerImpl {
@@ -46,7 +48,8 @@ func NewServiceManagerImpl(cfg *config.Config, repo *repository.RepoManagerImpl)
OSSSvc: oss.NewOSSService(repo.OSS),
PartnerSvc: partner.NewPartnerService(
repo.Partner, users.NewUserService(repo.User, repo.Branch), repo.Trx, repo.Wallet),
SiteSvc: site.NewSiteService(repo.Site),
SiteSvc: site.NewSiteService(repo.Site),
LicenseSvc: service.NewLicenseService(repo.License),
}
}
@@ -123,3 +126,10 @@ type Site interface {
GetAll(ctx context.Context, search entity.SiteSearch) ([]*entity.Site, int, error)
Delete(ctx mycontext.Context, id int64) error
}
type License interface {
Create(ctx mycontext.Context, licenseReq *entity.License) (*entity.License, error)
Update(ctx mycontext.Context, id string, licenseReq *entity.License) (*entity.License, error)
GetByID(ctx context.Context, id string) (*entity.License, error)
GetAll(ctx context.Context, limit, offset int) ([]*entity.License, int64, error)
}