-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathCreateCategoryItemHandler.cs
More file actions
28 lines (26 loc) · 1.12 KB
/
CreateCategoryItemHandler.cs
File metadata and controls
28 lines (26 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Category.Domain;
using FSH.Framework.Core.Persistence;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Category.Features.Create.v1;
public sealed class CreateCategoryItemHandler(
ILogger<CreateCategoryItemHandler> logger,
[FromKeyedServices("categoryItem")] IRepository<CategoryItem> repository)
: IRequestHandler<CreateCategoryItemCommand, CreateCategoryItemResponse>
{
public async Task<CreateCategoryItemResponse> Handle(CreateCategoryItemCommand request, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
var item = CategoryItem.Create(request.Name, request.Description);
await repository.AddAsync(item, cancellationToken).ConfigureAwait(false);
await repository.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
logger.LogInformation("CategoryItem item created {CategoryItemId}", item.Id);
return new CreateCategoryItemResponse(item.Id);
}
}