2024-04-23 12:09:32 +03:30
|
|
|
|
using Netina.Domain.Entities.Brands;
|
2024-01-13 13:05:37 +03:30
|
|
|
|
|
2024-04-16 20:01:34 +03:30
|
|
|
|
namespace Netina.Repository.Handlers.Brands;
|
2024-01-13 13:05:37 +03:30
|
|
|
|
|
2024-08-07 16:15:53 +03:30
|
|
|
|
public class CreateBrandCommandHandler(IRepositoryWrapper repositoryWrapper,IMartenRepositoryWrapper martenRepositoryWrapper , IMediator mediator) : IRequestHandler<CreateBrandCommand, Guid>
|
2024-01-13 13:05:37 +03:30
|
|
|
|
{
|
2024-06-08 22:51:09 +03:30
|
|
|
|
public async Task<Guid> Handle(CreateBrandCommand request, CancellationToken cancellationToken)
|
2024-01-13 13:05:37 +03:30
|
|
|
|
{
|
2024-02-22 20:34:51 +03:30
|
|
|
|
var ent = Brand.Create(request.PersianName,request.EnglishName, request.Description, request.HasSpecialPage, request.PageUrl);
|
2024-01-13 13:05:37 +03:30
|
|
|
|
foreach (var file in request.Files)
|
|
|
|
|
|
{
|
|
|
|
|
|
ent.AddFile(file.Name, file.FileLocation, file.FileName, file.IsHeader, file.IsPrimary, file.FileType);
|
|
|
|
|
|
}
|
2024-08-07 16:15:53 +03:30
|
|
|
|
repositoryWrapper.SetRepository<Brand>().Add(ent);
|
|
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
await UpdateFaqAsync(ent, request.Faqs, cancellationToken);
|
|
|
|
|
|
|
2024-06-08 22:51:09 +03:30
|
|
|
|
return ent.Id;
|
2024-01-13 13:05:37 +03:30
|
|
|
|
}
|
2024-08-07 16:15:53 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task UpdateFaqAsync(Brand newEnt, Dictionary<string, string> faqs, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
var url = newEnt.GetWebSiteUrl();
|
|
|
|
|
|
var oldFaq = await martenRepositoryWrapper.SetRepository<BaseFaq>()
|
|
|
|
|
|
.GetEntityAsync(f => f.Slug == newEnt.Slug, cancellationToken);
|
|
|
|
|
|
var newFaq = await martenRepositoryWrapper.SetRepository<BaseFaq>()
|
|
|
|
|
|
.GetEntityAsync(f => f.Slug == url, cancellationToken);
|
|
|
|
|
|
if (oldFaq != null)
|
|
|
|
|
|
await mediator.Send(new DeleteFaqCommand(oldFaq.Id), cancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
if (newFaq == null)
|
|
|
|
|
|
await mediator.Send(new CreateFaqCommand(faqs, url, newEnt.EnglishName), cancellationToken);
|
|
|
|
|
|
else
|
|
|
|
|
|
await mediator.Send(new UpdateFaqCommand(newFaq.Id, faqs, url, newEnt.EnglishName), cancellationToken);
|
|
|
|
|
|
}
|
2024-01-13 13:05:37 +03:30
|
|
|
|
}
|