2024-04-17 16:19:40 +03:30
|
|
|
|
using Netina.Domain.Dtos.RequestDtos.SeedDtos;
|
2024-04-17 12:24:47 +03:30
|
|
|
|
using Netina.Domain.Entities.Blogs;
|
2024-01-13 13:05:37 +03:30
|
|
|
|
|
2024-04-28 22:10:54 +03:30
|
|
|
|
namespace Netina.Api.Controllers;
|
2024-01-13 13:05:37 +03:30
|
|
|
|
public class SeedController : ICarterModule
|
|
|
|
|
|
{
|
2024-04-17 12:24:47 +03:30
|
|
|
|
private readonly IWebHostEnvironment _environment;
|
|
|
|
|
|
|
|
|
|
|
|
public SeedController(IWebHostEnvironment environment)
|
|
|
|
|
|
{
|
|
|
|
|
|
_environment = environment;
|
|
|
|
|
|
}
|
2024-01-13 13:05:37 +03:30
|
|
|
|
public void AddRoutes(IEndpointRouteBuilder app)
|
|
|
|
|
|
{
|
2024-04-17 12:24:47 +03:30
|
|
|
|
if (_environment.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
var group = app.NewVersionedApi("Seed")
|
|
|
|
|
|
.MapGroup("api/seed");
|
2024-01-13 13:05:37 +03:30
|
|
|
|
|
2024-05-28 10:46:18 +03:30
|
|
|
|
group.MapPost("sitemap", CreateSiteMapAsync)
|
|
|
|
|
|
.HasApiVersion(1.0);
|
2024-03-10 09:51:57 +03:30
|
|
|
|
|
2024-04-17 12:24:47 +03:30
|
|
|
|
group.MapPost("product/categories", SeedCategoriesAsync)
|
|
|
|
|
|
.WithDisplayName("SeedCategoriesAsync")
|
|
|
|
|
|
.HasApiVersion(1.0);
|
2024-03-10 09:27:13 +03:30
|
|
|
|
|
2024-04-17 12:24:47 +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);
|
|
|
|
|
|
|
2024-04-23 23:57:52 +03:30
|
|
|
|
group.MapPost("blog/categories", SeedBlogCategoriesAsync)
|
2024-04-17 12:24:47 +03:30
|
|
|
|
.WithDisplayName("SeedBlogCategoriesAsync")
|
|
|
|
|
|
.HasApiVersion(1.0);
|
|
|
|
|
|
}
|
2024-03-10 09:27:13 +03:30
|
|
|
|
|
2024-01-13 13:05:37 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-28 10:46:18 +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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-13 13:05:37 +03:30
|
|
|
|
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
|
|
|
|
|
2024-01-13 13:05:37 +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>();
|
2024-01-28 10:11:48 +03:30
|
|
|
|
var baseCat = await mediator.Send(new CreateProductCategoryCommand("دسته بندی نشده", "محصولات دسته بندی نشده",
|
|
|
|
|
|
true,
|
|
|
|
|
|
default,
|
2024-08-07 16:15:53 +03:30
|
|
|
|
new List<StorageFileSDto>(),
|
|
|
|
|
|
new Dictionary<string, string>(),
|
|
|
|
|
|
new Dictionary<string, string>()),cancellationToken);
|
2024-06-07 23:03:54 +03:30
|
|
|
|
categories.Add(0,baseCat);
|
2024-01-13 13:05:37 +03:30
|
|
|
|
foreach (var requestDto in request)
|
|
|
|
|
|
{
|
2024-01-28 10:11:48 +03:30
|
|
|
|
var lDto = await mediator.Send(new CreateProductCategoryCommand(requestDto.Name,requestDto.Description,true,default,
|
2024-08-07 16:15:53 +03:30
|
|
|
|
new List<StorageFileSDto>(),new Dictionary<string, string>(),new Dictionary<string, string>()), cancellationToken);
|
2024-06-07 23:03:54 +03:30
|
|
|
|
categories.Add(requestDto.BaseCategoryId,lDto);
|
2024-01-13 13:05:37 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>();
|
2024-08-07 16:15:53 +03:30
|
|
|
|
var baseBrand = await mediator.Send(new CreateBrandCommand("بدون برند","NoBrand",
|
|
|
|
|
|
"محصولات بدون برند",
|
|
|
|
|
|
false,
|
|
|
|
|
|
string.Empty,
|
|
|
|
|
|
new List<StorageFileSDto>(),
|
|
|
|
|
|
new Dictionary<string, string>(),
|
|
|
|
|
|
new Dictionary<string, string>()), cancellationToken);
|
2024-06-08 22:51:09 +03:30
|
|
|
|
brands.Add(0, baseBrand);
|
2024-01-13 13:05:37 +03:30
|
|
|
|
foreach (var requestDto in request)
|
|
|
|
|
|
{
|
2024-02-22 20:34:51 +03:30
|
|
|
|
var sDto = await mediator.Send(new CreateBrandCommand(requestDto.Name,string.Empty, requestDto.Description, false,
|
2024-08-07 16:15:53 +03:30
|
|
|
|
string.Empty, new List<StorageFileSDto>(),new Dictionary<string, string>(),new Dictionary<string, string>()), cancellationToken);
|
2024-06-08 22:51:09 +03:30
|
|
|
|
brands.Add(requestDto.BaseBrandId,sDto);
|
2024-01-13 13:05:37 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return TypedResults.Ok(brands);
|
|
|
|
|
|
}
|
2024-04-17 12:24:47 +03:30
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2024-04-22 13:33:39 +03:30
|
|
|
|
if (seedBlogRequestDto.CategoryId == default)
|
|
|
|
|
|
{
|
|
|
|
|
|
var noCategory = await repositoryWrapper.SetRepository<BlogCategory>()
|
|
|
|
|
|
.TableNoTracking
|
|
|
|
|
|
.FirstOrDefaultAsync(bc => bc.Name == "دسته بندی نشده", cancellationToken);
|
2024-04-28 22:10:54 +03:30
|
|
|
|
if (noCategory != null)
|
2024-04-22 13:33:39 +03:30
|
|
|
|
seedBlogRequestDto.CategoryId = noCategory.Id;
|
|
|
|
|
|
}
|
2024-04-28 22:10:54 +03:30
|
|
|
|
var ent = Blog.Create(seedBlogRequestDto.Title, seedBlogRequestDto.Slug, seedBlogRequestDto.Content, seedBlogRequestDto.Tags, seedBlogRequestDto.ReadingTime,
|
2024-04-17 12:24:47 +03:30
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-17 12:24:47 +03:30
|
|
|
|
repositoryWrapper.SetRepository<Blog>().Add(ent);
|
|
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
}
|
2024-04-28 22:10:54 +03:30
|
|
|
|
//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);
|
|
|
|
|
|
//}
|
2024-04-17 12:24:47 +03:30
|
|
|
|
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>();
|
2024-05-07 11:01:55 +03:30
|
|
|
|
var baseCategory = BlogCategory.Create("دسته بندی نشده","دسته بندی برای بلاگ های دسته بندی نشده",true);
|
2024-04-17 12:24:47 +03:30
|
|
|
|
repositoryWrapper.SetRepository<BlogCategory>().Add(baseCategory);
|
|
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
categories.Add(0, baseCategory.Id);
|
|
|
|
|
|
foreach (var requestDto in request)
|
|
|
|
|
|
{
|
2024-05-07 11:01:55 +03:30
|
|
|
|
var ent = BlogCategory.Create(requestDto.Name,requestDto.Slug, requestDto.Description,false);
|
2024-04-17 12:24:47 +03:30
|
|
|
|
repositoryWrapper.SetRepository<BlogCategory>().Add(ent);
|
|
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
categories.Add(requestDto.BaseCategoryId, ent.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return TypedResults.Ok(categories);
|
|
|
|
|
|
}
|
2024-01-13 13:05:37 +03:30
|
|
|
|
}
|