Api/Netina.Api/Controllers/SeedController.cs

193 lines
9.4 KiB
C#
Raw Normal View History

2024-04-17 16:19:40 +03:30
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);
2024-03-10 09:51:57 +03:30
group.MapPost("product/categories", SeedCategoriesAsync)
.WithDisplayName("SeedCategoriesAsync")
.HasApiVersion(1.0);
2024-03-10 09:27:13 +03:30
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);
}
2024-03-10 09:27:13 +03:30
}
public async Task<IResult> 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<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();
}
2024-04-17 16:19:40 +03:30
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);
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);
}
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);
}
public async Task<IResult> SeedBlogsAsync([FromBody] List<SeedBlogRequestDto> 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<BlogCategory>()
.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);
2024-04-17 16:19:40 +03:30
foreach (var storageFileSDto in seedBlogRequestDto.Files)
{
ent.AddFile(storageFileSDto.Name, storageFileSDto.FileLocation, storageFileSDto.FileName, true, true,
storageFileSDto.FileType);
}
repositoryWrapper.SetRepository<Blog>().Add(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
//var blogs = await repositoryWrapper.SetRepository<Blog>()
// .TableNoTracking
// .ToListAsync(cancellationToken);
//foreach (var blog in blogs)
//{
// if (blog.Id == Guid.Parse("358192ec-32b0-4643-8269-b42268755b5b"))
// {
// }
// blog.Content = blog.Content.Replace(@"<p style=""text-align: center;""><strong>(10 خط ) </strong><a href=""tel:02144850335"">02144850335</a>📞  <a href=""tel:02144872230"">02144872230</a> 📞  <a href=""tel:02144872230"">02144872231</a> 📞</p>", string.Empty);
// blog.Content = blog.Content.Replace(@"<p style=""text-align: center;""><strong>(10 خط ) </strong><a href=""tel:02144850335"">02144850335</a>📞  <a href=""tel:02144872230"">02144872230</a> 📞  <a href=""tel:02144872230"">02144872231</a> 📞</p>", string.Empty);
// blog.Content = blog.Content.Replace(@"<p style=""text-align: center;"">تلفن های تماس :</p>", 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<Blog>()
// .Update(blog);
// await repositoryWrapper.SaveChangesAsync(cancellationToken);
//}
return TypedResults.Ok();
}
public async Task<IResult> SeedBlogCategoriesAsync([FromBody] List<SeedBlogCategoryRequestDto> request, [FromQuery] string key, [FromServices]IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
{
if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==")
throw new AppException("Key is not valid", ApiResultStatusCode.UnAuthorized);
Dictionary<int, Guid> categories = new Dictionary<int, Guid>();
var baseCategory = BlogCategory.Create("دسته بندی نشده","دسته بندی برای بلاگ های دسته بندی نشده",true);
repositoryWrapper.SetRepository<BlogCategory>().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<BlogCategory>().Add(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
categories.Add(requestDto.BaseCategoryId, ent.Id);
}
return TypedResults.Ok(categories);
}
}