Private
Public Access
feat: hecho filtro de busqueda para los logs
This commit is contained in:
1 parent
1c0da9d70f
commit
a25afa1750
12 files changed
+288
-57
No files matched your search
@@ -1 +1,45 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto "repuestera_back/dto/Logs"
|
||||
"repuestera_back/models/logs"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func BusquedaLogs(c *gin.Context) {
|
||||
pag, err := strconv.ParseUint(c.Param("p"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var filtros dto.BusquedaLogsDto
|
||||
if err := c.ShouldBindJSON(&filtros); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var logs []logs.Log
|
||||
|
||||
if logs, err = r.ObtenerRepositorios().Logs.ObtenerConFiltros(int(pag), filtros); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
logsdto := dto_builders.NewLogTablaBuilder().ConLogs(logs).Build()
|
||||
|
||||
cantpags, err := r.ObtenerRepositorios().Logs.CantPaginasConFiltros(filtros)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"logs": logsdto,
|
||||
"maxlenght": cantpags,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dto
|
||||
|
||||
import "time"
|
||||
|
||||
type BusquedaLogsDto struct {
|
||||
UsuarioID uint `json:"usuarioId"`
|
||||
Accion string `json:"accion"`
|
||||
FechaInicio time.Time `json:"fechaInicio"`
|
||||
FechaFin time.Time `json:"fechaFin"`
|
||||
}
|
||||
@@ -6,10 +6,11 @@ import (
|
||||
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
type LogTablaDto struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||
UsuarioID uint `gorm:"index;not null" json:"usuarioId"`
|
||||
Accion string `gorm:"type:varchar(50);not null" json:"accion"`
|
||||
Detalles datatypes.JSONSlice[logs.LogDetalle] `gorm:"type:json" json:"detalles,omitempty"`
|
||||
ID uint64 `json:"id"`
|
||||
UsuarioID uint `json:"usuarioId"`
|
||||
Accion string `json:"accion"`
|
||||
Detalles datatypes.JSONSlice[logs.LogDetalle] `json:"detalles,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package repositorios
|
||||
|
||||
import (
|
||||
dto "repuestera_back/dto/Logs"
|
||||
"repuestera_back/models/logs"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -10,6 +11,8 @@ 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 {
|
||||
@@ -41,3 +44,58 @@ func (r *repositorioLogs) CantPaginas() (int64, error) {
|
||||
}
|
||||
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.
|
||||
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
|
||||
}
|
||||
@@ -12,9 +12,9 @@ func LogsRoute(r *gin.Engine) {
|
||||
repuestoGrupo.Use(middleware.AuthMiddleware(), middleware.PermisoMiddleware(5))
|
||||
{
|
||||
repuestoGrupo.GET("/:p/", logs.ObtenerLogs)
|
||||
repuestoGrupo.POST("/:p/busqueda", logs.BusquedaLogs)
|
||||
|
||||
repuestoGrupo.GET("/:p/log", logs.ObtenerLog)
|
||||
// repuestoGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
||||
// repuestoGrupo.POST("/", repuestos.CrearRepuesto)
|
||||
// repuestoGrupo.PUT("/:id", repuestos.ActualizarRepuesto)
|
||||
// repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
||||
|
||||
Reference in new issue
Block a user