195 lines
4.8 KiB
Go
195 lines
4.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/zona/repository"
|
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ZonaService interface {
|
|
Create(ctx context.Context, req dto.ZonaCreateRequest) (dto.ZonaResponse, error)
|
|
GetById(ctx context.Context, zonaId string) (dto.ZonaResponse, error)
|
|
GetAll(ctx context.Context, filter query.ZonaFilter) ([]dto.ZonaResponse, int64, error)
|
|
Update(ctx context.Context, req dto.ZonaUpdateRequest, zonaId string) (dto.ZonaResponse, error)
|
|
Delete(ctx context.Context, zonaId string) error
|
|
}
|
|
|
|
type zonaService struct {
|
|
db *gorm.DB
|
|
zonaRepo repository.ZonaRepository
|
|
}
|
|
|
|
func toZonaResponse(e entities.MZonaEntity) dto.ZonaResponse {
|
|
|
|
warehouse := pkgdto.IdNameResponse{}
|
|
if e.Warehouse.ID != uuid.Nil {
|
|
warehouse = pkgdto.IdNameResponse{
|
|
ID: e.Warehouse.ID.String(),
|
|
Name: e.Warehouse.Name,
|
|
}
|
|
}
|
|
|
|
client := pkgdto.IdNameResponse{}
|
|
if e.Client.ID != uuid.Nil {
|
|
client = pkgdto.IdNameResponse{
|
|
ID: e.Client.ID.String(),
|
|
Name: e.Client.Name,
|
|
}
|
|
}
|
|
|
|
return dto.ZonaResponse{
|
|
ID: e.ID.String(),
|
|
Code: e.Code,
|
|
Name: e.Name,
|
|
Type: e.Type,
|
|
Temperature: e.Temperature,
|
|
Hazardous: e.Hazardous,
|
|
QRCodeZone: e.QRCodeZone,
|
|
IsActive: e.IsActive,
|
|
Warehouse: warehouse,
|
|
Client: client,
|
|
}
|
|
}
|
|
|
|
func (s *zonaService) Create(ctx context.Context, req dto.ZonaCreateRequest) (dto.ZonaResponse, error) {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
warehouseUUID, err := uuid.Parse(req.WarehouseID)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
clientUUID, err := uuid.Parse(req.ClientID)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
zona := entities.MZonaEntity{
|
|
Code: req.Code,
|
|
Name: req.Name,
|
|
Type: req.Type,
|
|
Temperature: req.Temperature,
|
|
Hazardous: req.Hazardous,
|
|
// QRCodeZone: req.QRCodeZone,
|
|
// IsActive: req.IsActive,
|
|
WarehouseID: warehouseUUID,
|
|
ClientID: clientUUID,
|
|
}
|
|
created, err := s.zonaRepo.Create(ctx, tx, zona)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
tx.Commit()
|
|
result, err := s.zonaRepo.GetById(ctx, nil, created.ID.String())
|
|
if err != nil {
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
return toZonaResponse(result), nil
|
|
}
|
|
|
|
func (s *zonaService) GetById(ctx context.Context, zonaId string) (dto.ZonaResponse, error) {
|
|
zona, err := s.zonaRepo.GetById(ctx, nil, zonaId)
|
|
if err != nil {
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
return toZonaResponse(zona), nil
|
|
}
|
|
|
|
func (s *zonaService) GetAll(ctx context.Context, filter query.ZonaFilter) ([]dto.ZonaResponse, int64, error) {
|
|
zonas, total, err := s.zonaRepo.GetAll(ctx, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var responses []dto.ZonaResponse
|
|
for _, e := range zonas {
|
|
responses = append(responses, toZonaResponse(e))
|
|
}
|
|
if responses == nil {
|
|
responses = make([]dto.ZonaResponse, 0)
|
|
}
|
|
return responses, total, nil
|
|
}
|
|
|
|
func (s *zonaService) Update(ctx context.Context, req dto.ZonaUpdateRequest, zonaId string) (dto.ZonaResponse, error) {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
zona, err := s.zonaRepo.GetById(ctx, tx, zonaId)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
if req.Code != "" {
|
|
zona.Code = req.Code
|
|
}
|
|
if req.Name != "" {
|
|
zona.Name = req.Name
|
|
}
|
|
if req.Type != "" {
|
|
zona.Type = req.Type
|
|
}
|
|
zona.Temperature = req.Temperature
|
|
zona.Hazardous = req.Hazardous
|
|
// zona.QRCodeZone = req.QRCodeZone
|
|
zona.IsActive = req.IsActive
|
|
// if req.WarehouseID != "" {
|
|
// warehouseUUID, err := uuid.Parse(req.WarehouseID)
|
|
// if err == nil {
|
|
// zona.WarehouseID = warehouseUUID
|
|
// }
|
|
// }
|
|
// if req.ClientID != "" {
|
|
// clientUUID, err := uuid.Parse(req.ClientID)
|
|
// if err == nil {
|
|
// zona.ClientID = clientUUID
|
|
// }
|
|
// }
|
|
updated, err := s.zonaRepo.Update(ctx, tx, zona)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
tx.Commit()
|
|
result, err := s.zonaRepo.GetById(ctx, nil, updated.ID.String())
|
|
if err != nil {
|
|
return dto.ZonaResponse{}, err
|
|
}
|
|
return toZonaResponse(result), nil
|
|
}
|
|
|
|
func (s *zonaService) Delete(ctx context.Context, zonaId string) error {
|
|
tx := s.db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
if err := s.zonaRepo.Delete(ctx, tx, zonaId); err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
tx.Commit()
|
|
return nil
|
|
}
|
|
|
|
func NewZonaService(zonaRepo repository.ZonaRepository, db *gorm.DB) ZonaService {
|
|
return &zonaService{
|
|
zonaRepo: zonaRepo,
|
|
db: db,
|
|
}
|
|
}
|