Skip to content

Commit

Permalink
feat(squadron): find a squadron by its code
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Jun 15, 2021
1 parent 4e4ba75 commit 5881cce
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Armory.Api/Controllers/Squadron/SquadronsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Armory.Api.Controllers.Squadron.Requests;
using Armory.Shared.Domain.Bus.Command;
using Armory.Shared.Domain.Bus.Query;
using Armory.Squadron.Application;
using Armory.Squadron.Application.Create;
using Armory.Squadron.Application.SearchByCode;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -38,5 +40,19 @@ public async Task<IActionResult> RegisterSquadron([FromBody] CreateSquadronReque

return Ok();
}

[HttpGet("{code}")]
public async Task<ActionResult<SquadronResponse>> GetSquadron(string code)
{
var response = await _queryBus.Ask<SquadronResponse>(new SearchSquadronByCodeQuery(code));
if (response != null)
{
return Ok(response);
}

ModelState.AddModelError("SquadronNotFound",
$"El escuadrón con el código '{code}' no se encuentra registrado.");
return NotFound(new ValidationProblemDetails(ModelState));
}
}
}
2 changes: 2 additions & 0 deletions src/Armory.Api/Extensions/Application.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Armory.Shared.Extensions;
using Armory.Shared.Helpers;
using Armory.Squadron.Application.Create;
using Armory.Squadron.Application.SearchByCode;
using Armory.Users.Application.Authenticate;
using Armory.Users.Application.ChangePassword;
using Armory.Users.Application.ConfirmEmail;
Expand Down Expand Up @@ -29,6 +30,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services
services.AddQueryServices(AssemblyHelper.GetInstance(Assemblies.Users));

services.AddScoped<SquadronCreator, SquadronCreator>();
services.AddScoped<SquadronByCodeSearcher, SquadronByCodeSearcher>();
services.AddCommandServices(AssemblyHelper.GetInstance(Assemblies.Squadron));
services.AddQueryServices(AssemblyHelper.GetInstance(Assemblies.Squadron));

Expand Down
14 changes: 14 additions & 0 deletions src/Squadron/Application/SearchByCode/SearchSquadronByCodeQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Armory.Shared.Domain.Bus.Query;

namespace Armory.Squadron.Application.SearchByCode
{
public class SearchSquadronByCodeQuery : Query
{
public string Code { get; }

public SearchSquadronByCodeQuery(string code)
{
Code = code;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using Armory.Shared.Domain.Bus.Query;

namespace Armory.Squadron.Application.SearchByCode
{
public class SearchSquadronByCodeQueryHandler : IQueryHandler<SearchSquadronByCodeQuery, SquadronResponse>
{
private readonly SquadronByCodeSearcher _searcher;


public SearchSquadronByCodeQueryHandler(SquadronByCodeSearcher searcher)
{
_searcher = searcher;
}

public async Task<SquadronResponse> Handle(SearchSquadronByCodeQuery query)
{
return await _searcher.Search(query.Code);
}
}
}
21 changes: 21 additions & 0 deletions src/Squadron/Application/SearchByCode/SquadronByCodeSearcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using Armory.Squadron.Domain;

namespace Armory.Squadron.Application.SearchByCode
{
public class SquadronByCodeSearcher
{
private readonly ISquadronRepository _repository;

public SquadronByCodeSearcher(ISquadronRepository repository)
{
_repository = repository;
}

public async Task<SquadronResponse> Search(string code)
{
var squadron = await _repository.Find(code);
return squadron == null ? null : SquadronResponse.FromAggregate(squadron);
}
}
}
21 changes: 21 additions & 0 deletions src/Squadron/Application/SquadronResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Armory.Squadron.Application
{
public class SquadronResponse
{
public string Code { get; }
public string Name { get; }
public string ArmoryUserId { get; }

public SquadronResponse(string code, string name, string armoryUserId)
{
Code = code;
Name = name;
ArmoryUserId = armoryUserId;
}

public static SquadronResponse FromAggregate(Domain.Squadron squadron)
{
return new SquadronResponse(squadron.Code, squadron.Name, squadron.ArmoryUserId);
}
}
}
1 change: 1 addition & 0 deletions src/Squadron/Domain/ISquadronRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace Armory.Squadron.Domain
public interface ISquadronRepository
{
Task Save(Squadron squadron);
Task<Squadron> Find(string code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ public async Task Save(Domain.Squadron squadron)
await _context.Squadrons.AddAsync(squadron);
await _context.SaveChangesAsync();
}

public async Task<Domain.Squadron> Find(string code)
{
return await _context.Squadrons.FindAsync(code);
}
}
}

0 comments on commit 5881cce

Please sign in to comment.