Skip to content

Commit

Permalink
feat: add Squads
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Sep 19, 2021
1 parent 5ae4016 commit 3f738a2
Show file tree
Hide file tree
Showing 27 changed files with 2,304 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Armory.Api.Controllers.Squads.Requests
{
public class CreateSquadRequest
{
public string Code { get; set; }
public string Name { get; set; }
public string PersonId { get; set; }
}
}
74 changes: 74 additions & 0 deletions src/Armory.Api/Controllers/Squads/SquadsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Armory.Api.Controllers.Squads.Requests;
using Armory.Squads.Application;
using Armory.Squads.Application.CheckExists;
using Armory.Squads.Application.Create;
using Armory.Squads.Application.Find;
using Armory.Squads.Application.SearchAll;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Armory.Api.Controllers.Squads
{
[ApiController]
[Authorize]
[Route("[controller]")]
public class SquadsController : ControllerBase
{
private readonly IMapper _mapper;
private readonly IMediator _mediator;

public SquadsController(IMediator mediator, IMapper mapper)
{
_mediator = mediator;
_mapper = mapper;
}

[HttpPost]
public async Task<IActionResult> RegisterSquad([FromBody] CreateSquadRequest request)
{
try
{
var command = _mapper.Map<CreateSquadCommand>(request);
await _mediator.Send(command);
}
catch (DbUpdateException)
{
var exists = await _mediator.Send(new CheckSquadExistsQuery { SquadCode = request.Code });
if (!exists) throw;

ModelState.AddModelError("SquadAlreadyRegistered",
$"Ya esxiste un escuadrón con el código '{request.Code}'");
return Conflict(new ValidationProblemDetails(ModelState));
}

return Ok();
}

[HttpGet]
public async Task<ActionResult<IEnumerable<SquadResponse>>> GetSquads()
{
var squads = await _mediator.Send(new SearchAllSquadsQuery());
return Ok(squads);
}

private NotFoundObjectResult SquadNotFound(string code)
{
ModelState.AddModelError("SquadNotFound", $"No existe ningún escuadrón con el codigo '{code}'");
return NotFound(new ValidationProblemDetails(ModelState));
}

[HttpGet("{code}")]
public async Task<ActionResult<SquadResponse>> GetSquad(string code)
{
var squad = await _mediator.Send(new FindSquadQuery { Code = code });
if (squad != null) return Ok(squad);

return SquadNotFound(code);
}
}
}
9 changes: 9 additions & 0 deletions src/Armory.Api/Extensions/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
using Armory.Ranks.Application.Create;
using Armory.Ranks.Application.Find;
using Armory.Ranks.Application.SearchAll;
using Armory.Squads.Application.CheckExists;
using Armory.Squads.Application.Create;
using Armory.Squads.Application.Find;
using Armory.Squads.Application.SearchAll;
using Armory.Troopers.Application.CheckExists;
using Armory.Troopers.Application.Create;
using Armory.Troopers.Application.Find;
Expand Down Expand Up @@ -107,6 +111,11 @@ public static IServiceCollection AddApplication(this IServiceCollection services
services.AddScoped<PersonExistsChecker, PersonExistsChecker>();
services.AddScoped<PersonDegreeUpdater, PersonDegreeUpdater>();

services.AddScoped<SquadCreator, SquadCreator>();
services.AddScoped<SquadExistsChecker, SquadExistsChecker>();
services.AddScoped<SquadFinder, SquadFinder>();
services.AddScoped<SquadsSearcher, SquadsSearcher>();

services.AddScoped<FlightCreator, FlightCreator>();
services.AddScoped<FlightFinder, FlightFinder>();
services.AddScoped<FlightExistsChecker, FlightExistsChecker>();
Expand Down
3 changes: 3 additions & 0 deletions src/Armory.Api/Extensions/Infrastructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
using Armory.Shared.Infrastructure.Persistence;
using Armory.Shared.Infrastructure.Persistence.EntityFramework;
using Armory.Shared.Infrastructure.Persistence.EntityFramework.Transactions;
using Armory.Squads.Domain;
using Armory.Squads.Infrastructure.Persistence;
using Armory.Troopers.Domain;
using Armory.Troopers.Infrastructure.Persistence;
using Armory.Users.Domain;
Expand Down Expand Up @@ -87,6 +89,7 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
services.Configure<SecretKey>(configuration.GetSection("SecretKey"));

services.AddScoped<IArmoryUsersRepository, MySqlArmoryUsersRepository>();
services.AddScoped<ISquadsRepository, MySqlSquadsRepository>();
services.AddScoped<IFlightsRepository, MySqlFlightsRepository>();
services.AddScoped<IPeopleRepository, MySqlPeopleRepository>();
services.AddScoped<IFireteamsRepository, MySqlFireteamsRepository>();
Expand Down
14 changes: 14 additions & 0 deletions src/Armory.Api/Profiles/SquadsProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Armory.Api.Controllers.Squads.Requests;
using Armory.Squads.Application.Create;
using AutoMapper;

namespace Armory.Api.Profiles
{
public class SquadsProfile : Profile
{
public SquadsProfile()
{
CreateMap<CreateSquadRequest, CreateSquadCommand>();
}
}
}
4 changes: 4 additions & 0 deletions src/Armory/Flights/Domain/Flight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using Armory.Fireteams.Domain;
using Armory.People.Domain;
using Armory.Squads.Domain;

namespace Armory.Flights.Domain
{
Expand All @@ -25,6 +26,9 @@ private Flight()

[ForeignKey("PersonId")] public Person Owner { get; set; }

[Required] public string SquadCode { get; set; }
[ForeignKey("SquadCode")] public Squad Squad { get; set; }

public ICollection<Fireteam> Fireteams { get; set; } = new HashSet<Fireteam>();
}
}
Loading

0 comments on commit 3f738a2

Please sign in to comment.