Skip to content

Commit

Permalink
feat(degrees): implement degree update
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Nov 5, 2021
1 parent 277c93d commit 797b3aa
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Armory.Api/Controllers/Degrees/DegreesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Armory.Degrees.Application.Find;
using Armory.Degrees.Application.SearchAll;
using Armory.Degrees.Application.SearchAllByRank;
using Armory.Degrees.Application.Update;
using Armory.Degrees.Domain;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -76,5 +78,21 @@ public async Task<ActionResult<DegreeResponse>> GetDegree(int id)

return DegreeNotFound(id);
}

[HttpPut("{id:int}")]
public async Task<ActionResult> UpdateDegree(int id, [FromBody] UpdateDegreeRequest request)
{
try
{
var command = _mapper.Map<UpdateDegreeCommand>(request);
await _mediator.Send(command);
}
catch (DegreeNotFoundException)
{
return DegreeNotFound(id);
}

return Ok();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Armory.Api.Controllers.Degrees.Requests
{
public class UpdateDegreeRequest
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Armory.Degrees.Application.Find;
using Armory.Degrees.Application.SearchAll;
using Armory.Degrees.Application.SearchAllByRank;
using Armory.Degrees.Application.Update;
using Microsoft.Extensions.DependencyInjection;

namespace Armory.Api.Extensions.DependencyInjection.Degrees
Expand All @@ -14,6 +15,7 @@ public static IServiceCollection AddDegreesApplication(this IServiceCollection s
services.AddScoped<DegreeFinder, DegreeFinder>();
services.AddScoped<AllDegreesSearcher, AllDegreesSearcher>();
services.AddScoped<DegreesByRankSearcher, DegreesByRankSearcher>();
services.AddScoped<DegreeUpdater, DegreeUpdater>();

return services;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Armory.Api/Profiles/DegreesProfile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Armory.Api.Controllers.Degrees.Requests;
using Armory.Degrees.Application.Create;
using Armory.Degrees.Application.Update;
using AutoMapper;

namespace Armory.Api.Profiles
Expand All @@ -9,6 +10,7 @@ public class DegreesProfile : Profile
public DegreesProfile()
{
CreateMap<CreateDegreeRequest, CreateDegreeCommand>();
CreateMap<UpdateDegreeRequest, UpdateDegreeCommand>();
}
}
}
7 changes: 6 additions & 1 deletion src/Armory/Degrees/Application/Find/DegreeFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ public DegreeFinder(IDegreesRepository repository)
_repository = repository;
}

public async Task<Degree> Find(int id, bool noTracking)
{
return await _repository.Find(id, noTracking);
}

public async Task<Degree> Find(int id)
{
return await _repository.Find(id);
return await Find(id, true).ConfigureAwait(false);
}
}
}
22 changes: 22 additions & 0 deletions src/Armory/Degrees/Application/Update/DegreeUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Armory.Degrees.Domain;
using Armory.Shared.Domain.Persistence.EntityFramework;

namespace Armory.Degrees.Application.Update
{
public class DegreeUpdater
{
private readonly IUnitWork _unitWork;

public DegreeUpdater(IUnitWork unitWork)
{
_unitWork = unitWork;
}

public async Task Update(Degree degree, string newName)
{
degree.Name = newName;
await _unitWork.SaveChanges();
}
}
}
10 changes: 10 additions & 0 deletions src/Armory/Degrees/Application/Update/UpdateDegreeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Armory.Shared.Domain.Bus.Command;

namespace Armory.Degrees.Application.Update
{
public class UpdateDegreeCommand : Command
{
public int Id { get; init; }
public string Name { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading;
using System.Threading.Tasks;
using Armory.Degrees.Application.Find;
using Armory.Degrees.Domain;
using Armory.Shared.Domain.Bus.Command;

namespace Armory.Degrees.Application.Update
{
public class UpdateDegreeCommandHandler : CommandHandler<UpdateDegreeCommand>
{
private readonly DegreeFinder _finder;
private readonly DegreeUpdater _updater;

public UpdateDegreeCommandHandler(DegreeFinder finder, DegreeUpdater updater)
{
_finder = finder;
_updater = updater;
}

protected override async Task Handle(UpdateDegreeCommand request, CancellationToken cancellationToken)
{
var degree = await _finder.Find(request.Id, false);
if (degree is null)
{
throw new DegreeNotFoundException();
}

await _updater.Update(degree, request.Name);
}
}
}
11 changes: 11 additions & 0 deletions src/Armory/Degrees/Domain/DegreeNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Armory.Shared.Domain;

namespace Armory.Degrees.Domain
{
/// <summary>
/// Represents error that occur when degree is not found
/// </summary>
public class DegreeNotFoundException : ArmoryException
{
}
}
38 changes: 38 additions & 0 deletions test/Armory.Api.Test/Controllers/Degrees/DegreesControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
using Armory.Degrees.Application.Find;
using Armory.Degrees.Application.SearchAll;
using Armory.Degrees.Application.SearchAllByRank;
using Armory.Degrees.Application.Update;
using Armory.Degrees.Domain;
using AutoMapper;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Moq;
Expand Down Expand Up @@ -129,5 +132,40 @@ public async Task Get_A_Non_Existent_Degree()
Assert.IsTrue(errors.Any());
Assert.AreEqual("No se encontró ningun grado con el código '2'.", errors[0]);
}

[Test]
[Order(6)]
public async Task UpdateDegree_Should_Update_A_Degree()
{
Mediator.Setup(x => x.Send(It.IsAny<UpdateDegreeCommand>(), CancellationToken.None)).Verifiable();

var request = new UpdateDegreeRequest
{
Id = 1,
Name = "Estudiante 2"
};

var result = await _controller.UpdateDegree(request.Id, request);

result.Should().NotBeNull().And.BeAssignableTo<OkResult>();
}

[Test]
[Order(7)]
public async Task UpdateDegree_Should_Return_Not_Found()
{
Mediator.Setup(x => x.Send(It.IsAny<UpdateDegreeCommand>(), CancellationToken.None))
.Throws<DegreeNotFoundException>();

var request = new UpdateDegreeRequest
{
Id = 2,
Name = "Estudiante 2"
};

var result = await _controller.UpdateDegree(request.Id, request);

result.Should().NotBeNull().And.BeAssignableTo<NotFoundObjectResult>();
}
}
}

0 comments on commit 797b3aa

Please sign in to comment.