Private
Public Access
114 lines
2.5 KiB
Go
114 lines
2.5 KiB
Go
package repositorios
|
|
|
|
import (
|
|
dto "repuestera_back/dto/Logs"
|
|
"repuestera_back/models/logs"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type RepositorioLogs interface {
|
|
IRepositorio[*logs.Log]
|
|
ObtenerPorPaginas(pag uint) ([]logs.Log, error)
|
|
CantPaginas() (int64, error)
|
|
ObtenerConFiltros(p int, f dto.BusquedaLogsDto) ([]logs.Log, error)
|
|
CantPaginasConFiltros(f dto.BusquedaLogsDto) (int64, error)
|
|
}
|
|
|
|
type repositorioLogs struct {
|
|
*RepositorioBase[*logs.Log]
|
|
db *gorm.DB
|
|
empresaID uint
|
|
}
|
|
|
|
func NuevoRepositorioLogs(db *gorm.DB, empresaID uint) RepositorioLogs {
|
|
baseRepo := NewRepositorioBase[*logs.Log](db, empresaID)
|
|
return &repositorioLogs{
|
|
RepositorioBase: baseRepo,
|
|
db: dbConEmpresa(db, empresaID),
|
|
empresaID: empresaID,
|
|
}
|
|
}
|
|
|
|
// Crear persiste el log directamente, sin auditar la creación del propio log.
|
|
func (r *repositorioLogs) Crear(entidad *logs.Log, usuarioID uint) error {
|
|
if r.empresaID > 0 && entidad.EmpresaID == 0 {
|
|
entidad.EmpresaID = r.empresaID
|
|
}
|
|
return r.db.Create(entidad).Error
|
|
}
|
|
|
|
func (r *repositorioLogs) ObtenerPorPaginas(pag uint) ([]logs.Log, error) {
|
|
var logs []logs.Log
|
|
if err := r.db.Preload("Usuario").Order("id DESC").Limit(10).Offset(int(pag) * 10).Find(&logs).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return logs, nil
|
|
}
|
|
|
|
func (r *repositorioLogs) CantPaginas() (int64, error) {
|
|
var pags int64
|
|
err := r.db.Model(&logs.Log{}).Count(&pags).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return pags, nil
|
|
}
|
|
|
|
func (r *repositorioLogs) ObtenerConFiltros(p int, f dto.BusquedaLogsDto) ([]logs.Log, error) {
|
|
var logss []logs.Log
|
|
|
|
db := r.db.Model(&logs.Log{})
|
|
|
|
if f.UsuarioID != 0 {
|
|
db = db.Where("usuario_id = ?", f.UsuarioID)
|
|
}
|
|
|
|
if f.Accion != "" {
|
|
db = db.Where("accion LIKE ?", "%"+f.Accion+"%")
|
|
}
|
|
|
|
if !f.FechaInicio.IsZero() {
|
|
db = db.Where("created_at >= ?", f.FechaInicio)
|
|
}
|
|
|
|
if !f.FechaFin.IsZero() {
|
|
db = db.Where("created_at <= ?", f.FechaFin)
|
|
}
|
|
|
|
err := db.
|
|
Preload("Usuario").
|
|
Order("id DESC").
|
|
Limit(10).
|
|
Offset(10 * p).
|
|
Find(&logss).Error
|
|
|
|
return logss, err
|
|
}
|
|
|
|
func (r *repositorioLogs) CantPaginasConFiltros(f dto.BusquedaLogsDto) (int64, error) {
|
|
var cantpags int64
|
|
|
|
db := r.db.Model(&logs.Log{})
|
|
|
|
if f.UsuarioID != 0 {
|
|
db = db.Where("usuario_id = ?", f.UsuarioID)
|
|
}
|
|
|
|
if f.Accion != "" {
|
|
db = db.Where("accion LIKE ?", "%"+f.Accion+"%")
|
|
}
|
|
|
|
if !f.FechaInicio.IsZero() {
|
|
db = db.Where("created_at >= ?", f.FechaInicio)
|
|
}
|
|
|
|
if !f.FechaFin.IsZero() {
|
|
db = db.Where("created_at <= ?", f.FechaFin)
|
|
}
|
|
|
|
err := db.Count(&cantpags).Error
|
|
|
|
return cantpags, err
|
|
}
|