From 9539cec2d9ff474b5f085723424aca78c57e2677 Mon Sep 17 00:00:00 2001 From: nikita <368050@edu.itmo.ru> Date: Sat, 30 Mar 2024 12:50:06 +0300 Subject: [PATCH] add delete application endpoint --- .../Applications/DeleteApplication.cs | 14 +++++ .../Applications/DeleteApplicationHandler.cs | 59 +++++++++++++++++++ .../Applications/UpdateApplicationHandler.cs | 2 +- .../Controllers/ApplicationsController.cs | 14 +++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 Application/Application.Contracts/Applications/DeleteApplication.cs create mode 100644 Application/Application/Applications/DeleteApplicationHandler.cs diff --git a/Application/Application.Contracts/Applications/DeleteApplication.cs b/Application/Application.Contracts/Applications/DeleteApplication.cs new file mode 100644 index 0000000..cc8d3c4 --- /dev/null +++ b/Application/Application.Contracts/Applications/DeleteApplication.cs @@ -0,0 +1,14 @@ +using MediatR; + +namespace Application.Contracts.Applications; + +public static class DeleteApplication +{ + public record struct Command(Guid Id) : IRequest; + + public abstract record Response; + + public sealed record Success(string Message) : Response; + + public sealed record Failed(string Error) : Response; +} \ No newline at end of file diff --git a/Application/Application/Applications/DeleteApplicationHandler.cs b/Application/Application/Applications/DeleteApplicationHandler.cs new file mode 100644 index 0000000..88b537d --- /dev/null +++ b/Application/Application/Applications/DeleteApplicationHandler.cs @@ -0,0 +1,59 @@ +using Application.Abstractions.DataAccess; +using MediatR; +using Microsoft.EntityFrameworkCore; +using static Application.Contracts.Applications.DeleteApplication; + +namespace Application.Applications; + +public class DeleteApplicationHandler : IRequestHandler +{ + private readonly IDatabaseContext _databaseContext; + + public DeleteApplicationHandler(IDatabaseContext databaseContext) + { + _databaseContext = databaseContext; + } + + public async Task Handle(Command request, CancellationToken cancellationToken) + { + var unsubmittedApplication = await _databaseContext.UnsubmittedApplications + .Where(a => a.Id == request.Id) + .FirstOrDefaultAsync(cancellationToken); + + if (unsubmittedApplication is not null) + { + _databaseContext.UnsubmittedApplications.Remove(unsubmittedApplication); + try + { + await _databaseContext.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateException e) + { + return new Failed("Error while removing application from database"); + } + + return new Success("Ok"); + } + + var submittedApplication = await _databaseContext.SubmittedApplications + .Where(a => a.Id == request.Id) + .FirstOrDefaultAsync(cancellationToken); + + if (submittedApplication is null) + { + return new Failed("No application with such id"); + } + + _databaseContext.SubmittedApplications.Remove(submittedApplication); + try + { + await _databaseContext.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateException e) + { + return new Failed("Error while removing application from database"); + } + + return new Success("Ok"); + } +} \ No newline at end of file diff --git a/Application/Application/Applications/UpdateApplicationHandler.cs b/Application/Application/Applications/UpdateApplicationHandler.cs index 65317f7..ec6a48d 100644 --- a/Application/Application/Applications/UpdateApplicationHandler.cs +++ b/Application/Application/Applications/UpdateApplicationHandler.cs @@ -60,7 +60,7 @@ await _databaseContext.ApplicationActivities.FirstOrDefaultAsync(x => x.Name == } catch (DbUpdateException e) { - return new Failed("Error while changing application in database"); + return new Failed("Error while changing application in database"); } return new Success(application.AsDto()); diff --git a/Presentation/Controllers/ApplicationsController.cs b/Presentation/Controllers/ApplicationsController.cs index cd02909..5f41e53 100644 --- a/Presentation/Controllers/ApplicationsController.cs +++ b/Presentation/Controllers/ApplicationsController.cs @@ -54,4 +54,18 @@ public async Task> UpdateAsync( _ => BadRequest() }; } + + [HttpDelete("{applicationId:guid}")] + public async Task> DeleteAsync(Guid applicationId) + { + var command = new DeleteApplication.Command(applicationId); + var response = await _mediator.Send(command, CancellationToken); + + return response switch + { + DeleteApplication.Success result => Ok(result.Message), + DeleteApplication.Failed result => BadRequest(result.Error), + _ => BadRequest() + }; + } } \ No newline at end of file