-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Application/Features/Teams/Queries/GetAllTeams/GetAllHeroesQuery.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,21 @@ | ||
using AutoMapper.QueryableExtensions; | ||
using Microsoft.EntityFrameworkCore; | ||
using SSW.CleanArchitecture.Application.Common.Interfaces; | ||
|
||
namespace SSW.CleanArchitecture.Application.Features.Teams.Queries.GetAllTeams; | ||
|
||
public record GetAllTeamsQuery : IRequest<IReadOnlyList<TeamDto>>; | ||
|
||
public sealed class GetAllTeamsQueryHandler( | ||
IMapper mapper, | ||
IApplicationDbContext dbContext) : IRequestHandler<GetAllTeamsQuery, IReadOnlyList<TeamDto>> | ||
{ | ||
public async Task<IReadOnlyList<TeamDto>> Handle( | ||
GetAllTeamsQuery request, | ||
CancellationToken cancellationToken) | ||
{ | ||
return await dbContext.Teams | ||
.ProjectTo<TeamDto>(mapper.ConfigurationProvider) | ||
.ToListAsync(cancellationToken); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Application/Features/Teams/Queries/GetAllTeams/GetAllTeamsMapping.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,12 @@ | ||
using SSW.CleanArchitecture.Domain.Teams; | ||
|
||
namespace SSW.CleanArchitecture.Application.Features.Teams.Queries.GetAllTeams; | ||
|
||
public class GetAllTeamsMapping : Profile | ||
{ | ||
public GetAllTeamsMapping() | ||
{ | ||
CreateMap<Team, TeamDto>() | ||
.ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id.Value)); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Application/Features/Teams/Queries/GetAllTeams/TeamDto.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,7 @@ | ||
namespace SSW.CleanArchitecture.Application.Features.Teams.Queries.GetAllTeams; | ||
|
||
public class TeamDto | ||
{ | ||
public Guid Id { get; init; } | ||
public required string Name { get; init; } | ||
} |
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
20 changes: 20 additions & 0 deletions
20
tests/WebApi.IntegrationTests/Common/Factories/TeamFactory.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,20 @@ | ||
using Bogus; | ||
using SSW.CleanArchitecture.Domain.Teams; | ||
|
||
namespace WebApi.IntegrationTests.Common.Factories; | ||
|
||
public static class TeamFactory | ||
{ | ||
private static readonly Faker<Team> TeamFaker = new Faker<Team>().CustomInstantiator(f => | ||
{ | ||
var team = Team.Create( | ||
f.Company.CompanyName() | ||
); | ||
|
||
return team; | ||
}); | ||
|
||
public static Team Generate() => TeamFaker.Generate(); | ||
|
||
public static IReadOnlyList<Team> Generate(int count) => TeamFaker.Generate(count); | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/WebApi.IntegrationTests/Endpoints/Teams/Queries/GetAllHeroesQueryTests.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,32 @@ | ||
using SSW.CleanArchitecture.Application.Features.Heroes.Queries.GetAllHeroes; | ||
using SSW.CleanArchitecture.Application.Features.Teams.Queries.GetAllTeams; | ||
using System.Net.Http.Json; | ||
using WebApi.IntegrationTests.Common.Factories; | ||
using WebApi.IntegrationTests.Common.Fixtures; | ||
|
||
namespace WebApi.IntegrationTests.Endpoints.Teams.Queries; | ||
|
||
public class GetAllTeamsQueryTests(TestingDatabaseFixture fixture, ITestOutputHelper output) | ||
: IntegrationTestBase(fixture, output) | ||
{ | ||
[Fact] | ||
public async Task Query_ShouldReturnAllTeams() | ||
{ | ||
// Arrange | ||
const int entityCount = 10; | ||
var entities = TeamFactory.Generate(entityCount); | ||
await AddEntitiesAsync(entities); | ||
var client = GetAnonymousClient(); | ||
|
||
// Act | ||
var result = await client.GetFromJsonAsync<TeamDto[]>("/api/teams"); | ||
|
||
// Assert | ||
result.Should().NotBeNull(); | ||
result!.Length.Should().Be(entityCount); | ||
|
||
var firstTeam = result.First(); | ||
firstTeam.Id.Should().NotBeEmpty(); | ||
firstTeam.Name.Should().NotBeEmpty(); | ||
} | ||
} |