55 lines
2.4 KiB
C#
55 lines
2.4 KiB
C#
|
|
using Dapper;
|
||
|
|
using System.Data;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
using GestionIntegral.Api.Models.Distribucion;
|
||
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
namespace GestionIntegral.Api.Data.Repositories.Distribucion
|
||
|
|
{
|
||
|
|
public class PubliSeccionRepository : IPubliSeccionRepository
|
||
|
|
{
|
||
|
|
private readonly DbConnectionFactory _cf; private readonly ILogger<PubliSeccionRepository> _log;
|
||
|
|
public PubliSeccionRepository(DbConnectionFactory cf, ILogger<PubliSeccionRepository> log) { _cf = cf; _log = log; }
|
||
|
|
|
||
|
|
public async Task<bool> DeleteByPublicacionIdAsync(int idPublicacion, int idUsuarioAuditoria, IDbTransaction transaction)
|
||
|
|
{
|
||
|
|
const string selectSql = "SELECT * FROM dbo.dist_dtPubliSecciones WHERE Id_Publicacion = @IdPublicacion";
|
||
|
|
var itemsToDelete = await transaction.Connection!.QueryAsync<PubliSeccion>(selectSql, new { IdPublicacion = idPublicacion }, transaction);
|
||
|
|
|
||
|
|
if (itemsToDelete.Any())
|
||
|
|
{
|
||
|
|
const string insertHistoricoSql = @"
|
||
|
|
INSERT INTO dbo.dist_dtPubliSecciones_H (Id_Seccion, Id_Publicacion, Nombre, Estado, Id_Usuario, FechaMod, TipoMod)
|
||
|
|
VALUES (@Id_Seccion, @Id_Publicacion, @Nombre, @Estado, @Id_Usuario, @FechaMod, @TipoMod);";
|
||
|
|
|
||
|
|
foreach (var item in itemsToDelete)
|
||
|
|
{
|
||
|
|
await transaction.Connection!.ExecuteAsync(insertHistoricoSql, new
|
||
|
|
{
|
||
|
|
Id_Seccion = item.IdSeccion, // Mapeo de propiedad a parámetro SQL
|
||
|
|
Id_Publicacion = item.IdPublicacion,
|
||
|
|
item.Nombre,
|
||
|
|
item.Estado,
|
||
|
|
Id_Usuario = idUsuarioAuditoria,
|
||
|
|
FechaMod = DateTime.Now,
|
||
|
|
TipoMod = "Eliminado (Cascada)"
|
||
|
|
}, transaction);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const string deleteSql = "DELETE FROM dbo.dist_dtPubliSecciones WHERE Id_Publicacion = @IdPublicacion";
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await transaction.Connection!.ExecuteAsync(deleteSql, new { IdPublicacion = idPublicacion }, transaction);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
catch (System.Exception e)
|
||
|
|
{
|
||
|
|
_log.LogError(e, "Error al eliminar PubliSecciones por IdPublicacion: {idPublicacion}", idPublicacion);
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|