using Netina.Domain.Dtos.RequestDtos.SeedDtos; using Netina.Domain.Entities.Blogs; namespace Netina.Api.Controllers; public class SeedController : ICarterModule { private readonly IWebHostEnvironment _environment; public SeedController(IWebHostEnvironment environment) { _environment = environment; } public void AddRoutes(IEndpointRouteBuilder app) { if (_environment.IsDevelopment()) { var group = app.NewVersionedApi("Seed") .MapGroup("api/seed"); group.MapPost("sitemap", CreateSiteMapAsync) .HasApiVersion(1.0); group.MapPost("product/categories", SeedCategoriesAsync) .WithDisplayName("SeedCategoriesAsync") .HasApiVersion(1.0); group.MapPost("product/brands", SeedBrandsAsync) .WithDisplayName("SeedBrandsAsync") .HasApiVersion(1.0); group.MapPost("products", SeedProductsAsync) .WithDisplayName("SeedProductsAsync") .HasApiVersion(1.0); group.MapPost("blogs", SeedBlogsAsync) .WithDisplayName("SeedBlogsAsync") .HasApiVersion(1.0); group.MapPost("blog/categories", SeedBlogCategoriesAsync) .WithDisplayName("SeedBlogCategoriesAsync") .HasApiVersion(1.0); } } public async Task CreateSiteMapAsync([FromQuery] string key, ISiteMapService siteMapService, CancellationToken cancellationToken) { if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==") throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized); await siteMapService.CreateSiteMapAsync(); return TypedResults.Ok(); } 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); 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); } 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("بدون برند","NoBrand", "محصولات بدون برند", false,string.Empty, new List()), 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()), cancellationToken); brands.Add(requestDto.BaseBrandId,sDto.Id); } return TypedResults.Ok(brands); } public async Task SeedBlogsAsync([FromBody] List request, [FromQuery]string key,IRepositoryWrapper repositoryWrapper,CancellationToken cancellationToken) { if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==") throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized); foreach (var seedBlogRequestDto in request) { if (seedBlogRequestDto.CategoryId == default) { var noCategory = await repositoryWrapper.SetRepository() .TableNoTracking .FirstOrDefaultAsync(bc => bc.Name == "دسته بندی نشده", cancellationToken); if (noCategory != null) seedBlogRequestDto.CategoryId = noCategory.Id; } var ent = Blog.Create(seedBlogRequestDto.Title, seedBlogRequestDto.Slug, seedBlogRequestDto.Content, seedBlogRequestDto.Tags, seedBlogRequestDto.ReadingTime, seedBlogRequestDto.Summery, seedBlogRequestDto.IsSuggested, seedBlogRequestDto.CategoryId); foreach (var storageFileSDto in seedBlogRequestDto.Files) { ent.AddFile(storageFileSDto.Name, storageFileSDto.FileLocation, storageFileSDto.FileName, true, true, storageFileSDto.FileType); } repositoryWrapper.SetRepository().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); } //var blogs = await repositoryWrapper.SetRepository() // .TableNoTracking // .ToListAsync(cancellationToken); //foreach (var blog in blogs) //{ // if (blog.Id == Guid.Parse("358192ec-32b0-4643-8269-b42268755b5b")) // { // } // blog.Content = blog.Content.Replace(@"

(10 خط ) 02144850335📞  02144872230 📞  02144872231 📞

", string.Empty); // blog.Content = blog.Content.Replace(@"

(10 خط ) 02144850335📞  02144872230 📞  02144872231 📞

", string.Empty); // blog.Content = blog.Content.Replace(@"

تلفن های تماس :

", string.Empty); // blog.Content = blog.Content.Replace("02122894819", string.Empty); // blog.Content = blog.Content.Replace("02144850335", string.Empty); // blog.Content = blog.Content.Replace("02126702518", string.Empty); // blog.Content = blog.Content.Replace("02144872230", string.Empty); // blog.Content = blog.Content.Replace("02144872231", string.Empty); // blog.Content = blog.Content.Replace("02144850335", string.Empty); // blog.Content = blog.Content.Replace("02144872230", string.Empty); // blog.Content = blog.Content.Replace("02144872231", string.Empty); // blog.Content = blog.Content.Replace("۰۲۱۴۴۸۵۰۳۳۵", string.Empty); // blog.Content = blog.Content.Replace("۰۲۱۴۴۸۷۲۲۳۰", string.Empty); // blog.Content = blog.Content.Replace("۰۲۱۴۴۸۷۲۲۳۱", string.Empty); // repositoryWrapper.SetRepository() // .Update(blog); // await repositoryWrapper.SaveChangesAsync(cancellationToken); //} return TypedResults.Ok(); } public async Task SeedBlogCategoriesAsync([FromBody] List request, [FromQuery] string key, [FromServices]IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==") throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized); Dictionary categories = new Dictionary(); var baseCategory = BlogCategory.Create("دسته بندی نشده","دسته بندی برای بلاگ های دسته بندی نشده",true); repositoryWrapper.SetRepository().Add(baseCategory); await repositoryWrapper.SaveChangesAsync(cancellationToken); categories.Add(0, baseCategory.Id); foreach (var requestDto in request) { var ent = BlogCategory.Create(requestDto.Name,requestDto.Slug, requestDto.Description,false); repositoryWrapper.SetRepository().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); categories.Add(requestDto.BaseCategoryId, ent.Id); } return TypedResults.Ok(categories); } }