92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
|
|
package branches
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"furtuna-be/internal/common/logger"
|
||
|
|
"furtuna-be/internal/entity"
|
||
|
|
|
||
|
|
"go.uber.org/zap"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
type BranchRepository struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewBranchRepository(db *gorm.DB) *BranchRepository {
|
||
|
|
return &BranchRepository{
|
||
|
|
db: db,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *BranchRepository) CreateBranch(ctx context.Context, branch *entity.BranchDB) (*entity.BranchDB, error) {
|
||
|
|
err := b.db.Create(branch).Error
|
||
|
|
if err != nil {
|
||
|
|
logger.ContextLogger(ctx).Error("error when create branch", zap.Error(err))
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return branch, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *BranchRepository) UpdateBranch(ctx context.Context, branch *entity.BranchDB) (*entity.BranchDB, error) {
|
||
|
|
if err := b.db.Save(branch).Error; err != nil {
|
||
|
|
logger.ContextLogger(ctx).Error("error when update branch", zap.Error(err))
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return branch, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *BranchRepository) GetBranchByID(ctx context.Context, id int64) (*entity.BranchDB, error) {
|
||
|
|
branch := new(entity.BranchDB)
|
||
|
|
if err := b.db.First(branch, id).Error; err != nil {
|
||
|
|
logger.ContextLogger(ctx).Error("error when get by id branch", zap.Error(err))
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return branch, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *BranchRepository) GetAllBranches(ctx context.Context, req entity.BranchSearch) (entity.BranchList, int, error) {
|
||
|
|
var branches []*entity.BranchDB
|
||
|
|
var total int64
|
||
|
|
|
||
|
|
query := b.db
|
||
|
|
query = query.Where("deleted_at is null")
|
||
|
|
|
||
|
|
if req.Search != "" {
|
||
|
|
query = query.Where("name ILIKE ?", "%"+req.Search+"%")
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Name != "" {
|
||
|
|
query = query.Where("name ILIKE ?", "%"+req.Name+"%")
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Limit > 0 {
|
||
|
|
query = query.Limit(req.Limit)
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Offset > 0 {
|
||
|
|
query = query.Offset(req.Offset)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := query.Find(&branches).Error; err != nil {
|
||
|
|
logger.ContextLogger(ctx).Error("error when get all branches", zap.Error(err))
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := b.db.Model(&entity.BranchDB{}).Where(query).Count(&total).Error; err != nil {
|
||
|
|
logger.ContextLogger(ctx).Error("error when count branches", zap.Error(err))
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return branches, int(total), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (b *BranchRepository) DeleteBranch(ctx context.Context, id int64) error {
|
||
|
|
branch := new(entity.BranchDB)
|
||
|
|
branch.ID = id
|
||
|
|
if err := b.db.Delete(branch).Error; err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|