Compare commits

...

2 Commits

30 changed files with 3760 additions and 205 deletions

View File

@ -1 +1 @@
0.22.25.43
0.22.25.44

View File

@ -0,0 +1,78 @@
{
"ConnectionStrings": {
"PostgresServer": "User ID=postgres;Password=root;Host=localhost;Port=5432;Database=iGarsonDB;",
"Postgres": "Host=pg-0,pg-1;Username=igarsonAgent;Password=xHTpBf4wC+bBeNg2pL6Ga7VEWKFJx7VPEUpqxwPFfOc2YYTVwFQuHfsiqoVeT9+6;Database=NetinaShopDB02;Load Balance Hosts=true;Target Session Attributes=primary;Application Name=iGLS",
"MartenDB": "Host=pg-0,pg-1;Username=igarsonAgent;Password=xHTpBf4wC+bBeNg2pL6Ga7VEWKFJx7VPEUpqxwPFfOc2YYTVwFQuHfsiqoVeT9+6;Database=NetinaShopMartenDB;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "None",
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.AspNetCore.SignalR": "Debug",
"Microsoft.AspNetCore.Http.Connections": "Debug"
}
},
"ShopSettings": {
"TaxesFee": 9
},
"SiteSettings": {
"BaseUrl": "http://localhost:32770",
"WebSiteUrl": "https://hamyanedalat.com",
"AdminPanelBaseUrl": "https://admin.hamyanedalat.com",
"StorageBaseUrl": "https://storage.hamyanedalat.com",
"KaveNegarApiKey": "3735494B4143727A794346457461576A2B4B6668414973424E333561505A694B",
"UserSetting": {
"Username": "09214802813",
"Email": "avvampier@gmail.com",
"Password": "eF79o4P4BopCUbUK",
"Phone": "09214802813",
"RoleName": "RootAdmin",
"FirstName": "همه کاره",
"LastName": "سیستم"
},
"Manager": {
"Username": "09122171024",
"Email": "info@hamyanedalat.com",
"Password": "eF79o4P4BopCUbUK",
"Phone": "09122171024",
"RoleName": "Manager",
"FirstName": "کامران",
"LastName": "میرزایی"
},
"StorageSetting": {
"AccessKey": "de129835-b43a-4552-8ed4-865e2635ab3d",
"SecretKey": "f00197a766336e818023f90408ba3b88b4fdc86d0e7180f74f199d9f03db7575",
"BucketKey": "hamyan-content"
},
"JwtSettings": {
"SecretKey": "YAEMAMZAMAN_KHODET_NEGAHDAR_IN_KEY_BASH_nw+8E0EABj0Wg8c4mHg/bDBf5qGMhmBPb6u16DVe9/MzYva1e+/J1zImyIoQX2Lmra2kvzsIjGiwP7r3Znd_YA_JADE_NASABE_v+Ro/CDixScDv6EkpZnkBv9MFdPnSmFXNGMH9gA1BzQUoC1iSX9Aq+pMIw/cMKXI9WA==_YA_HUSEIN_SEYED_SHOHADA_BE_OMID_KHODET",
"Issuer": "HamaynEdalat",
"Audience": "HamaynEdalat",
"ExpireAddDay": "15"
}
},
"IpRateLimiting": {
"EnableEndpointRateLimiting": false,
"StackBlockedRequests": false,
"RealIpHeader": "X-Real-IP",
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
"IpWhitelist": [ "127.0.0.1", "::1/10", "192.168.0.0/24" ],
"EndpointWhitelist": [ "get:/api/license", "*:/api/status" ],
"ClientWhitelist": [ "dev-id-1", "dev-id-2" ],
"GeneralRules": [
{
"Endpoint": "*",
"Period": "1m",
"Limit": 60
},
{
"Endpoint": "*",
"Period": "15m",
"Limit": 250
}
]
},
"AllowedHosts": "*"
}

View File

@ -47,8 +47,8 @@
},
"JwtSettings": {
"SecretKey": "YAEMAMZAMAN_KHODET_NEGAHDAR_IN_KEY_BASH_nw+8E0EABj0Wg8c4mHg/bDBf5qGMhmBPb6u16DVe9/MzYva1e+/J1zImyIoQX2Lmra2kvzsIjGiwP7r3Znd_YA_JADE_NASABE_v+Ro/CDixScDv6EkpZnkBv9MFdPnSmFXNGMH9gA1BzQUoC1iSX9Aq+pMIw/cMKXI9WA==_YA_HUSEIN_SEYED_SHOHADA_BE_OMID_KHODET",
"Issuer": "NetinaShop",
"Audience": "NetinaShop",
"Issuer": "Vesmeh",
"Audience": "Vesmeh",
"ExpireAddDay": "15"
}
},

View File

@ -10,8 +10,7 @@ public class FileController : ICarterModule
public void AddRoutes(IEndpointRouteBuilder app)
{
var group = app.NewVersionedApi("File")
.MapGroup("api/file")
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
.MapGroup("api/file");
group.MapGet("", GetFilesAsync)
.WithDisplayName("GetFilesAsync")
@ -20,7 +19,6 @@ public class FileController : ICarterModule
group.MapPost("", UploadFileAsync)
.WithDisplayName("UploadFileAsync")
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageFiles))
.HasApiVersion(1.0);
}

View File

@ -1,4 +1,5 @@
using Netina.Core.Abstracts;
using Microsoft.Extensions.Options;
using Netina.Core.Abstracts;
using Netina.Domain.CommandQueries.Queries;
using Netina.Domain.Models.Claims;
using TypedResults = Microsoft.AspNetCore.Http.TypedResults;
@ -36,16 +37,17 @@ public class PaymentController : ICarterModule
=> TypedResults.Ok(await mediator.Send(new GetShippingQuery(id), cancellationToken));
// POST:Create Entity
public async Task<IResult> VerifyPaymentAsync([FromQuery] string Authority, [FromQuery] string Status, IPaymentService paymentService, ILogger<PaymentController> logger, CancellationToken cancellationToken)
public async Task<IResult> VerifyPaymentAsync([FromQuery] string Authority, [FromQuery] string Status, IPaymentService paymentService, IOptionsSnapshot<SiteSettings> optionsSnapshot, ILogger<PaymentController> logger, CancellationToken cancellationToken)
{
var siteUrl = optionsSnapshot.Value.WebSiteUrl;
if (Status == "OK")
{
var result = await paymentService.VerifyPaymentAsync(authority: Authority, cancellationToken);
return TypedResults.Redirect($"https://vesmeh.com/purchase-callback?refid={result.Item1}&paymentStatus=true&factorNumber={result.Item2}", true);
return TypedResults.Redirect($"{siteUrl}/purchase-callback?refid={result.Item1}&paymentStatus=true&factorNumber={result.Item2}", true);
}
else
{
return TypedResults.Redirect($"https://vesmeh.com/purchase-callback?refid=0&paymentStatus=false&factorNumber=0", true);
return TypedResults.Redirect($"{siteUrl}/purchase-callback?refid=0&paymentStatus=false&factorNumber=0", true);
}
}

View File

@ -11,8 +11,25 @@ public class SearchController : ICarterModule
group.MapGet("", SearchAsync)
.WithDisplayName("Search Async")
.HasApiVersion(1.0);
group.MapGet("zarehbin", ZarehbinAsync)
.WithDisplayName("Search Async")
.HasApiVersion(1.0);
}
private async Task<IResult> SearchAsync([FromQuery] string name, [FromServices] ISearchService searchService, CancellationToken cancellationToken)
=> TypedResults.Ok(await searchService.SearchAsync(name, cancellationToken));
private async Task<IResult> ZarehbinAsync([FromQuery] string? product_id, [FromQuery] int? page, [FromServices] ISearchService searchService, CancellationToken cancellationToken)
{
if (product_id == null)
{
if (page == null)
return TypedResults.Ok(await searchService.ZarehbinAsync(0, cancellationToken));
else
return TypedResults.Ok(await searchService.ZarehbinAsync(page.Value, cancellationToken));
}
else
return TypedResults.Ok(await searchService.ZarehbinAsync(Guid.Parse(product_id), cancellationToken));
}
}

View File

@ -1,13 +1,7 @@
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;
using Netina.Domain.Dtos.RequestDtos.SeedDtos;
using Netina.Domain.Entities.Blogs;
using Netina.Repository.Repositories.Base;
namespace Netina.Api.Controller;
public class SeedController : ICarterModule
{
private readonly IWebHostEnvironment _environment;
@ -63,6 +57,7 @@ public class SeedController : ICarterModule
return TypedResults.Ok();
}
public async Task<IResult> SeedCategoriesAsync([FromBody] List<SeedCategoryRequestDto> request, [FromQuery] string key, IMediator mediator,CancellationToken cancellationToken)
{
if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==")
@ -112,6 +107,13 @@ public class SeedController : ICarterModule
{
var ent = Blog.Create(seedBlogRequestDto.Title, 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<Blog>().Add(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}

View File

@ -6,8 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<AssemblyVersion>0.22.25.43</AssemblyVersion>
<FileVersion>0.22.25.43</FileVersion>
<AssemblyVersion>0.22.25.44</AssemblyVersion>
<FileVersion>0.22.25.44</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -1,12 +1,3 @@
using Netina.Api.WebFramework.Configurations;
using Netina.Api.WebFramework.MiddleWares;
using Netina.Api.WebFramework.Swagger;
using Netina.Common.Models;
using Netina.Core;
using Netina.Domain;
using Netina.Domain.Models.Settings;
using Netina.Infrastructure;
using Netina.Repository;
using Netina.Repository.Behaviors;
var builder = WebApplication.CreateBuilder(args);
@ -15,10 +6,9 @@ builder.Host.UseSerilog();
LoggerConfig.ConfigureSerilog();
string env = builder.Environment.IsDevelopment() == true ? "Development" : "Production";
builder.Host.UseContentRoot(Directory.GetCurrentDirectory());
string projectName = "Vesmeh";
if (builder.Environment.IsDevelopment())
builder.Configuration
.AddJsonFile($"AppSettings/appsettings.json")
.AddJsonFile($"AppSettings/appsettings.{env}.json");
builder.Configuration.AddJsonFile($"AppSettings/appsettings.json").AddJsonFile($"AppSettings/appsettings.{env}{projectName}.json");
if (builder.Environment.IsProduction())
builder.Configuration.AddJsonFile($"AppSettings/Production/appsettings.{env}.json");

View File

@ -1,9 +1,10 @@
using Netina.Common.Models;
using Netina.Domain.Dtos.ResponseDtos;
using Netina.Domain.Dtos.ResponseDtos.Zarehbin;
namespace Netina.Core.CoreServices.Abstracts;
public interface ISearchService : IScopedDependency
{
public Task<SearchResponseDto> SearchAsync(string name,CancellationToken cancellationToken = default);
public Task<ZarehbinPagedResponseDto> ZarehbinAsync(int page,CancellationToken cancellationToken = default);
public Task<ZarehbinProductResponseDto> ZarehbinAsync(Guid productId,CancellationToken cancellationToken = default);
}

View File

@ -1,8 +1,5 @@
using Netina.Core.CoreServices.Abstracts;
using Netina.Domain.Dtos.ResponseDtos;
using Netina.Domain.Dtos.ResponseDtos.Zarehbin;
using Netina.Domain.Entities.ProductCategories;
using Netina.Domain.Entities.Products;
using Netina.Repository.Repositories.Base.Contracts;
namespace Netina.Core.CoreServices;
@ -34,4 +31,14 @@ public class SearchService : ISearchService
Categories = categories.Select(c => c.Adapt<SearchedProductCategoryResponseDto>()).ToList()
};
}
public Task<ZarehbinPagedResponseDto> ZarehbinAsync(int page, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<ZarehbinProductResponseDto> ZarehbinAsync(Guid productId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}

View File

@ -104,11 +104,11 @@ public class GetOrderInvoiceCommandHandler : IRequestHandler<GetOrderInvoiceComm
orderAtText.StringFormat = format;
result = orderAtText.Draw(currentPage, new PointF(clientSize.Width - 100, result.Bounds.Y + 30));
var nameText = new PdfTextElement($"{order.UserFullName}", _mediumFont);
var nameText = new PdfTextElement($"{order.CustomerFullName}", _mediumFont);
nameText.StringFormat = format;
result = nameText.Draw(currentPage, new PointF(clientSize.Width - 100, result.Bounds.Bottom));
var phoneText = new PdfTextElement($"{order.UserPhoneNumber}", _mediumFont);
var phoneText = new PdfTextElement($"{order.CustomerPhoneNumber}", _mediumFont);
phoneText.StringFormat = new PdfStringFormat(PdfTextAlignment.Left);
result = phoneText.Draw(currentPage, new PointF(0, result.Bounds.Y));

View File

@ -9,10 +9,13 @@ public static class ImageConvertor
public static async Task<Stream> ImageResize(this FileUploadRequest fileUpload, Stream input, Stream output, int newWidth)
{
using var image = await Image.LoadAsync(input);
var height_width = image.Width /image.Height;
var new_Height = newWidth * height_width;
image.Mutate(x => x.Resize(newWidth, new_Height));
image.Mutate(x => x.Resize(newWidth, new_Height));
if (image.Width > newWidth)
{
double ratio = (double)newWidth / image.Width;
int new_Height = (int)Math.Round(image.Height * ratio);
image.Mutate(x => x.Resize(newWidth, new_Height));
image.Mutate(x => x.Resize(newWidth, new_Height));
}
await image.SaveAsJpegAsync(output);
return output;
}

View File

@ -1,9 +1,4 @@
using Netina.Common.Models.Mapper;
using Netina.Domain.Dtos.SmallDtos;
using Netina.Domain.Entities.Orders;
using Netina.Domain.Enums;
namespace Netina.Domain.Dtos.LargDtos;
namespace Netina.Domain.Dtos.LargDtos;
public class OrderLDto : BaseDto<OrderLDto,Order>
{
public string FactorCode { get; set; } = string.Empty;
@ -24,8 +19,8 @@ public class OrderLDto : BaseDto<OrderLDto,Order>
public string DiscountCode { get; set; } = string.Empty;
public long TotalPriceWithoutDiscount => TotalPrice + DiscountPrice;
public string UserFullName { get; set; } = string.Empty;
public string UserPhoneNumber { get; set; } = string.Empty;
public string CustomerFullName { get; set; } = string.Empty;
public string CustomerPhoneNumber { get; set; } = string.Empty;
public List<OrderProductSDto> OrderProducts { get; set; } = new();

View File

@ -9,6 +9,7 @@ public class SeedBlogRequestDto
public string Summery { get; set; } = string.Empty;
public bool IsSuggested { get; set; }
public Guid CategoryId { get; set; }
public List<StorageFileSDto> Files { get; set; } = new();
}
public class SeedBlogCategoryRequestDto

View File

@ -0,0 +1,26 @@
namespace Netina.Domain.Dtos.ResponseDtos.Zarehbin;
public class ZarehbinPagedResponseDto
{
public int count { get; set; }
public int total_pages_count { get; set; }
public List<ZarehbinProductResponseDto> products { get; set; } = new();
}
public class ZarehbinProductResponseDto
{
public string title { get; set; } = string.Empty;
public string subtitle { get; set; } = string.Empty;
public string id { get; set; } = string.Empty;
public string current_price { get; set; } = string.Empty;
public string old_price { get; set; } = string.Empty;
public string availability { get; set; } = string.Empty;
public List<string> categories { get; set; } = new();
public string image_link { get; set; } = string.Empty;
public List<string> image_links { get; set; } = new();
public string page_url { get; set; } = string.Empty;
public string short_desc { get; set; } = string.Empty;
public List<Tuple<string, string>> spec { get; set; } = new();
public string registry { get; set; } = string.Empty;
public string guarantee { get; set; } = string.Empty;
}

View File

@ -1,7 +1,4 @@
using Netina.Domain.Entities.StorageFiles;
using Netina.Domain.Enums;
namespace Netina.Domain.Entities.Blogs;
namespace Netina.Domain.Entities.Blogs;
public partial class BlogStorageFile : StorageFile
{

View File

@ -34,10 +34,10 @@ public partial class Discount
public bool IsExpired()
=> !Immortal && ExpireDate.Date < DateTime.Today.Date;
public void SetCorporate(Guid userId)
public void SetCorporate(Guid marketerId)
{
IsForSaleCooperation = true;
CorporateUserId = userId;
MarketerId = marketerId;
}
}

View File

@ -82,8 +82,8 @@ public partial class Discount : ApiEntity
public bool IsForFirstPurchase { get; internal set; }
public Guid? CorporateUserId { get; internal set; }
public ApplicationUser? CorporateUser { get; internal set; }
public Guid? MarketerId { get; internal set; }
public Marketer? Marketer { get; internal set; }
public List<Order> Orders { get; internal set; } = new();
}

View File

@ -1,6 +1,4 @@
using Netina.Common.Models.Entity;
namespace Netina.Domain.Entities.Users;
namespace Netina.Domain.Entities.Users;
[AdaptTwoWays("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Map | MapType.MapToTarget)]
[AdaptTo("[name]SDto", IgnoreAttributes = new[] { typeof(AdaptIgnoreAttribute) }, MapType = MapType.Projection)]
@ -9,6 +7,10 @@ public class Marketer : ApiEntity
{
public string FatherName { get; set; } = string.Empty;
public string Shaba { get; set; } = string.Empty;
public DateTime LastSettlement { get; set; }
public Guid UserId { get; set; }
public ApplicationUser? User { get; set; }
public Discount? Discount { get; set; }
}

View File

@ -178,6 +178,8 @@ namespace Netina.Domain.Mappers
OrderAt = p19.OrderAt,
PreparingMinute = p19.PreparingMinute,
DiscountCode = p19.DiscountCode,
CustomerFullName = p19.Customer != null && p19.Customer.User != null ? p19.Customer.User.FirstName + " " + p19.Customer.User.LastName : string.Empty,
CustomerPhoneNumber = p19.Customer != null && p19.Customer.User != null ? p19.Customer.User.PhoneNumber : string.Empty,
OrderProducts = funcMain8(p19.OrderProducts),
Payments = funcMain9(p19.Payments),
OrderDelivery = p19.OrderDelivery == null ? null : new OrderDeliverySDto()
@ -228,6 +230,8 @@ namespace Netina.Domain.Mappers
result.OrderAt = p22.OrderAt;
result.PreparingMinute = p22.PreparingMinute;
result.DiscountCode = p22.DiscountCode;
result.CustomerFullName = p22.Customer != null && p22.Customer.User != null ? p22.Customer.User.FirstName + " " + p22.Customer.User.LastName : string.Empty;
result.CustomerPhoneNumber = p22.Customer != null && p22.Customer.User != null ? p22.Customer.User.PhoneNumber : string.Empty;
result.OrderProducts = funcMain10(p22.OrderProducts, result.OrderProducts);
result.Payments = funcMain11(p22.Payments, result.Payments);
result.OrderDelivery = funcMain12(p22.OrderDelivery, result.OrderDelivery);
@ -254,6 +258,8 @@ namespace Netina.Domain.Mappers
OrderAt = p30.OrderAt,
PreparingMinute = p30.PreparingMinute,
DiscountCode = p30.DiscountCode,
CustomerFullName = p30.Customer != null && p30.Customer.User != null ? p30.Customer.User.FirstName + " " + p30.Customer.User.LastName : string.Empty,
CustomerPhoneNumber = p30.Customer != null && p30.Customer.User != null ? p30.Customer.User.PhoneNumber : string.Empty,
OrderProducts = p30.OrderProducts.Select<OrderProduct, OrderProductSDto>(p31 => new OrderProductSDto()
{
Count = p31.Count,

View File

@ -0,0 +1,7 @@
namespace Netina.Domain.MartenEntities.Settings;
public class MarketerSetting
{
public int ProfitPercent { get; set; }
public string ContractTerms { get; set; } = string.Empty;
}

View File

@ -1,13 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Netina.Common.Extensions;
using Netina.Common.Models.Api;
using Netina.Common.Models.Exception;
using Netina.Domain.CommandQueries.Commands;
using Netina.Domain.Entities.Discounts;
using Netina.Domain.Entities.Users;
using Netina.Domain.Enums;
using Netina.Repository.Abstracts;
using Netina.Repository.Repositories.Base.Contracts;
namespace Netina.Repository.Handlers.Discounts;
@ -33,20 +24,27 @@ public class CreateSaleCooperationDiscountHandler : IRequestHandler<CreateSaleCo
if (!Guid.TryParse(_currentUserService.UserId, out userId))
throw new AppException("User id is wrong");
}
var user = await _userManager.FindByIdAsync(request.CorporateUserId.ToString());
var user = await _userManager.FindByIdAsync(userId.ToString());
if (user == null)
throw new AppException("User not found", ApiResultStatusCode.NotFound);
var marketer = await _repositoryWrapper.SetRepository<Marketer>()
.TableNoTracking
.FirstOrDefaultAsync(m => m.UserId == user.Id, cancellationToken);
if (marketer == null)
throw new AppException("Marketer not found", ApiResultStatusCode.NotFound);
var foundedDiscount = await _repositoryWrapper.SetRepository<Discount>()
.TableNoTracking
.FirstOrDefaultAsync(d => d.IsForSaleCooperation && d.CorporateUserId == userId, cancellationToken);
.FirstOrDefaultAsync(d => d.IsForSaleCooperation && d.MarketerId == marketer.Id, cancellationToken);
if (foundedDiscount == null)
{
var code = StringExtensions.GetId();
foundedDiscount = Discount.Create(code,$"کد مخصوص همکاری در فروش برای کاربر - {user.FirstName} {user.LastName}", 10, 0, true,
DiscountAmountType.Percent, DiscountType.All, 0, true, DateTime.Today, DateTime.Today.AddYears(10), 0,
false, 0, false, true, 0, false, false,false);
foundedDiscount.SetCorporate(userId);
foundedDiscount.SetCorporate(marketer.Id);
_repositoryWrapper.SetRepository<Discount>().Add(foundedDiscount);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -40,9 +40,9 @@ public class ApplicationContext : IdentityDbContext<ApplicationUser, Application
protected void RenameIdentityTables(ModelBuilder builder)
{
builder.HasDefaultSchema("public");
builder.Entity<ApplicationUser>().ToTable("Users");
builder.Entity<ApplicationRole>().ToTable("Roles");
builder.Entity<IdentityRoleClaim<Guid>>().ToTable("RoleClaims");
builder.Entity<IdentityUserRole<Guid>>().ToTable("UserRoles");
builder.Entity<IdentityUserClaim<Guid>>().ToTable("Claims");

View File

@ -1,10 +1,21 @@
using Netina.Common.Extensions;
using Netina.Domain.CommandQueries.Commands;
using Netina.Domain.Dtos.RequestDtos.SeedDtos;
using Netina.Domain.Dtos.SmallDtos;
using Netina.Domain.Entities.Blogs;
using Netina.WordPressDBConverter.Models;
using Netina.WordPressDBConverter.Services.RestServices;
using Newtonsoft.Json;
using Netina.Common.Models.Api;
using Netina.Domain.Dtos.SmallDtos;
using Netina.Domain.Enums;
using Netina.WordPressDBConverter.Services.RestServices;
using Refit;
var postMetaReader = new StreamReader("F:\\wp_postmeta.json");
var postMetaJson = postMetaReader.ReadToEnd();
Console.WriteLine("Terms File Read Success !");
var postMetas = JsonConvert.DeserializeObject<List<WordPressPostMetaDto>>(postMetaJson);
if (postMetas == null)
throw new Exception("Terms is null");
var termReader = new StreamReader("F:\\wp_terms.json");
@ -37,8 +48,10 @@ if (posts == null)
throw new Exception("Posts is null");
//CREATE CATEGORY PART
List<SeedBlogCategoryRequestDto> categories = new List<SeedBlogCategoryRequestDto>();
List<WordPressPostTagDto> tags = new List<WordPressPostTagDto>();
foreach (var taxonomy in termTaxonomies)
{
@ -64,15 +77,17 @@ foreach (var taxonomy in termTaxonomies)
Tag = term.name
});
}
}
var categoriesRest = await RestWrapper.Instance.SeedRestApi.SeedBlogCategoriesAsync(categories, "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==");
Console.WriteLine($"{categories.Count} ProductCategory Added Success !");
}
var categoriesRest = new Dictionary<int, Guid>();
categoriesRest = await RestWrapper.Instance.SeedRestApi.SeedBlogCategoriesAsync(categories, "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==");
Console.WriteLine($"{categories.Count} BlogCategory Added Success !");
//CREATE BLOG PART
List<SeedBlogRequestDto> blogs = new List<SeedBlogRequestDto>();
var random = new Random(1532);
int thumbCounter = 0;
foreach (var wordPressPostDto in posts.Where(p => p.post_type == "post"))
{
SeedBlogRequestDto blog;
@ -94,6 +109,35 @@ foreach (var wordPressPostDto in posts.Where(p => p.post_type == "post"))
if (categoryId == default)
categoryId = categoriesRest.FirstOrDefault(c => c.Key == 0).Value;
var files = new List<StorageFileSDto>();
var thumbnailMeta = postMetas.FirstOrDefault(pm => pm.post_id == wordPressPostDto.ID && pm.meta_key == "_thumbnail_id");
if (thumbnailMeta != null)
{
var thumbnail = posts.FirstOrDefault(p => p.ID.ToInt() == thumbnailMeta.meta_value.ToInt());
if (thumbnail != null)
{
using var httpClient = new HttpClient();
var imageBytes = await httpClient.GetByteArrayAsync(thumbnail.guid);
var uploadFile = new FileUploadRequest
{
ContentType = thumbnail.post_mime_type,
FileName = $"{StringExtensions.GetId(10)}.jpeg",
FileUploadType = FileUploadType.Image,
StringBaseFile = Convert.ToBase64String(imageBytes),
};
var fileRest = await RestWrapper.Instance.UploadRestApi.UploadAsync(uploadFile);
files.Add(new StorageFileSDto
{
FileLocation = fileRest.FileLocation,
FileName = fileRest.FileName,
Name = fileRest.FileName,
FileType = StorageFileType.Image
});
Console.WriteLine($"Thumbnail uploaded : {thumbCounter++}");
}
}
blog = new SeedBlogRequestDto
{
@ -102,17 +146,14 @@ foreach (var wordPressPostDto in posts.Where(p => p.post_type == "post"))
IsSuggested = false,
ReadingTime = random.Next(1, 15),
Title = wordPressPostDto.post_title,
Files = files
};
blogs.Add(blog);
}
for (int i = 0; i < blogs.Count / 50; i++)
{
await RestWrapper.Instance.SeedRestApi.SeedBlogsAsync(blogs.Skip(i * 50).Take(50).ToList(),
"kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==");
await RestWrapper.Instance.SeedRestApi.SeedBlogsAsync(new List<SeedBlogRequestDto>{blog}, "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==");
Console.WriteLine($"{i * 50} / {blogs.Count} Product Added Success !");
Console.WriteLine($"{thumbCounter++ } / {blogs.Count} Blog Added Success !");
}

View File

@ -1,4 +1,5 @@
using Netina.Domain.CommandQueries.Commands;
using Netina.Common.Models.Api;
using Netina.Domain.CommandQueries.Commands;
using Netina.Domain.Dtos.RequestDtos.SeedDtos;
using Refit;
@ -22,3 +23,9 @@ public interface ISeedRestApi
[Post("/blogs")]
Task SeedBlogsAsync([Body] List<SeedBlogRequestDto> request, [Query] string key);
}
public interface IUploadRestApi
{
[Post("")]
Task<FileUploadResponse> UploadAsync([Body] FileUploadRequest uploadRequest);
}

View File

@ -1,11 +1,19 @@
using Refit;
using Newtonsoft.Json;
using Refit;
namespace Netina.WordPressDBConverter.Services.RestServices;
public class RestWrapper
{
private static RefitSettings setting = new RefitSettings(new NewtonsoftJsonContentSerializer(new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
}));
private static RestWrapper? _instance;
public static RestWrapper Instance => _instance = _instance ?? new RestWrapper();
public ISeedRestApi SeedRestApi => RestService.For<ISeedRestApi>("http://localhost:32770/api/seed");
public ISeedRestApi SeedRestApi => RestService.For<ISeedRestApi>("http://localhost:32770/api/seed", setting);
public IUploadRestApi UploadRestApi => RestService.For<IUploadRestApi>("http://localhost:32770/api/file", setting);
}