From 7c89c3081f2cfd4463ff24201c54903b98d7bd59 Mon Sep 17 00:00:00 2001 From: nikita <368050@edu.itmo.ru> Date: Sat, 30 Mar 2024 14:32:41 +0300 Subject: [PATCH] add get endpoint & fix ApplicationDto --- Application.Dto/ApplicationDto.cs | 5 +++ Application.Dto/UnsubmittedApplicationDto.cs | 5 --- .../Applications/CreateApplication.cs | 2 +- .../Applications/GetApplication.cs | 15 +++++++ .../Applications/UpdateApplication.cs | 2 +- .../Applications/DeleteApplicationHandler.cs | 1 + .../Applications/GetApplicationHandler.cs | 42 +++++++++++++++++++ .../Mapping/ApplicationDtoMapper.cs | 23 ++++++++++ .../UnsubmittedApplicationDtoMapper.cs | 15 ------- .../Controllers/ApplicationsController.cs | 24 ++++++++--- 10 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 Application.Dto/ApplicationDto.cs delete mode 100644 Application.Dto/UnsubmittedApplicationDto.cs create mode 100644 Application/Application.Contracts/Applications/GetApplication.cs create mode 100644 Application/Application/Applications/GetApplicationHandler.cs create mode 100644 Application/Application/Mapping/ApplicationDtoMapper.cs delete mode 100644 Application/Application/Mapping/UnsubmittedApplicationDtoMapper.cs diff --git a/Application.Dto/ApplicationDto.cs b/Application.Dto/ApplicationDto.cs new file mode 100644 index 0000000..2e5c0f9 --- /dev/null +++ b/Application.Dto/ApplicationDto.cs @@ -0,0 +1,5 @@ +using System.Diagnostics; + +namespace Application.Dto; + +public record ApplicationDto(Guid Id, Guid Author, string? Activity, string? Name, string? Description, string? Outline); \ No newline at end of file diff --git a/Application.Dto/UnsubmittedApplicationDto.cs b/Application.Dto/UnsubmittedApplicationDto.cs deleted file mode 100644 index 023bff5..0000000 --- a/Application.Dto/UnsubmittedApplicationDto.cs +++ /dev/null @@ -1,5 +0,0 @@ -using System.Diagnostics; - -namespace Application.Dto; - -public record UnsubmittedApplicationDto(Guid Id, Guid Author, string? Activity, string? Name, string? Description, string? Outline); \ No newline at end of file diff --git a/Application/Application.Contracts/Applications/CreateApplication.cs b/Application/Application.Contracts/Applications/CreateApplication.cs index d444ce8..6da77d2 100644 --- a/Application/Application.Contracts/Applications/CreateApplication.cs +++ b/Application/Application.Contracts/Applications/CreateApplication.cs @@ -9,7 +9,7 @@ public record struct Command(Guid? Author, string? Activity, string? Name, strin public abstract record Response; - public sealed record Success(UnsubmittedApplicationDto Application) : Response; + public sealed record Success(ApplicationDto Application) : Response; public sealed record Failed(string Error) : Response; } \ No newline at end of file diff --git a/Application/Application.Contracts/Applications/GetApplication.cs b/Application/Application.Contracts/Applications/GetApplication.cs new file mode 100644 index 0000000..89f55af --- /dev/null +++ b/Application/Application.Contracts/Applications/GetApplication.cs @@ -0,0 +1,15 @@ +using Application.Dto; +using MediatR; + +namespace Application.Contracts.Applications; + +public static class GetApplication +{ + public record struct Command(Guid Id) : IRequest; + + public abstract record Response; + + public sealed record Success(ApplicationDto Application) : Response; + + public sealed record Failed(string Error) : Response; +} \ No newline at end of file diff --git a/Application/Application.Contracts/Applications/UpdateApplication.cs b/Application/Application.Contracts/Applications/UpdateApplication.cs index 66df0fa..ead8fff 100644 --- a/Application/Application.Contracts/Applications/UpdateApplication.cs +++ b/Application/Application.Contracts/Applications/UpdateApplication.cs @@ -9,7 +9,7 @@ public record struct Command(Guid Id, string? Activity, string? Name, string? De public abstract record Response; - public sealed record Success(UnsubmittedApplicationDto Application) : Response; + public sealed record Success(ApplicationDto Application) : 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 index ae435ee..d888b55 100644 --- a/Application/Application/Applications/DeleteApplicationHandler.cs +++ b/Application/Application/Applications/DeleteApplicationHandler.cs @@ -17,6 +17,7 @@ public DeleteApplicationHandler(IDatabaseContext databaseContext) public async Task Handle(Command request, CancellationToken cancellationToken) { var unsubmittedApplication = await _databaseContext.UnsubmittedApplications + .Include(a => a.Activity) .Where(a => a.Id == request.Id) .FirstOrDefaultAsync(cancellationToken); diff --git a/Application/Application/Applications/GetApplicationHandler.cs b/Application/Application/Applications/GetApplicationHandler.cs new file mode 100644 index 0000000..40aa2d9 --- /dev/null +++ b/Application/Application/Applications/GetApplicationHandler.cs @@ -0,0 +1,42 @@ +using Application.Abstractions.DataAccess; +using Application.Mapping; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace Application.Applications; +using static Application.Contracts.Applications.GetApplication; + +public class GetApplicationHandler : IRequestHandler +{ + private readonly IDatabaseContext _databaseContext; + + public GetApplicationHandler(IDatabaseContext databaseContext) + { + _databaseContext = databaseContext; + } + + public async Task Handle(Command request, CancellationToken cancellationToken) + { + var unsubmittedApplication = await _databaseContext.UnsubmittedApplications + .Include(a => a.Activity) + .Where(a => a.Id == request.Id) + .FirstOrDefaultAsync(cancellationToken); + + if (unsubmittedApplication is not null) + { + return new Success(unsubmittedApplication.AsDto()); + } + + var submittedApplication = await _databaseContext.SubmittedApplications + .Include(a => a.Activity) + .Where(a => a.Id == request.Id) + .FirstOrDefaultAsync(cancellationToken); + + if (submittedApplication is not null) + { + return new Success(submittedApplication.AsDto()); + } + + return new Failed("No application with such id"); + } +} \ No newline at end of file diff --git a/Application/Application/Mapping/ApplicationDtoMapper.cs b/Application/Application/Mapping/ApplicationDtoMapper.cs new file mode 100644 index 0000000..871ee6e --- /dev/null +++ b/Application/Application/Mapping/ApplicationDtoMapper.cs @@ -0,0 +1,23 @@ +using Application.Dto; +using Domain.Applications; + +namespace Application.Mapping; + +public static class ApplicationDtoMapper +{ + public static ApplicationDto AsDto(this UnsubmittedApplication application) + => new ApplicationDto(application.Id, + application.UserId, + application.Activity?.Name, + application.Name, + application.Description, + application.Plan); + + public static ApplicationDto AsDto(this SubmittedApplication application) + => new ApplicationDto(application.Id, + application.UserId, + application.Activity.Name, + application.Name, + application.Description, + application.Plan); +} \ No newline at end of file diff --git a/Application/Application/Mapping/UnsubmittedApplicationDtoMapper.cs b/Application/Application/Mapping/UnsubmittedApplicationDtoMapper.cs deleted file mode 100644 index 9df2e22..0000000 --- a/Application/Application/Mapping/UnsubmittedApplicationDtoMapper.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Application.Dto; -using Domain.Applications; - -namespace Application.Mapping; - -public static class UnsubmittedApplicationDtoMapper -{ - public static UnsubmittedApplicationDto AsDto(this UnsubmittedApplication application) - => new UnsubmittedApplicationDto(application.Id, - application.UserId, - application.Activity?.Name, - application.Name, - application.Description, - application.Plan); -} \ No newline at end of file diff --git a/Presentation/Controllers/ApplicationsController.cs b/Presentation/Controllers/ApplicationsController.cs index 8286210..2d9503b 100644 --- a/Presentation/Controllers/ApplicationsController.cs +++ b/Presentation/Controllers/ApplicationsController.cs @@ -23,8 +23,22 @@ public ApplicationsController(IMediator mediator, JsonSerializerSettings jsonSet public CancellationToken CancellationToken => HttpContext.RequestAborted; + [HttpGet("{applicationId:guid}")] + public async Task> GetAsync(Guid applicationId) + { + var command = new GetApplication.Command(applicationId); + var response = await _mediator.Send(command, CancellationToken); + + return response switch + { + GetApplication.Success result => Ok(JsonConvert.SerializeObject(result.Application, _jsonSettings)), + GetApplication.Failed result => BadRequest(result.Error), + _ => BadRequest() + }; + } + [HttpPost] - public async Task> CreateAsync([FromBody] CreateApplicationModel model) + public async Task> CreateAsync([FromBody] CreateApplicationModel model) { var command = new CreateApplication.Command(model.Author, model.Activity, model.Name, model.Description, model.Outline); @@ -39,7 +53,7 @@ public async Task> CreateAsync([FromBody } [HttpPut("{applicationId:guid}")] - public async Task> UpdateAsync( + public async Task> UpdateAsync( Guid applicationId, [FromBody] UpdateApplicationModel model) { @@ -56,7 +70,7 @@ public async Task> UpdateAsync( } [HttpDelete("{applicationId:guid}")] - public async Task> DeleteAsync(Guid applicationId) + public async Task> DeleteAsync(Guid applicationId) { var command = new DeleteApplication.Command(applicationId); var response = await _mediator.Send(command, CancellationToken); @@ -68,9 +82,9 @@ public async Task> DeleteAsync(Guid appl _ => BadRequest() }; } - + [HttpPost("{applicationId:guid}/submit")] - public async Task> SubmitAsync(Guid applicationId) + public async Task> SubmitAsync(Guid applicationId) { var command = new SubmitApplication.Command(applicationId); var response = await _mediator.Send(command, CancellationToken);