Skip to content

Commit e1e7aa1

Browse files
authored
✨ Added list categories (#115)
Added list categories
1 parent 55a36d0 commit e1e7aa1

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace DDD.Application.Categories.Queries.GetAllCategories;
2+
3+
public record CategoryDto(Guid Id, string Name);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace DDD.Application.Categories.Queries.GetAllCategories;
2+
3+
public record GetAllCategoriesQuery : IRequest<IEnumerable<CategoryDto>>;
4+
5+
public class GetAllCategoriesQueryHandler : IRequestHandler<GetAllCategoriesQuery, IEnumerable<CategoryDto>>
6+
{
7+
private readonly IApplicationDbContext _dbContext;
8+
9+
public GetAllCategoriesQueryHandler(IApplicationDbContext dbContext)
10+
{
11+
_dbContext = dbContext;
12+
}
13+
14+
public async Task<IEnumerable<CategoryDto>> Handle(GetAllCategoriesQuery request, CancellationToken cancellationToken)
15+
{
16+
return await _dbContext.Categories
17+
.Select(c => new CategoryDto(c.Id.Value, c.Name))
18+
.ToListAsync(cancellationToken);
19+
}
20+
}

src/DDD.WebApi/Endpoints/CategoryEndpoints.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using DDD.Application.Categorys.Commands.CreateCategory;
1+
using DDD.Application.Categories.Queries.GetAllCategories;
2+
using DDD.Application.Categorys.Commands.CreateCategory;
23
using DDD.WebApi.Extensions;
34
using MediatR;
45

@@ -13,10 +14,10 @@ public static void MapCategoryEndpoints(this WebApplication app)
1314
.WithTags("Categories")
1415
.WithOpenApi();
1516

16-
//group
17-
// .MapGet("/", async (ISender sender, CancellationToken ct) => await sender.Send(new GetAllProductsQuery(), ct))
18-
// .WithName("GetProducts")
19-
// .ProducesGet<ProductDto[]>();
17+
group
18+
.MapGet("/", async (ISender sender, CancellationToken ct) => await sender.Send(new GetAllCategoriesQuery(), ct))
19+
.WithName("GetCategories")
20+
.ProducesGet<CategoryDto[]>();
2021

2122
group
2223
.MapPost("/", async (ISender sender, CreateCategoryCommand command, CancellationToken ct) => await sender.Send(command, ct))

0 commit comments

Comments
 (0)