-
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
5ae4016
commit 3f738a2
Showing
27 changed files
with
2,304 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/Armory.Api/Controllers/Squads/Requests/CreateSquadRequest.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,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; } | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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
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 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>(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.