Skip to content

Commit

Permalink
feat: use automapper on all controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Jul 12, 2021
1 parent ff75f57 commit dba04a8
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 19 deletions.
13 changes: 8 additions & 5 deletions src/Armory.Api/Controllers/ArmoryUsers/ArmoryUsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Armory.Users.Application.ResetPassword;
using Armory.Users.Application.SearchAllRoles;
using Armory.Users.Domain;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -25,11 +26,13 @@ public class ArmoryUsersController : ControllerBase
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;
private readonly IMapper _mapper;

public ArmoryUsersController(ICommandBus commandBus, IQueryBus queryBus)
public ArmoryUsersController(ICommandBus commandBus, IQueryBus queryBus, IMapper mapper)
{
_commandBus = commandBus;
_queryBus = queryBus;
_mapper = mapper;
}

private IActionResult IdentityErrors(IEnumerable<IdentityError> errors)
Expand Down Expand Up @@ -66,8 +69,8 @@ public async Task<IActionResult> ResetPassword(string usernameOrEmail, [FromBody
{
try
{
await _commandBus.Dispatch(
new ResetPasswordCommand(usernameOrEmail, request.Token, request.NewPassword));
var command = _mapper.Map<ResetPasswordCommand>(request);
await _commandBus.Dispatch(command);
}
catch (FormatException)
{
Expand All @@ -92,8 +95,8 @@ public async Task<IActionResult> ChangePassword(string usernameOrEmail,
{
try
{
await _commandBus.Dispatch(new ChangePasswordCommand(usernameOrEmail, request.OldPassword,
request.NewPassword));
var command = _mapper.Map<ChangePasswordCommand>(request);
await _commandBus.Dispatch(command);
}
catch (ArmoryUserNotFound)
{
Expand Down
13 changes: 8 additions & 5 deletions src/Armory.Api/Controllers/People/PeopleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Armory.Shared.Domain.Bus.Command;
using Armory.Shared.Domain.Bus.Query;
using Armory.Users.Domain;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -28,11 +29,13 @@ public class PeopleController : ControllerBase
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;
private readonly IMapper _mapper;

public PeopleController(ICommandBus commandBus, IQueryBus queryBus)
public PeopleController(ICommandBus commandBus, IQueryBus queryBus, IMapper mapper)
{
_commandBus = commandBus;
_queryBus = queryBus;
_mapper = mapper;
}

private IActionResult IdentityErrors(IEnumerable<IdentityError> errors)
Expand All @@ -50,8 +53,8 @@ public async Task<IActionResult> RegisterPerson([FromBody] CreatePersonRequest r
{
try
{
await _commandBus.Dispatch(new CreatePersonCommand(request.Id, request.FirstName, request.SecondName,
request.LastName, request.SecondLastName, request.Email, request.PhoneNumber, request.RoleName));
var command = _mapper.Map<CreatePersonCommand>(request);
await _commandBus.Dispatch(command);
}
catch (DbUpdateException)
{
Expand Down Expand Up @@ -125,8 +128,8 @@ public async Task<IActionResult> UpdatePerson(string id, [FromBody] UpdatePerson
{
try
{
await _commandBus.Dispatch(new UpdatePersonCommand(id, request.FirstName, request.SecondName,
request.LastName, request.SecondLastName));
var command = _mapper.Map<UpdatePersonCommand>(request);
await _commandBus.Dispatch(command);
}
catch (PersonNotFound)
{
Expand Down
15 changes: 15 additions & 0 deletions src/Armory.Api/Controllers/People/Requests/UpdatePersonRequest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
using System.ComponentModel.DataAnnotations;

namespace Armory.Api.Controllers.People.Requests
{
public class UpdatePersonRequest
{
[Required(ErrorMessage = "La identificación de la persona es requerida.")]
[MaxLength(10, ErrorMessage = "La identificación de la persona no debe tener más de 10 caracteres.")]
public string Id { get; set; }

[Required(ErrorMessage = "El primer nombre de la persona es requerido.")]
[MaxLength(50, ErrorMessage = "El primer nombre de la persona no debe tener más de 50 caracteres.")]
public string FirstName { get; set; }

[MaxLength(50, ErrorMessage = "El segundo nombre de la persona no debe tener más de 50 caracteres.")]
public string SecondName { get; set; }

[Required(ErrorMessage = "El primer apellido de la persona es requerido.")]
[MaxLength(50, ErrorMessage = "El primer aoellido de la persona no debe tener más de 50 caracteres.")]
public string LastName { get; set; }

[MaxLength(50, ErrorMessage = "El segundo apellido de la persona no debe tener más de 50 caracteres.")]
public string SecondLastName { get; set; }
}
}
14 changes: 9 additions & 5 deletions src/Armory.Api/Controllers/Squadron/SquadronsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using Armory.Squadrons.Application.Create;
using Armory.Squadrons.Application.Find;
using Armory.Squadrons.Application.SearchAll;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Armory.Api.Controllers.Squadron
{
Expand All @@ -20,24 +22,26 @@ public class SquadronsController : ControllerBase
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;
private readonly IMapper _mapper;

public SquadronsController(ICommandBus commandBus, IQueryBus queryBus)
public SquadronsController(ICommandBus commandBus, IQueryBus queryBus, IMapper mapper)
{
_commandBus = commandBus;
_queryBus = queryBus;
_mapper = mapper;
}

[HttpPost]
public async Task<IActionResult> RegisterSquadron([FromBody] CreateSquadronRequest request)
{
try
{
await _commandBus.Dispatch(new CreateSquadronCommand(request.Code, request.Name, request.PersonId));
var command = _mapper.Map<CreateSquadronCommand>(request);
await _commandBus.Dispatch(command);
}
catch (Exception e)
catch (DbUpdateException)
{
Console.WriteLine(e);
throw;
return BadRequest();
}

return Ok();
Expand Down
9 changes: 6 additions & 3 deletions src/Armory.Api/Controllers/Squads/SquadsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Armory.Squads.Application.Create;
using Armory.Squads.Application.Find;
using Armory.Squads.Application.SearchAll;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand All @@ -20,20 +21,22 @@ public class SquadsController : ControllerBase
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;
private readonly IMapper _mapper;

public SquadsController(ICommandBus commandBus, IQueryBus queryBus)
public SquadsController(ICommandBus commandBus, IQueryBus queryBus, IMapper mapper)
{
_commandBus = commandBus;
_queryBus = queryBus;
_mapper = mapper;
}

[HttpPost]
public async Task<IActionResult> RegisterSquad([FromBody] CreateSquadRequest request)
{
try
{
await _commandBus.Dispatch(new CreateSquadCommand(request.Code, request.Name, request.PersonId,
request.SquadronCode));
var command = _mapper.Map<CreateSquadCommand>(request);
await _commandBus.Dispatch(command);
}
catch (DbUpdateException)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Armory.Api/Profiles/ArmoryUsersProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Armory.Api.Controllers.ArmoryUsers.Requests;
using Armory.Users.Application.ChangePassword;
using Armory.Users.Application.ResetPassword;
using AutoMapper;

namespace Armory.Api.Profiles
{
public class ArmoryUsersProfile : Profile
{
public ArmoryUsersProfile()
{
CreateMap<ResetPasswordRequest, ResetPasswordCommand>();
CreateMap<ChangePasswordRequest, ChangePasswordCommand>();
}
}
}
16 changes: 16 additions & 0 deletions src/Armory.Api/Profiles/PeopleProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Armory.Api.Controllers.People.Requests;
using Armory.People.Application.Create;
using Armory.People.Application.Update;
using AutoMapper;

namespace Armory.Api.Profiles
{
public class PeopleProfile : Profile
{
public PeopleProfile()
{
CreateMap<CreatePersonRequest, CreatePersonCommand>();
CreateMap<UpdatePersonRequest, UpdatePersonCommand>();
}
}
}
14 changes: 14 additions & 0 deletions src/Armory.Api/Profiles/SquadronsProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Armory.Api.Controllers.Squadron.Requests;
using Armory.Squadrons.Application.Create;
using AutoMapper;

namespace Armory.Api.Profiles
{
public class SquadronsProfile : Profile
{
public SquadronsProfile()
{
CreateMap<CreateSquadronRequest, CreateSquadronCommand>();
}
}
}
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>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ArmoryUsersControllerTest : ApiTest
[SetUp]
public void SetUp()
{
_controller = new ArmoryUsersController(CommandBus.Object, QueryBus.Object);
_controller = new ArmoryUsersController(CommandBus.Object, QueryBus.Object, null);
}

private void ShouldHaveForgottenPasswordQuery()
Expand Down

0 comments on commit dba04a8

Please sign in to comment.