Private
Public Access
version pre testeo de descuentos
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
a9ec514687
commit
ea009e276f
25 files changed
+1100
-13
No files matched your search
@@ -0,0 +1,84 @@
|
||||
package dto_builders
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
dto "repuestera_back/dto/Descuentos"
|
||||
"repuestera_back/models"
|
||||
)
|
||||
|
||||
type DescuentoBuilder struct {
|
||||
dto *dto.DescuentoDto
|
||||
activoPorDefecto bool
|
||||
err error
|
||||
}
|
||||
|
||||
var _ IBuilder[models.Descuento] = (*DescuentoBuilder)(nil)
|
||||
|
||||
func NewDescuentoBuilder() *DescuentoBuilder {
|
||||
return &DescuentoBuilder{activoPorDefecto: true}
|
||||
}
|
||||
|
||||
func (b *DescuentoBuilder) ConDto(data dto.DescuentoDto) *DescuentoBuilder {
|
||||
b.dto = &data
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DescuentoBuilder) ConActivoPorDefecto(activo bool) *DescuentoBuilder {
|
||||
b.activoPorDefecto = activo
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DescuentoBuilder) Build() (*models.Descuento, error) {
|
||||
if b.dto == nil {
|
||||
return nil, errors.New("faltan los datos del descuento")
|
||||
}
|
||||
|
||||
nombre, ok := ValidarTexto(b.dto.Nombre, "El nombre", &b.err)
|
||||
if ok {
|
||||
if len([]rune(nombre)) > 100 {
|
||||
AcumularError(&b.err, "El nombre no puede superar los 100 caracteres")
|
||||
} else {
|
||||
b.dto.Nombre = nombre
|
||||
}
|
||||
}
|
||||
|
||||
descripcion := strings.TrimSpace(b.dto.Descripcion)
|
||||
if len([]rune(descripcion)) > 500 {
|
||||
AcumularError(&b.err, "La descripción no puede superar los 500 caracteres")
|
||||
} else {
|
||||
b.dto.Descripcion = descripcion
|
||||
}
|
||||
|
||||
tipo := models.TipoDescuento(strings.ToUpper(strings.TrimSpace(b.dto.Tipo)))
|
||||
if !tipo.Valido() {
|
||||
AcumularError(&b.err, "El tipo debe ser PORCENTAJE o FIJO")
|
||||
}
|
||||
|
||||
valor := math.Round(b.dto.Valor*100) / 100
|
||||
if valor <= 0 {
|
||||
AcumularError(&b.err, "El valor debe ser mayor a cero")
|
||||
}
|
||||
if tipo.Valido() && tipo == models.Porcentaje && valor > 100 {
|
||||
AcumularError(&b.err, "El porcentaje no puede superar el 100%")
|
||||
}
|
||||
|
||||
if b.err != nil {
|
||||
return nil, b.err
|
||||
}
|
||||
|
||||
activo := b.activoPorDefecto
|
||||
if b.dto.Activo != nil {
|
||||
activo = *b.dto.Activo
|
||||
}
|
||||
|
||||
return &models.Descuento{
|
||||
Nombre: b.dto.Nombre,
|
||||
Descripcion: b.dto.Descripcion,
|
||||
Tipo: tipo,
|
||||
Valor: valor,
|
||||
Activo: activo,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package directors
|
||||
|
||||
import (
|
||||
builders "repuestera_back/dto/Builders"
|
||||
dto "repuestera_back/dto/Descuentos"
|
||||
"repuestera_back/models"
|
||||
)
|
||||
|
||||
type DescuentoDirector struct{}
|
||||
|
||||
func NewDescuentoDirector() *DescuentoDirector {
|
||||
return &DescuentoDirector{}
|
||||
}
|
||||
|
||||
func (d *DescuentoDirector) Crear(data dto.DescuentoDto) (*models.Descuento, error) {
|
||||
return builders.NewDescuentoBuilder().
|
||||
ConDto(data).
|
||||
ConActivoPorDefecto(true).
|
||||
Build()
|
||||
}
|
||||
|
||||
func (d *DescuentoDirector) Actualizar(data dto.DescuentoDto, activoPorDefecto bool) (*models.Descuento, error) {
|
||||
return builders.NewDescuentoBuilder().
|
||||
ConDto(data).
|
||||
ConActivoPorDefecto(activoPorDefecto).
|
||||
Build()
|
||||
}
|
||||
Reference in new issue
Block a user