-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/Application/Features/Teams/Commands/ExecuteMission/SendTeamOnMissionCommand.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,42 @@ | ||
using SSW.CleanArchitecture.Application.Common.Exceptions; | ||
using SSW.CleanArchitecture.Application.Common.Interfaces; | ||
using SSW.CleanArchitecture.Domain.Teams; | ||
|
||
namespace SSW.CleanArchitecture.Application.Features.Teams.Commands.ExecuteMission; | ||
|
||
public sealed record ExecuteMissionCommand(Guid TeamId, string Description) : IRequest<Guid>; | ||
|
||
// ReSharper disable once UnusedType.Global | ||
public sealed class ExecuteMissionCommandHandler(IApplicationDbContext dbContext) | ||
: IRequestHandler<ExecuteMissionCommand, Guid> | ||
{ | ||
public async Task<Guid> Handle(ExecuteMissionCommand 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.ExecuteMission(request.Description); | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
return team.Missions.First().Id.Value; | ||
} | ||
} | ||
|
||
public class ExecuteMissionCommandValidator : AbstractValidator<ExecuteMissionCommand> | ||
{ | ||
public ExecuteMissionCommandValidator() | ||
{ | ||
RuleFor(v => v.TeamId) | ||
.NotEmpty(); | ||
|
||
RuleFor(v => v.Description) | ||
.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
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
40 changes: 40 additions & 0 deletions
40
tests/WebApi.IntegrationTests/Endpoints/Teams/Commands/ExecuteMissionCommandTests.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,40 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using SSW.CleanArchitecture.Domain.Teams; | ||
using SSW.CleanArchitecture.WebApi.Features; | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using WebApi.IntegrationTests.Common.Factories; | ||
using WebApi.IntegrationTests.Common.Fixtures; | ||
|
||
namespace WebApi.IntegrationTests.Endpoints.Teams.Commands; | ||
|
||
public class ExecuteMissionCommandTests(TestingDatabaseFixture fixture, ITestOutputHelper output) | ||
: IntegrationTestBase(fixture, output) | ||
{ | ||
[Fact] | ||
public async Task Command_ShouldExecuteMission() | ||
{ | ||
// Arrange | ||
var hero = HeroFactory.Generate(); | ||
var team = TeamFactory.Generate(); | ||
await AddEntityAsync(hero); | ||
await AddEntityAsync(team); | ||
team.AddHero(hero); | ||
await Context.SaveChangesAsync(); | ||
var teamId = team.Id.Value; | ||
var client = GetAnonymousClient(); | ||
var request = new ExcuteMissionRequest("Save the world"); | ||
|
||
// Act | ||
var result = await client.PostAsJsonAsync($"/api/teams/{teamId}/missions", request); | ||
|
||
// Assert | ||
var response = await result.Content.ReadFromJsonAsync<Guid>(); | ||
var missionId = new MissionId(response); | ||
var mission = await GetQueryable<Mission>().FirstOrDefaultAsync(m => m.Id == missionId); | ||
|
||
result.StatusCode.Should().Be(HttpStatusCode.OK); | ||
mission.Should().NotBeNull(); | ||
mission!.Description.Should().Be(request.Description); | ||
} | ||
} |