Skip to content

Commit

Permalink
✨ Added Get Teams Query (#322)
Browse files Browse the repository at this point in the history
Added Get Teams Query
  • Loading branch information
danielmackay authored May 28, 2024
1 parent f8a24a0 commit cf28e78
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
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);
}
}
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 src/Application/Features/Teams/Queries/GetAllTeams/TeamDto.cs
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; }
}
10 changes: 10 additions & 0 deletions src/WebApi/Features/TeamEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MediatR;
using SSW.CleanArchitecture.Application.Features.Teams.Commands.CreateTeam;
using SSW.CleanArchitecture.Application.Features.Teams.Queries.GetAllTeams;
using SSW.CleanArchitecture.WebApi.Extensions;

namespace SSW.CleanArchitecture.WebApi.Features;
Expand All @@ -18,5 +19,14 @@ public static void MapTeamEndpoints(this WebApplication app)
})
.WithName("CreateTeam")
.ProducesPost();

group
.MapGet("/", async (ISender sender, CancellationToken ct) =>
{
var results = await sender.Send(new GetAllTeamsQuery(), ct);
return Results.Ok(results);
})
.WithName("GetAllTeams")
.ProducesGet<TeamDto[]>();
}
}
4 changes: 2 additions & 2 deletions tests/WebApi.IntegrationTests/Common/Factories/HeroFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public static class HeroFactory
private static readonly Faker<Hero> HeroFaker = new Faker<Hero>().CustomInstantiator(f =>
{
var hero = Hero.Create(
f.Company.CompanyName(),
f.Company.Bs()
f.Person.FullName,
f.Person.FirstName
);

var powers = PowerFaker
Expand Down
20 changes: 20 additions & 0 deletions tests/WebApi.IntegrationTests/Common/Factories/TeamFactory.cs
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);
}
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();
}
}

0 comments on commit cf28e78

Please sign in to comment.