167 lines
4.2 KiB
Go
167 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
|
dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/uom/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/uom/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/uom/repository"
|
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UomService interface {
|
|
Create(ctx context.Context, req dtodomain.UomCreateRequest) (dtodomain.UomResponse, error)
|
|
GetById(ctx context.Context, uomId string) (dtodomain.UomResponse, error)
|
|
GetAll(ctx context.Context, filter query.UomFilter) ([]dtodomain.UomResponse, int64, error)
|
|
Update(ctx context.Context, req dtodomain.UomUpdateRequest, uomId string) (dtodomain.UomResponse, error)
|
|
Delete(ctx context.Context, uomId string) error
|
|
}
|
|
|
|
type uomService struct {
|
|
db *gorm.DB
|
|
uomRepo repository.UomRepository
|
|
}
|
|
|
|
func NewUomService(uomRepo repository.UomRepository, db *gorm.DB) UomService {
|
|
return &uomService{
|
|
uomRepo: uomRepo,
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (s *uomService) Create(ctx context.Context, req dtodomain.UomCreateRequest) (dtodomain.UomResponse, error) {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
uom := entities.MUomEntity{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
Symbol: req.Symbol,
|
|
Code: req.Code,
|
|
StdPrecision: req.StdPrecision,
|
|
IsActive: req.IsActive,
|
|
}
|
|
clientUUID, err := uuid.Parse(req.ClientID)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
uom.ClientID = clientUUID
|
|
created, err := s.uomRepo.Create(ctx, tx, uom)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
tx.Commit()
|
|
|
|
result, err := s.uomRepo.GetById(ctx, nil, created.ID.String())
|
|
if err != nil {
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
return toUomResponse(result), nil
|
|
}
|
|
|
|
func (s *uomService) GetById(ctx context.Context, uomId string) (dtodomain.UomResponse, error) {
|
|
uom, err := s.uomRepo.GetById(ctx, nil, uomId)
|
|
if err != nil {
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
return toUomResponse(uom), nil
|
|
}
|
|
|
|
func (s *uomService) GetAll(ctx context.Context, filter query.UomFilter) ([]dtodomain.UomResponse, int64, error) {
|
|
uoms, total, err := s.uomRepo.GetAll(ctx, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var responses []dtodomain.UomResponse
|
|
for _, u := range uoms {
|
|
responses = append(responses, toUomResponse(u))
|
|
}
|
|
if responses == nil {
|
|
responses = make([]dtodomain.UomResponse, 0)
|
|
}
|
|
return responses, total, nil
|
|
}
|
|
|
|
func (s *uomService) Update(ctx context.Context, req dtodomain.UomUpdateRequest, uomId string) (dtodomain.UomResponse, error) {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
uom, err := s.uomRepo.GetById(ctx, tx, uomId)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
uom.Name = req.Name
|
|
uom.Description = req.Description
|
|
uom.Symbol = req.Symbol
|
|
uom.Code = req.Code
|
|
uom.StdPrecision = req.StdPrecision
|
|
uom.IsActive = req.IsActive
|
|
if req.ClientID != "" {
|
|
clientUUID, err := uuid.Parse(req.ClientID)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
uom.ClientID = clientUUID
|
|
uom.Client = entities.M_Client{}
|
|
} else {
|
|
uom.Client = entities.M_Client{}
|
|
}
|
|
updated, err := s.uomRepo.Update(ctx, tx, uom)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
tx.Commit()
|
|
|
|
result, err := s.uomRepo.GetById(ctx, nil, updated.ID.String())
|
|
if err != nil {
|
|
return dtodomain.UomResponse{}, err
|
|
}
|
|
|
|
return toUomResponse(result), nil
|
|
}
|
|
|
|
func (s *uomService) Delete(ctx context.Context, uomId string) error {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
if err := s.uomRepo.Delete(ctx, tx, uomId); err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
tx.Commit()
|
|
return nil
|
|
}
|
|
|
|
func toUomResponse(e entities.MUomEntity) dtodomain.UomResponse {
|
|
return dtodomain.UomResponse{
|
|
ID: e.ID.String(),
|
|
Name: e.Name,
|
|
Description: e.Description,
|
|
Symbol: e.Symbol,
|
|
Code: e.Code,
|
|
StdPrecision: e.StdPrecision,
|
|
IsActive: e.IsActive,
|
|
Client: pkgdto.ClientResponse{
|
|
ID: e.Client.ID.String(),
|
|
Name: e.Client.Name,
|
|
},
|
|
}
|
|
}
|