Private
Public Access
hecha migracion de hooks a un builder de logs
This commit is contained in:
1 parent
5f741b3cc7
commit
38afdcc742
15 files changed
+402
-274
No files matched your search
@@ -1,7 +1,11 @@
|
||||
package repositorios
|
||||
|
||||
import (
|
||||
"repuestera_back/logs"
|
||||
applog "log"
|
||||
"reflect"
|
||||
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
models_logs "repuestera_back/models/logs"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -13,6 +17,7 @@ type IRepositorio[T any] interface {
|
||||
Crear(entidad T, usuarioID uint) error
|
||||
Actualizar(entidad *T, input T, usuarioID uint) error
|
||||
Eliminar(id uint, usuarioID uint) error
|
||||
Baja(id uint, usuarioID uint) error
|
||||
// Alta updates a soft‑deleted record, clearing DeletedAt so it can be re‑activated.
|
||||
Alta(entidad *T, input T, usuarioID uint) error
|
||||
}
|
||||
@@ -25,10 +30,6 @@ func NewRepositorioBase[T any](db *gorm.DB) *RepositorioBase[T] {
|
||||
return &RepositorioBase[T]{db: db}
|
||||
}
|
||||
|
||||
func (r *RepositorioBase[T]) dbConAuditoria(usuarioID uint) *gorm.DB {
|
||||
return logs.DBConAuditoria(r.db, usuarioID)
|
||||
}
|
||||
|
||||
func (r *RepositorioBase[T]) Guardar(entidad *T) error {
|
||||
return r.db.Save(entidad).Error
|
||||
}
|
||||
@@ -42,29 +43,143 @@ func (r *RepositorioBase[T]) ObtenerTodos() ([]T, error) {
|
||||
}
|
||||
|
||||
func (r *RepositorioBase[T]) ObtenerPorID(id uint) (*T, error) {
|
||||
var entidad T
|
||||
if err := r.db.Unscoped().First(&entidad, id).Error; err != nil {
|
||||
entidad := nuevaEntidad[T]()
|
||||
if err := r.db.Unscoped().First(destinoConsulta(&entidad), id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &entidad, nil
|
||||
}
|
||||
|
||||
func (r *RepositorioBase[T]) Crear(entidad T, usuarioID uint) error {
|
||||
return r.dbConAuditoria(usuarioID).Create(&entidad).Error
|
||||
if err := r.db.Create(destinoConsulta(&entidad)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log, err := dto_builders.NewLogDirector().Crear(usuarioID, r.tabla(), dto_builders.ObtenerID(entidad))
|
||||
r.registrarLog(log, err, usuarioID, "creación")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RepositorioBase[T]) Actualizar(entidad *T, input T, usuarioID uint) error {
|
||||
return r.dbConAuditoria(usuarioID).Model(entidad).Updates(input).Error
|
||||
id := dto_builders.ObtenerID(entidad)
|
||||
|
||||
anterior := nuevaEntidad[T]()
|
||||
if id != nil {
|
||||
if err := r.db.Unscoped().First(destinoConsulta(&anterior), id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := r.db.Model(destinoConsulta(entidad)).Updates(input).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
actual := nuevaEntidad[T]()
|
||||
if id != nil {
|
||||
if err := r.db.Unscoped().First(destinoConsulta(&actual), id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
actual = input
|
||||
}
|
||||
|
||||
log, err := dto_builders.NewLogDirector().Modificar(usuarioID, r.tabla(), anterior, actual)
|
||||
r.registrarLog(log, err, usuarioID, "modificación")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Eliminar realiza un borrado físico del registro.
|
||||
func (r *RepositorioBase[T]) Eliminar(id uint, usuarioID uint) error {
|
||||
entidad := nuevaEntidad[T]()
|
||||
if err := r.db.Unscoped().Delete(destinoConsulta(&entidad), id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log, err := dto_builders.NewLogDirector().Eliminar(usuarioID, r.tabla(), id)
|
||||
r.registrarLog(log, err, usuarioID, "eliminación")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Baja realiza un borrado lógico del registro.
|
||||
func (r *RepositorioBase[T]) Baja(id uint, usuarioID uint) error {
|
||||
entidad := nuevaEntidad[T]()
|
||||
if err := r.db.Delete(destinoConsulta(&entidad), id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log, err := dto_builders.NewLogDirector().Baja(usuarioID, r.tabla(), id)
|
||||
r.registrarLog(log, err, usuarioID, "baja")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RepositorioBase[T]) Alta(entidad *T, input T, usuarioID uint) error {
|
||||
if err := r.dbConAuditoria(usuarioID).Unscoped().Model(entidad).Update("deleted_at", nil).Error; err != nil {
|
||||
modelo := destinoConsulta(entidad)
|
||||
if err := r.db.Unscoped().Model(modelo).Update("deleted_at", nil).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return r.dbConAuditoria(usuarioID).Unscoped().Model(entidad).Updates(input).Error
|
||||
if err := r.db.Unscoped().Model(modelo).Updates(input).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log, err := dto_builders.NewLogDirector().Alta(usuarioID, r.tabla(), dto_builders.ObtenerID(entidad))
|
||||
r.registrarLog(log, err, usuarioID, "alta")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RepositorioBase[T]) Eliminar(id uint, usuarioID uint) error {
|
||||
func (r *RepositorioBase[T]) tabla() string {
|
||||
entidad := nuevaEntidad[T]()
|
||||
stmt := &gorm.Statement{DB: r.db}
|
||||
if err := stmt.Parse(destinoConsulta(&entidad)); err == nil && stmt.Schema != nil {
|
||||
return stmt.Schema.Table
|
||||
}
|
||||
|
||||
tipo := reflect.TypeOf(entidad)
|
||||
for tipo != nil && tipo.Kind() == reflect.Ptr {
|
||||
tipo = tipo.Elem()
|
||||
}
|
||||
if tipo == nil {
|
||||
return ""
|
||||
}
|
||||
return r.db.NamingStrategy.TableName(tipo.Name())
|
||||
}
|
||||
|
||||
// registrarLog persiste el log sin interrumpir la operación de negocio si falla.
|
||||
func (r *RepositorioBase[T]) registrarLog(log *models_logs.Log, err error, usuarioID uint, contexto string) {
|
||||
if err != nil {
|
||||
applog.Printf("ERROR construyendo log de %s: %v", contexto, err)
|
||||
return
|
||||
}
|
||||
if log == nil {
|
||||
return
|
||||
}
|
||||
if err := ObtenerRepositorios().Logs.Crear(log, usuarioID); err != nil {
|
||||
applog.Printf("ERROR registrando log de %s: %v", contexto, err)
|
||||
}
|
||||
}
|
||||
|
||||
// nuevaEntidad retorna una instancia no nula del tipo T, incluso si T es un puntero.
|
||||
func nuevaEntidad[T any]() T {
|
||||
var entidad T
|
||||
return r.dbConAuditoria(usuarioID).Delete(&entidad, id).Error
|
||||
tipo := reflect.TypeOf(entidad)
|
||||
if tipo != nil && tipo.Kind() == reflect.Ptr {
|
||||
return reflect.New(tipo.Elem()).Interface().(T)
|
||||
}
|
||||
return entidad
|
||||
}
|
||||
|
||||
// destinoConsulta adapta T al destino que espera GORM, preservando el ID
|
||||
// cuando T es un puntero.
|
||||
func destinoConsulta[T any](entidad *T) any {
|
||||
tipo := reflect.TypeOf(*entidad)
|
||||
if tipo == nil || tipo.Kind() != reflect.Ptr {
|
||||
return entidad
|
||||
}
|
||||
|
||||
valor := reflect.ValueOf(*entidad)
|
||||
if valor.IsNil() {
|
||||
nueva := reflect.New(tipo.Elem())
|
||||
*entidad = nueva.Interface().(T)
|
||||
return *entidad
|
||||
}
|
||||
return *entidad
|
||||
}
|
||||
Reference in new issue
Block a user