Skip to content

Commit

Permalink
add get endpoint & fix ApplicationDto
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyork817 committed Mar 30, 2024
1 parent 7f73ea0 commit 7c89c30
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 27 deletions.
5 changes: 5 additions & 0 deletions Application.Dto/ApplicationDto.cs
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 0 additions & 5 deletions Application.Dto/UnsubmittedApplicationDto.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions Application/Application.Contracts/Applications/GetApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Application.Dto;
using MediatR;

namespace Application.Contracts.Applications;

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

public abstract record Response;

public sealed record Success(ApplicationDto Application) : Response;

public sealed record Failed(string Error) : Response;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public DeleteApplicationHandler(IDatabaseContext databaseContext)
public async Task<Response> Handle(Command request, CancellationToken cancellationToken)
{
var unsubmittedApplication = await _databaseContext.UnsubmittedApplications
.Include(a => a.Activity)
.Where(a => a.Id == request.Id)
.FirstOrDefaultAsync(cancellationToken);

Expand Down
42 changes: 42 additions & 0 deletions Application/Application/Applications/GetApplicationHandler.cs
Original file line number Diff line number Diff line change
@@ -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<Command, Response>
{
private readonly IDatabaseContext _databaseContext;

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

public async Task<Response> 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");
}
}
23 changes: 23 additions & 0 deletions Application/Application/Mapping/ApplicationDtoMapper.cs
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 0 additions & 15 deletions Application/Application/Mapping/UnsubmittedApplicationDtoMapper.cs

This file was deleted.

24 changes: 19 additions & 5 deletions Presentation/Controllers/ApplicationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@ public ApplicationsController(IMediator mediator, JsonSerializerSettings jsonSet

public CancellationToken CancellationToken => HttpContext.RequestAborted;

[HttpGet("{applicationId:guid}")]
public async Task<ActionResult<ApplicationDto>> 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<ActionResult<UnsubmittedApplicationDto>> CreateAsync([FromBody] CreateApplicationModel model)
public async Task<ActionResult<ApplicationDto>> CreateAsync([FromBody] CreateApplicationModel model)
{
var command =
new CreateApplication.Command(model.Author, model.Activity, model.Name, model.Description, model.Outline);
Expand All @@ -39,7 +53,7 @@ public async Task<ActionResult<UnsubmittedApplicationDto>> CreateAsync([FromBody
}

[HttpPut("{applicationId:guid}")]
public async Task<ActionResult<UnsubmittedApplicationDto>> UpdateAsync(
public async Task<ActionResult<ApplicationDto>> UpdateAsync(
Guid applicationId,
[FromBody] UpdateApplicationModel model)
{
Expand All @@ -56,7 +70,7 @@ public async Task<ActionResult<UnsubmittedApplicationDto>> UpdateAsync(
}

[HttpDelete("{applicationId:guid}")]
public async Task<ActionResult<UnsubmittedApplicationDto>> DeleteAsync(Guid applicationId)
public async Task<ActionResult<string>> DeleteAsync(Guid applicationId)
{
var command = new DeleteApplication.Command(applicationId);
var response = await _mediator.Send(command, CancellationToken);
Expand All @@ -68,9 +82,9 @@ public async Task<ActionResult<UnsubmittedApplicationDto>> DeleteAsync(Guid appl
_ => BadRequest()
};
}

[HttpPost("{applicationId:guid}/submit")]
public async Task<ActionResult<UnsubmittedApplicationDto>> SubmitAsync(Guid applicationId)
public async Task<ActionResult<string>> SubmitAsync(Guid applicationId)
{
var command = new SubmitApplication.Command(applicationId);
var response = await _mediator.Send(command, CancellationToken);
Expand Down

0 comments on commit 7c89c30

Please sign in to comment.