138 lines
3.9 KiB
Go
138 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
|
dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/att_set/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/att_set/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/att_set/repository"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
|
"github.com/google/uuid"
|
|
"github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AttSetService interface {
|
|
Create(ctx context.Context, req dtodomain.AttSetCreateRequest) (dtodomain.AttSetResponse, error)
|
|
GetById(ctx context.Context, attSetId string) (dtodomain.AttSetResponse, error)
|
|
GetAll(ctx context.Context, filter query.AttSetFilter) ([]dtodomain.AttSetResponse, int64, error)
|
|
Update(ctx context.Context, req dtodomain.AttSetUpdateRequest, attSetId string) (dtodomain.AttSetResponse, error)
|
|
Delete(ctx context.Context, attSetId string) error
|
|
}
|
|
|
|
type attSetService struct {
|
|
db *gorm.DB
|
|
attSetRepo repository.AttSetRepository
|
|
log *logrus.Logger
|
|
}
|
|
|
|
func NewAttSetService(attSetRepo repository.AttSetRepository, db *gorm.DB, log *logrus.Logger) AttSetService {
|
|
return &attSetService{
|
|
db: db,
|
|
attSetRepo: attSetRepo,
|
|
log: log,
|
|
}
|
|
}
|
|
|
|
func (s *attSetService) Create(ctx context.Context, req dtodomain.AttSetCreateRequest) (dtodomain.AttSetResponse, error) {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
clientUUID, err := uuid.Parse(req.ClientID)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.AttSetResponse{}, err
|
|
}
|
|
attSet := entities.TAttributSetInstanceEntity{
|
|
AttributSetInstance: req.AttributSetInstance,
|
|
DateMaterialPolicy: utils.StringToDate(req.DateMaterialPolicy),
|
|
TransactionType: req.TransactionType,
|
|
ClientID: clientUUID,
|
|
}
|
|
created, err := s.attSetRepo.Create(ctx, tx, attSet)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.AttSetResponse{}, err
|
|
}
|
|
tx.Commit()
|
|
resp := dtodomain.ToAttSetResponse(created)
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *attSetService) GetById(ctx context.Context, attSetId string) (dtodomain.AttSetResponse, error) {
|
|
attSet, err := s.attSetRepo.GetById(ctx, nil, attSetId)
|
|
if err != nil {
|
|
return dtodomain.AttSetResponse{}, err
|
|
}
|
|
resp := dtodomain.ToAttSetResponse(attSet)
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *attSetService) GetAll(ctx context.Context, filter query.AttSetFilter) ([]dtodomain.AttSetResponse, int64, error) {
|
|
attSets, count, err := s.attSetRepo.GetAll(ctx, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var resp []dtodomain.AttSetResponse
|
|
for _, attSet := range attSets {
|
|
resp = append(resp, dtodomain.ToAttSetResponse(attSet))
|
|
}
|
|
return resp, count, nil
|
|
}
|
|
|
|
func (s *attSetService) Update(ctx context.Context, req dtodomain.AttSetUpdateRequest, attSetId string) (dtodomain.AttSetResponse, error) {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
attSet, err := s.attSetRepo.GetById(ctx, tx, attSetId)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.AttSetResponse{}, err
|
|
}
|
|
if req.AttributSetInstance != "" {
|
|
attSet.AttributSetInstance = req.AttributSetInstance
|
|
}
|
|
if req.DateMaterialPolicy != "" {
|
|
attSet.DateMaterialPolicy = utils.StringToDate(req.DateMaterialPolicy)
|
|
}
|
|
if req.TransactionType != "" {
|
|
attSet.TransactionType = req.TransactionType
|
|
}
|
|
if req.ClientID != "" {
|
|
clientUUID, err := uuid.Parse(req.ClientID)
|
|
if err == nil {
|
|
attSet.ClientID = clientUUID
|
|
}
|
|
}
|
|
updated, err := s.attSetRepo.Update(ctx, tx, attSet)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.AttSetResponse{}, err
|
|
}
|
|
tx.Commit()
|
|
resp := dtodomain.ToAttSetResponse(updated)
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *attSetService) Delete(ctx context.Context, attSetId string) error {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
if err := s.attSetRepo.Delete(ctx, tx, attSetId); err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
tx.Commit()
|
|
return nil
|
|
}
|