Add License
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user