-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 320 ddd complete a mission 2 (#326)
* Added endpoint for completing a mission. Updated ApiFilter validation to handle DomainException. * Fixed up tests
- Loading branch information
1 parent
e4f8a7b
commit e46ce92
Showing
12 changed files
with
152 additions
and
30 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/Application/Features/Teams/Commands/CompleteMission/CompleteMissionCommand.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,37 @@ | ||
using SSW.CleanArchitecture.Application.Common.Exceptions; | ||
using SSW.CleanArchitecture.Application.Common.Interfaces; | ||
using SSW.CleanArchitecture.Domain.Teams; | ||
|
||
namespace SSW.CleanArchitecture.Application.Features.Teams.Commands.CompleteMission; | ||
|
||
public sealed record CompleteMissionCommand(Guid TeamId) : IRequest; | ||
|
||
// ReSharper disable once UnusedType.Global | ||
public sealed class CompleteMissionCommandHandler(IApplicationDbContext dbContext) | ||
: IRequestHandler<CompleteMissionCommand> | ||
{ | ||
public async Task Handle(CompleteMissionCommand request, CancellationToken cancellationToken) | ||
{ | ||
var teamId = new TeamId(request.TeamId); | ||
var team = dbContext.Teams | ||
.WithSpecification(new TeamByIdSpec(teamId)) | ||
.FirstOrDefault(); | ||
|
||
if (team is null) | ||
{ | ||
throw new NotFoundException(nameof(Team), teamId); | ||
} | ||
|
||
team.CompleteCurrentMission(); | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
|
||
public class CompleteMissionCommandValidator : AbstractValidator<CompleteMissionCommand> | ||
{ | ||
public CompleteMissionCommandValidator() | ||
{ | ||
RuleFor(v => v.TeamId) | ||
.NotEmpty(); | ||
} | ||
} |
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,16 @@ | ||
namespace SSW.CleanArchitecture.Domain.Common; | ||
|
||
public class DomainException : Exception | ||
{ | ||
public DomainException() | ||
{ | ||
} | ||
|
||
public DomainException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public DomainException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
} |
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
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
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
39 changes: 39 additions & 0 deletions
39
tests/WebApi.IntegrationTests/Endpoints/Teams/Commands/CompleteMissionCommandTests.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,39 @@ | ||
using Ardalis.Specification.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
using SSW.CleanArchitecture.Domain.Teams; | ||
using System.Net; | ||
using WebApi.IntegrationTests.Common.Factories; | ||
using WebApi.IntegrationTests.Common.Fixtures; | ||
|
||
namespace WebApi.IntegrationTests.Endpoints.Teams.Commands; | ||
|
||
public class CompleteMissionCommandTests(TestingDatabaseFixture fixture, ITestOutputHelper output) | ||
: IntegrationTestBase(fixture, output) | ||
{ | ||
[Fact] | ||
public async Task Command_ShouldCompleteMission() | ||
{ | ||
// Arrange | ||
var hero = HeroFactory.Generate(); | ||
var team = TeamFactory.Generate(); | ||
team.AddHero(hero); | ||
team.ExecuteMission("Save the world"); | ||
await AddEntityAsync(team); | ||
var teamId = team.Id.Value; | ||
var client = GetAnonymousClient(); | ||
|
||
// Act | ||
var result = await client.PostAsync($"/api/teams/{teamId}/complete-mission", null); | ||
|
||
// Assert | ||
var updatedTeam = await GetQueryable<Team>() | ||
.WithSpecification(new TeamByIdSpec(team.Id)) | ||
.FirstOrDefaultAsync(); | ||
var mission = updatedTeam!.Missions.First(); | ||
|
||
result.StatusCode.Should().Be(HttpStatusCode.OK); | ||
updatedTeam!.Missions.Should().HaveCount(1); | ||
updatedTeam.Status.Should().Be(TeamStatus.Available); | ||
mission.Status.Should().Be(MissionStatus.Complete); | ||
} | ||
} |
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