Add DeleteObjectProcedure for entity deletion handling
Introduce `DeleteObjectProcedure` to encapsulate parameters for delete operations, including entity, ID range, and force flag. Add `DeleteObjectProcedureExtensions` for convenient invocation via `ISender`. Implement `DeleteObjectProcedureHandler` to execute the `[dbo].[PRREC_DELETE_OBJECT]` stored procedure using dynamic SQL parameters. Throw `DeleteObjectFailedException` on failure and ensure `End` defaults to `Start` if unset. Add XML documentation for improved code clarity. Include necessary `using` directives for dependencies.
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
using ReC.Application.Common.Exceptions;
|
||||||
|
|
||||||
|
namespace ReC.Application.Common.Procedures.DeleteProcedure;
|
||||||
|
|
||||||
|
public record DeleteObjectProcedure : IRequest<int>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Target entity: ACTION, ENDPOINT, ENDPOINT_AUTH, ENDPOINT_PARAMS, PROFILE, RESULT
|
||||||
|
/// </summary>
|
||||||
|
public string Entity { get; set; } = null!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Start GUID/ID (inclusive)
|
||||||
|
/// </summary>
|
||||||
|
public long Start { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// End GUID/ID (inclusive). If 0, will be set to Start value.
|
||||||
|
/// </summary>
|
||||||
|
public long End { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If true, delete even if dependent data exists
|
||||||
|
/// </summary>
|
||||||
|
public bool Force { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DeleteObjectProcedureExtensions
|
||||||
|
{
|
||||||
|
public static Task<int> ExecuteDeleteProcedure(this ISender sender, IDeleteProcedure procedure, CancellationToken cancel = default)
|
||||||
|
{
|
||||||
|
return sender.Send(procedure.ToObjectProcedure(), cancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<int> ExecuteDeleteProcedure(this ISender sender, string entity, long start, long end = 0, bool force = false, CancellationToken cancel = default)
|
||||||
|
{
|
||||||
|
return sender.Send(new DeleteObjectProcedure
|
||||||
|
{
|
||||||
|
Entity = entity,
|
||||||
|
Start = start,
|
||||||
|
End = end,
|
||||||
|
Force = force
|
||||||
|
}, cancel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DeleteObjectProcedureHandler(IRepository repo) : IRequestHandler<DeleteObjectProcedure, int>
|
||||||
|
{
|
||||||
|
public async Task<int> Handle(DeleteObjectProcedure request, CancellationToken cancel)
|
||||||
|
{
|
||||||
|
var parameters = new[]
|
||||||
|
{
|
||||||
|
new SqlParameter("@pENTITY", request.Entity ?? (object)DBNull.Value),
|
||||||
|
new SqlParameter("@pSTART", request.Start.ToString()),
|
||||||
|
new SqlParameter("@pEND", request.End.ToString()),
|
||||||
|
new SqlParameter("@pFORCE", (object?)request.Force ?? DBNull.Value)
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = await repo.ExecuteQueryRawAsync(
|
||||||
|
"EXEC @RC = [dbo].[PRREC_DELETE_OBJECT] " +
|
||||||
|
"@pENTITY, @pSTART, @pEND, @pFORCE; " +
|
||||||
|
"SELECT @RC;",
|
||||||
|
parameters,
|
||||||
|
cancel);
|
||||||
|
|
||||||
|
// The stored procedure returns 0 on success, error codes > 0 on failure
|
||||||
|
if (result > 0)
|
||||||
|
{
|
||||||
|
throw new DeleteObjectFailedException(request, $"DeleteObject stored procedure failed with error code: {result}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user