diff --git a/src/ReC.Application/Common/Procedures/DeleteProcedure/DeleteObjectProcedure.cs b/src/ReC.Application/Common/Procedures/DeleteProcedure/DeleteObjectProcedure.cs index dad028b..861022e 100644 --- a/src/ReC.Application/Common/Procedures/DeleteProcedure/DeleteObjectProcedure.cs +++ b/src/ReC.Application/Common/Procedures/DeleteProcedure/DeleteObjectProcedure.cs @@ -1,7 +1,9 @@ using DigitalData.Core.Abstraction.Application.Repository; +using DigitalData.Core.Exceptions; using MediatR; using Microsoft.Data.SqlClient; using ReC.Application.Common.Exceptions; +using ReC.Application.Common.Options; namespace ReC.Application.Common.Procedures.DeleteProcedure; @@ -47,7 +49,7 @@ public static class DeleteObjectProcedureExtensions } } -public class DeleteObjectProcedureHandler(IRepository repo) : IRequestHandler +public class DeleteObjectProcedureHandler(IRepository repo, SqlExceptionOptions sqlExceptionOptions) : IRequestHandler { public async Task Handle(DeleteObjectProcedure request, CancellationToken cancel) { @@ -59,20 +61,30 @@ public class DeleteObjectProcedureHandler(IRepository repo) : IRequestHandler 0 on failure - if (result > 0) + try { - throw new DeleteObjectFailedException(request, $"DeleteObject stored procedure failed with error code: {result}"); - } + var result = await repo.ExecuteQueryRawAsync( + "DECLARE @RC SMALLINT = 0; " + + "EXEC @RC = [dbo].[PRREC_DELETE_OBJECT] " + + "@pENTITY, @pSTART, @pEND, @pFORCE; " + + "SELECT @RC;", + parameters, + cancel); - return result; + // 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; + } + catch (SqlException ex) + { + if (sqlExceptionOptions.SqlExceptionNumber.Contains(ex.Number)) + throw new BadRequestException(ex.Message, ex); + else + throw; + } } }