Api/Netina.Api/Controller/SeedController.cs

85 lines
3.6 KiB
C#

using Netina.Common.Models.Api;
using Netina.Common.Models.Exception;
using Netina.Domain.CommandQueries.Commands;
using Netina.Domain.Dtos.RequestDtos.SeedDtos;
using Netina.Domain.Dtos.SmallDtos;
namespace Netina.Api.Controller;
public class SeedController : ICarterModule
{
public void AddRoutes(IEndpointRouteBuilder app)
{
//var group = app.NewVersionedApi("Seed")
// .MapGroup("api/seed");
//group.MapPost("categories", SeedCategoriesAsync)
// .WithDisplayName("SeedCategoriesAsync")
// .HasApiVersion(1.0);
//group.MapPost("brands", SeedBrandsAsync)
// .WithDisplayName("SeedBrandsAsync")
// .HasApiVersion(1.0);
//group.MapPost("products",SeedProductsAsync)
// .WithDisplayName("SeedProductsAsync")
// .HasApiVersion(1.0);
}
public async Task<IResult> SeedProductsAsync([FromBody] List<CreateProductCommand> request, [FromQuery] string key, IMediator mediator, CancellationToken cancellationToken)
{
if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==")
throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized);
foreach (var requestDto in request)
{
await mediator.Send(requestDto, cancellationToken);
}
return TypedResults.Ok();
}
public async Task<IResult> SeedCategoriesAsync([FromBody] List<SeedCategoryRequestDto> request, [FromQuery] string key, IMediator mediator,CancellationToken cancellationToken)
{
if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==")
throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized);
Dictionary<int,Guid> categories = new Dictionary<int, Guid>();
var baseCat = await mediator.Send(new CreateProductCategoryCommand("دسته بندی نشده", "محصولات دسته بندی نشده",
true,
default,
new List<StorageFileSDto>()),cancellationToken);
categories.Add(0,baseCat.Id);
foreach (var requestDto in request)
{
var lDto = await mediator.Send(new CreateProductCategoryCommand(requestDto.Name,requestDto.Description,true,default,
new List<StorageFileSDto>()), cancellationToken);
categories.Add(requestDto.BaseCategoryId,lDto.Id);
}
return TypedResults.Ok(categories);
}
public async Task<IResult> SeedBrandsAsync([FromBody] List<SeedBrandRequestDto> request, [FromQuery] string key, IMediator mediator, CancellationToken cancellationToken)
{
if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==")
throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized);
Dictionary<int, Guid> brands = new Dictionary<int, Guid>();
var baseBrand = await mediator.Send(new CreateBrandCommand("بدون برند","NoBrand", "محصولات بدون برند", false,string.Empty,
new List<StorageFileSDto>()), cancellationToken);
brands.Add(0, baseBrand.Id);
foreach (var requestDto in request)
{
var sDto = await mediator.Send(new CreateBrandCommand(requestDto.Name,string.Empty, requestDto.Description, false,
string.Empty, new List<StorageFileSDto>()), cancellationToken);
brands.Add(requestDto.BaseBrandId,sDto.Id);
}
return TypedResults.Ok(brands);
}
}