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
+66
View File
@@ -0,0 +1,66 @@
package license
import (
"context"
"furtuna-be/internal/common/logger"
"furtuna-be/internal/entity"
"github.com/google/uuid"
"go.uber.org/zap"
"gorm.io/gorm"
)
type LicenseRepository struct {
db *gorm.DB
}
func NewLicenseRepository(db *gorm.DB) *LicenseRepository {
return &LicenseRepository{
db: db,
}
}
func (r *LicenseRepository) Create(ctx context.Context, license *entity.LicenseDB) (*entity.LicenseDB, error) {
if err := r.db.WithContext(ctx).Create(license).Error; err != nil {
logger.ContextLogger(ctx).Error("error when creating license", zap.Error(err))
return nil, err
}
return license, nil
}
func (r *LicenseRepository) Update(ctx context.Context, license *entity.LicenseDB) (*entity.LicenseDB, error) {
if err := r.db.WithContext(ctx).Save(license).Error; err != nil {
logger.ContextLogger(ctx).Error("error when updating license", zap.Error(err))
return nil, err
}
return license, nil
}
func (r *LicenseRepository) FindByID(ctx context.Context, id string) (*entity.LicenseDB, error) {
licenseID, err := uuid.Parse(id)
if err != nil {
return nil, err
}
license := new(entity.LicenseDB)
if err := r.db.WithContext(ctx).First(license, licenseID).Error; err != nil {
logger.ContextLogger(ctx).Error("error when finding license by ID", zap.Error(err))
return nil, err
}
return license, nil
}
func (r *LicenseRepository) GetAll(ctx context.Context, limit, offset int) ([]*entity.License, int64, error) {
var licenses []*entity.License
var total int64
if err := r.db.WithContext(ctx).
Limit(limit).
Offset(offset).
Find(&licenses).
Count(&total).
Error; err != nil {
return nil, 0, err
}
return licenses, total, nil
}
+10
View File
@@ -5,6 +5,7 @@ import (
"database/sql"
"furtuna-be/internal/repository/branches"
"furtuna-be/internal/repository/brevo"
"furtuna-be/internal/repository/license"
mdtrns "furtuna-be/internal/repository/midtrans"
"furtuna-be/internal/repository/orders"
"furtuna-be/internal/repository/oss"
@@ -43,6 +44,7 @@ type RepoManagerImpl struct {
Midtrans Midtrans
Payment Payment
EmailService EmailService
License License
}
func NewRepoManagerImpl(db *gorm.DB, cfg *config.Config) *RepoManagerImpl {
@@ -63,6 +65,7 @@ func NewRepoManagerImpl(db *gorm.DB, cfg *config.Config) *RepoManagerImpl {
Midtrans: mdtrns.New(&cfg.Midtrans),
Payment: payment.NewPaymentRepository(db),
EmailService: brevo.New(&cfg.Brevo),
License: license.NewLicenseRepository(db),
}
}
@@ -183,3 +186,10 @@ type Payment interface {
type EmailService interface {
SendEmailTransactional(ctx context.Context, param entity.SendEmailNotificationParam) error
}
type License interface {
Create(ctx context.Context, license *entity.LicenseDB) (*entity.LicenseDB, error)
Update(ctx context.Context, license *entity.LicenseDB) (*entity.LicenseDB, error)
FindByID(ctx context.Context, id string) (*entity.LicenseDB, error)
GetAll(ctx context.Context, limit, offset int) ([]*entity.License, int64, error)
}