Skip to content

Commit

Permalink
Getting all beers in brewery works
Browse files Browse the repository at this point in the history
  • Loading branch information
RieBi committed Jul 25, 2024
1 parent e804e13 commit 2c6e8ea
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Api/Controllers/BreweryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ public async Task<IList<BreweryDto>> All()
return breweries;
}

[HttpGet]
[Route("{id}/Beers")]
public async Task<IList<BeerDto>?> Beers(string id)
{
var beers = await _mediator.Send(new GetAllBeersInBreweryQuery(id));

return beers;
}
}
2 changes: 1 addition & 1 deletion Application/DTOs/BeerDto.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Application.DTOs;
internal class BeerDto
public class BeerDto
{
public string Id { get; set; } = default!;
public string BrewerId { get; set; } = default!;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;

namespace Application.Queries.BreweryQueries;
internal class GetAllBeersInBreweryQueryHandler(DataContext context, IMapper mapper) : IRequestHandler<GetAllBeersInBreweryQuery, IList<BeerDto>?>
Expand All @@ -8,11 +9,19 @@ internal class GetAllBeersInBreweryQueryHandler(DataContext context, IMapper map

public async Task<IList<BeerDto>?> Handle(GetAllBeersInBreweryQuery request, CancellationToken cancellationToken)
{
var beers = await _context.Breweries
.Include(f => f.Beers)
var count = await _context.Breweries
.Where(f => f.Id == request.BreweryId)
.Select(f => f.Beers)
.SingleOrDefaultAsync(cancellationToken);
.CountAsync(cancellationToken);

if (count == 0)
return null;

var beers = await _context.Beers
.Include(f => f.Brewer)
.ThenInclude(f => f.Brewery)
.Where(f => f.Brewer.Brewery.Id == request.BreweryId)
.ProjectTo<BeerDto>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);

if (beers is null)
return null;
Expand Down
4 changes: 0 additions & 4 deletions Data/ModelConfigurations/BreweryConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,5 @@ public void Configure(EntityTypeBuilder<Brewery> builder)
builder
.HasMany(f => f.Brewers)
.WithOne(f => f.Brewery);

builder
.HasMany(f => f.Beers)
.WithOne(f => f.Brewer.Brewery);
}
}
1 change: 0 additions & 1 deletion Domain/Models/Brewery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ public class Brewery
public string Address { get; set; } = default!;

public IList<Brewer> Brewers { get; set; } = [];
public IList<Beer> Beers { get; set; } = [];
}

0 comments on commit 2c6e8ea

Please sign in to comment.