Private
Public Access
cocinado cu-01
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
31f8b493d0
commit
3c4bc5c008
15 files changed
+510
-32
No files matched your search
@@ -0,0 +1,17 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type DecoradorCliente struct {
|
||||
envuelto IFacturaQuery
|
||||
cliente string
|
||||
}
|
||||
|
||||
func (d DecoradorCliente) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := d.envuelto.Aplicar(db)
|
||||
if d.cliente != "" {
|
||||
consulta = consulta.Joins("JOIN clientes ON clientes.id = factura.`ClienteID`").
|
||||
Where("clientes.nombre LIKE ?", "%"+d.cliente+"%")
|
||||
}
|
||||
return consulta
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type DecoradorClienteID struct {
|
||||
envuelto IFacturaQuery
|
||||
clienteID uint
|
||||
}
|
||||
|
||||
func (d DecoradorClienteID) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := d.envuelto.Aplicar(db)
|
||||
if d.clienteID > 0 {
|
||||
consulta = consulta.Where("`ClienteID` = ?", d.clienteID)
|
||||
}
|
||||
return consulta
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DecoradorDesde struct {
|
||||
envuelto IFacturaQuery
|
||||
desde *time.Time
|
||||
}
|
||||
|
||||
func (d DecoradorDesde) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := d.envuelto.Aplicar(db)
|
||||
if d.desde != nil {
|
||||
consulta = consulta.Where("factura.created_at >= ?", *d.desde)
|
||||
}
|
||||
return consulta
|
||||
}
|
||||
|
||||
type DecoradorHasta struct {
|
||||
envuelto IFacturaQuery
|
||||
hasta *time.Time
|
||||
}
|
||||
|
||||
func (d DecoradorHasta) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := d.envuelto.Aplicar(db)
|
||||
if d.hasta != nil {
|
||||
consulta = consulta.Where("factura.created_at < ?", d.hasta.AddDate(0, 0, 1))
|
||||
}
|
||||
return consulta
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type DecoradorNumero struct {
|
||||
envuelto IFacturaQuery
|
||||
numero string
|
||||
}
|
||||
|
||||
func (d DecoradorNumero) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := d.envuelto.Aplicar(db)
|
||||
if d.numero != "" {
|
||||
consulta = consulta.Where("numero LIKE ?", "%"+d.numero+"%")
|
||||
}
|
||||
return consulta
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type DecoradorPaginacion struct {
|
||||
envuelto IFacturaQuery
|
||||
pagina int
|
||||
porPagina int
|
||||
}
|
||||
|
||||
func (d DecoradorPaginacion) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := d.envuelto.Aplicar(db)
|
||||
pagina, porPagina := NormalizarPagina(d.pagina, d.porPagina)
|
||||
consulta = consulta.Order("factura.created_at DESC, factura.id DESC").
|
||||
Offset((pagina - 1) * porPagina).
|
||||
Limit(porPagina)
|
||||
return consulta
|
||||
}
|
||||
|
||||
func NormalizarPagina(pagina, porPagina int) (int, int) {
|
||||
if pagina < 1 {
|
||||
pagina = 1
|
||||
}
|
||||
if porPagina < 1 {
|
||||
porPagina = 50
|
||||
}
|
||||
if porPagina > 200 {
|
||||
porPagina = 200
|
||||
}
|
||||
return pagina, porPagina
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import "time"
|
||||
|
||||
// FiltrosFactura acota la consulta del listado de facturas.
|
||||
type FiltrosFactura struct {
|
||||
Numero string
|
||||
ClienteID uint
|
||||
Cliente string
|
||||
Desde *time.Time
|
||||
Hasta *time.Time
|
||||
Pagina int
|
||||
PorPagina int
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import (
|
||||
"repuestera_back/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// IFacturaQuery es el componente base del patrón Decorator para facturas.
|
||||
type IFacturaQuery interface {
|
||||
Aplicar(db *gorm.DB) *gorm.DB
|
||||
}
|
||||
|
||||
// FacturaQueryBase es la implementación base de la cadena.
|
||||
type FacturaQueryBase struct {
|
||||
Preload bool
|
||||
}
|
||||
|
||||
func (q FacturaQueryBase) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := db.Model(&models.Factura{})
|
||||
if q.Preload {
|
||||
consulta = consulta.Preload("Cliente")
|
||||
}
|
||||
return consulta
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package repositorios_decorator
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// NuevaQueryFacturas arma la cadena de decoradores según los filtros.
|
||||
func NuevaQueryFacturas(filtros FiltrosFactura, preload bool, paginar bool) IFacturaQuery {
|
||||
var actual IFacturaQuery = FacturaQueryBase{Preload: preload}
|
||||
|
||||
actual = DecoradorNumero{envuelto: actual, numero: filtros.Numero}
|
||||
actual = DecoradorClienteID{envuelto: actual, clienteID: filtros.ClienteID}
|
||||
actual = DecoradorCliente{envuelto: actual, cliente: filtros.Cliente}
|
||||
actual = DecoradorDesde{envuelto: actual, desde: filtros.Desde}
|
||||
actual = DecoradorHasta{envuelto: actual, hasta: filtros.Hasta}
|
||||
if paginar {
|
||||
actual = DecoradorPaginacion{envuelto: actual, pagina: filtros.Pagina, porPagina: filtros.PorPagina}
|
||||
} else {
|
||||
actual = decoradorOrdenFacturas{envuelto: actual}
|
||||
}
|
||||
|
||||
return actual
|
||||
}
|
||||
|
||||
// decoradorOrdenFacturas aplica el orden por defecto cuando no se pagina.
|
||||
type decoradorOrdenFacturas struct {
|
||||
envuelto IFacturaQuery
|
||||
}
|
||||
|
||||
func (d decoradorOrdenFacturas) Aplicar(db *gorm.DB) *gorm.DB {
|
||||
consulta := d.envuelto.Aplicar(db)
|
||||
return consulta.Order("factura.created_at DESC")
|
||||
}
|
||||
@@ -1,24 +1,15 @@
|
||||
package repositorios
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"repuestera_back/models"
|
||||
decorator "repuestera_back/repositorios/Decorator/facturas"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FiltrosFactura struct {
|
||||
Numero string
|
||||
ClienteID uint
|
||||
Cliente string
|
||||
Desde *time.Time
|
||||
Hasta *time.Time
|
||||
}
|
||||
|
||||
type RepositorioFactura interface {
|
||||
IRepositorio[models.Factura]
|
||||
Listar(filtros FiltrosFactura) ([]models.Factura, error)
|
||||
Listar(filtros decorator.FiltrosFactura) ([]models.Factura, error)
|
||||
ObtenerConRelaciones(id uint) (*models.Factura, error)
|
||||
}
|
||||
|
||||
@@ -39,25 +30,10 @@ func NuevoRepositorioFactura(db *gorm.DB, empresaID uint) RepositorioFactura {
|
||||
func (r *repositorioFactura) Listar(filtros FiltrosFactura) ([]models.Factura, error) {
|
||||
var facturas []models.Factura
|
||||
|
||||
query := r.db.Model(&models.Factura{}).Preload("Cliente")
|
||||
if filtros.Numero != "" {
|
||||
query = query.Where("numero LIKE ?", "%"+filtros.Numero+"%")
|
||||
}
|
||||
if filtros.ClienteID > 0 {
|
||||
query = query.Where("`ClienteID` = ?", filtros.ClienteID)
|
||||
}
|
||||
if filtros.Cliente != "" {
|
||||
query = query.Joins("JOIN clientes ON clientes.id = factura.`ClienteID`").
|
||||
Where("clientes.nombre LIKE ?", "%"+filtros.Cliente+"%")
|
||||
}
|
||||
if filtros.Desde != nil {
|
||||
query = query.Where("factura.created_at >= ?", *filtros.Desde)
|
||||
}
|
||||
if filtros.Hasta != nil {
|
||||
query = query.Where("factura.created_at < ?", filtros.Hasta.AddDate(0, 0, 1))
|
||||
}
|
||||
paginar := filtros.Pagina > 0 || filtros.PorPagina > 0
|
||||
query := decorator.NuevaQueryFacturas(filtros, true, paginar).Aplicar(r.db)
|
||||
|
||||
if err := query.Order("factura.created_at DESC").Find(&facturas).Error; err != nil {
|
||||
if err := query.Find(&facturas).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return facturas, nil
|
||||
|
||||
Reference in new issue
Block a user