using NetinaShop.Domain.Dtos.RequestDtos.SeedDtos; namespace NetinaShop.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 SeedProductsAsync([FromBody] List 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 SeedCategoriesAsync([FromBody] List request, [FromQuery] string key, IMediator mediator,CancellationToken cancellationToken) { if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==") throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized); Dictionary categories = new Dictionary(); var baseCat = await mediator.Send(new CreateProductCategoryCommand("دسته بندی نشده", "محصولات دسته بندی نشده", true, default, new List()),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()), cancellationToken); categories.Add(requestDto.BaseCategoryId,lDto.Id); } return TypedResults.Ok(categories); } public async Task SeedBrandsAsync([FromBody] List request, [FromQuery] string key, IMediator mediator, CancellationToken cancellationToken) { if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==") throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized); Dictionary brands = new Dictionary(); var baseBrand = await mediator.Send(new CreateBrandCommand("بدون برند", "محصولات بدون برند", false,string.Empty, new List())); brands.Add(0, baseBrand.Id); foreach (var requestDto in request) { var sDto = await mediator.Send(new CreateBrandCommand(requestDto.Name, requestDto.Description, false, string.Empty, new List())); brands.Add(requestDto.BaseBrandId,sDto.Id); } return TypedResults.Ok(brands); } }