-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4432d47
commit 9539cec
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
Application/Application.Contracts/Applications/DeleteApplication.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
Application/Application/Applications/DeleteApplicationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters