Skip to content

Commit

Permalink
add delete application endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyork817 committed Mar 30, 2024
1 parent 4432d47 commit 9539cec
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using MediatR;

namespace Application.Contracts.Applications;

public static class DeleteApplication
{
public record struct Command(Guid Id) : IRequest<Response>;

public abstract record Response;

public sealed record Success(string Message) : Response;

public sealed record Failed(string Error) : Response;
}
59 changes: 59 additions & 0 deletions Application/Application/Applications/DeleteApplicationHandler.cs
Original file line number Diff line number Diff line change
@@ -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<Command, Response>
{
private readonly IDatabaseContext _databaseContext;

public DeleteApplicationHandler(IDatabaseContext databaseContext)
{
_databaseContext = databaseContext;
}

public async Task<Response> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
14 changes: 14 additions & 0 deletions Presentation/Controllers/ApplicationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ public async Task<ActionResult<UnsubmittedApplicationDto>> UpdateAsync(
_ => BadRequest()
};
}

[HttpDelete("{applicationId:guid}")]
public async Task<ActionResult<UnsubmittedApplicationDto>> 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()
};
}
}

0 comments on commit 9539cec

Please sign in to comment.