feat : products , ordering system , discount

add discount page and CRUD , complete products CRUD , improve ordering system
release
Amir Hossein Khademi 2024-02-09 19:43:17 +03:30
parent 70e667468d
commit d442ed17ee
23 changed files with 1789 additions and 91 deletions

View File

@ -14,11 +14,11 @@ public class OrderBagController : ICarterModule
.WithDisplayName("GetUserCurrentOrderBagAsync")
.HasApiVersion(1.0);
group.MapPost("add/{productId}", AddProductToBagAsync)
group.MapPost("add", AddProductToBagAsync)
.WithDisplayName("AddProductToBag")
.HasApiVersion(1.0);
group.MapPost("remove/{productId}", RemoveFromOrderBagAsync)
group.MapDelete("remove", RemoveFromOrderBagAsync)
.WithDisplayName("RemoveFromOrderBag")
.HasApiVersion(1.0);
@ -40,12 +40,12 @@ public class OrderBagController : ICarterModule
public async Task<IResult> GetUserCurrentOrderBagAsync(IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new GetUserOrderBagQuery(), cancellationToken));
public async Task<IResult> AddProductToBagAsync( Guid productId, [FromQuery]int count, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new AddToOrderBagCommand(productId,count), cancellationToken));
public async Task<IResult> AddProductToBagAsync([FromBody] List<OrderBagRequestDto> requestDtos, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new AddToOrderBagCommand(requestDtos), cancellationToken));
public async Task<IResult> RemoveFromOrderBagAsync(Guid productId, [FromQuery] int count, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new RemoveFromOrderBagCommand(productId, count), cancellationToken));
public async Task<IResult> RemoveFromOrderBagAsync([FromBody] List<OrderBagRequestDto> requestDtos, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new RemoveFromOrderBagCommand(requestDtos), cancellationToken));
public async Task<IResult> AddDiscountToOrderBagAsync(Guid orderId, [FromQuery] string discountCode, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new SubmitDiscountCommand(orderId, discountCode), cancellationToken));

View File

@ -34,8 +34,8 @@ public class RoleController : ICarterModule
}
// GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery] int page, IUserService userService, CancellationToken cancellationToken)
=> TypedResults.Ok(await userService.GetRolesAsync(page, cancellationToken));
public async Task<IResult> GetAllAsync([FromQuery] int? page, [FromQuery]string? roleName, IUserService userService, CancellationToken cancellationToken)
=> TypedResults.Ok(await userService.GetRolesAsync(page,roleName, cancellationToken));
// GET:Get An Entity By Id
public async Task<IResult> GetAsync(Guid id, IUserService userService, CancellationToken cancellationToken)

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
@ -12,9 +12,9 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
</ItemGroup>
</ItemGroup>-->
<!--<PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
@ -25,7 +25,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
</ItemGroup>-->
</ItemGroup>
<ItemGroup>
<Using Include="MD.PersianDateTime.Standard" />

View File

@ -13,6 +13,7 @@ public interface IUserService : IScopedDependency
Task<List<ApplicationRole>> GetRolesAsync(int page = 0, CancellationToken cancellationToken = default);
Task<List<ApplicationRole>> GetRolesAsync(int? page,string? roleName, CancellationToken cancellationToken = default);
Task<RoleActionRequestDto> GetRoleAsync(Guid roleId, CancellationToken cancellationToken = default);
Task<ApplicationRole> CreateRoleAsync(RoleActionRequestDto request, CancellationToken cancellationToken = default);
Task<bool> EditRoleAsync(RoleActionRequestDto request, CancellationToken cancellationToken = default);

View File

@ -20,18 +20,23 @@ public class AddToOrderBagCommandHandler : IRequestHandler<AddToOrderBagCommand,
if (!Guid.TryParse(_currentUserService.UserId, out Guid userId))
throw new AppException("User id wrong", ApiResultStatusCode.BadRequest);
var product = await _repositoryWrapper.SetRepository<Product>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == request.ProductId, cancellationToken);
if (product == null)
throw new AppException("Product not found ",ApiResultStatusCode.NotFound);
if (!product.IsEnable)
throw new AppException("Product is not enable", ApiResultStatusCode.BadRequest);
var orderBag = await _mediator.Send(new GetUserOrderBagQuery(), cancellationToken);
orderBag.AddToOrderBag(product, request.Count);
foreach (var requestDto in request.RequestDtos)
{
var product = await _repositoryWrapper.SetRepository<Product>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == requestDto.ProductId, cancellationToken);
if (product == null)
throw new AppException("Product not found ", ApiResultStatusCode.NotFound);
if (!product.IsEnable)
throw new AppException("Product is not enable", ApiResultStatusCode.BadRequest);
orderBag.AddToOrderBag(product, requestDto.Count);
}
_repositoryWrapper.SetRepository<Order>().Update(orderBag);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);

View File

@ -19,18 +19,22 @@ public class RemoveFromOrderBagCommandHandler : IRequestHandler<RemoveFromOrderB
if (!Guid.TryParse(_currentUserService.UserId, out Guid userId))
throw new AppException("User id wrong", ApiResultStatusCode.BadRequest);
var product = await _repositoryWrapper.SetRepository<Product>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == request.ProductId, cancellationToken);
if (product == null)
throw new AppException("Product not found ", ApiResultStatusCode.NotFound);
if (!product.IsEnable)
throw new AppException("Product is not enable", ApiResultStatusCode.BadRequest);
var orderBag = await _mediator.Send(new GetUserOrderBagQuery(), cancellationToken);
orderBag.RemoveFromOrderBag(product, request.Count);
foreach (var requestDto in request.RequestDtos)
{
var product = await _repositoryWrapper.SetRepository<Product>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == requestDto.ProductId, cancellationToken);
if (product == null)
throw new AppException("Product not found ", ApiResultStatusCode.NotFound);
if (!product.IsEnable)
throw new AppException("Product is not enable", ApiResultStatusCode.BadRequest);
orderBag.RemoveFromOrderBag(product, requestDto.Count);
}
_repositoryWrapper.SetRepository<Order>().Update(orderBag);

View File

@ -1,4 +1,5 @@
using StackExchange.Redis;
using System.Linq;
using System.Security.Claims;
namespace NetinaShop.Core.EntityServices;
@ -71,6 +72,16 @@ public class UserService : IUserService
.Skip(page * 15).Take(15)
.Select(ApplicationUserMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
foreach (var user in users)
{
var roles = await _userManager.GetRolesAsync(user.AdaptToApplicationUser());
foreach (var roleName in roles)
{
var role = await _roleManager.FindByNameAsync(roleName);
if (role != null)
user.RoleName += role.PersianName + " ";
}
}
return users;
}
@ -258,6 +269,20 @@ public class UserService : IUserService
return roles;
}
public async Task<List<ApplicationRole>> GetRolesAsync(int? page, string? roleName, CancellationToken cancellationToken = default)
{
IQueryable<ApplicationRole> roles;
if (roleName!=null)
roles = _roleManager.Roles.Where(r => r.Name != "RootAdmin" && r.PersianName.Trim().ToLower().Contains(roleName));
else
roles = _roleManager.Roles.Where(r => r.Name != "RootAdmin");
if (page != null)
roles = roles.Skip(page.Value * 15).Take(15);
else
roles = roles;
return await roles.ToListAsync(cancellationToken);
}
public async Task<RoleActionRequestDto> GetRoleAsync(Guid roleId, CancellationToken cancellationToken = default)
{
var role = (await _roleManager.FindByIdAsync(roleId.ToString()));
@ -324,9 +349,6 @@ public class UserService : IUserService
foreach (var claim in request.Permissions)
await _roleManager.AddClaimAsync(applicationRole, new Claim(CustomClaimType.Permission, claim));
foreach (var claim in roleClaims)
await _roleManager.RemoveClaimAsync(applicationRole, claim);
return true;
}

View File

@ -1,9 +1,11 @@
namespace NetinaShop.Domain.CommandQueries.Commands;
using NetinaShop.Domain.Entities.Products;
namespace NetinaShop.Domain.CommandQueries.Commands;
public sealed record CreateOrderCommand(string DiscountCode, List<OrderProductSDto> OrderProducts, OrderDeliverySDto OrderDelivery) : IRequest<OrderSDto>;
public sealed record AddToOrderBagCommand(Guid ProductId, int Count) : IRequest<bool>;
public sealed record RemoveFromOrderBagCommand(Guid ProductId, int Count) : IRequest<bool>;
public sealed record AddToOrderBagCommand(List<OrderBagRequestDto> RequestDtos) : IRequest<bool>;
public sealed record RemoveFromOrderBagCommand(List<OrderBagRequestDto> RequestDtos) : IRequest<bool>;
public sealed record SubmitDiscountCommand(Guid OrderId,string DiscountCode) : IRequest<bool>;
public sealed record SubmitOrderDeliveryCommand(string Address, string PostalCode, string ReceiverPhoneNumber, string ReceiverFullName, Guid OrderId, Guid ShippingId) : IRequest<bool>;

View File

@ -10,6 +10,7 @@ string Warranty,
bool BeDisplayed,
double Cost,
double PackingCost,
int Stock,
bool HasExpressDelivery,
int MaxOrderCount,
bool IsSpecialOffer,
@ -30,6 +31,7 @@ public sealed record UpdateProductCommand(
bool BeDisplayed,
double Cost,
double PackingCost,
int Stock,
bool HasExpressDelivery,
int MaxOrderCount,
bool IsSpecialOffer,

View File

@ -13,6 +13,7 @@ public class ProductLDto : BaseDto<ProductLDto,Product>
public int MaxOrderCount { get; set; }
public double Cost { get; set; }
public double PackingCost { get; set; }
public int Stock { get; set; }
public Guid BrandId { get; set; }
public string BrandName { get; set; } = string.Empty;
public Guid CategoryId { get; set; }

View File

@ -0,0 +1,7 @@
namespace NetinaShop.Domain.Dtos.RequestDtos;
public class OrderBagRequestDto
{
public Guid ProductId { get; set; }
public int Count { get; set; }
}

View File

@ -7,11 +7,27 @@ public partial class Product
double cost,
double packingCost,
bool hasExpressDelivery,
int stock,
int maxOrderCount,
Guid brandId,
Guid categoryId)
{
return new Product(persianName, englishName, summery, expertCheck, tags, warranty, beDisplayed,cost, packingCost,hasExpressDelivery,maxOrderCount, brandId,categoryId);
return new Product(
persianName,
englishName,
summery,
expertCheck,
tags,
warranty,
beDisplayed,
cost,
packingCost,
stock,
hasExpressDelivery,
maxOrderCount,
stock > 0,
brandId,
categoryId);
}
public void AddRate(float rate)

View File

@ -23,8 +23,10 @@ public partial class Product : ApiEntity
bool beDisplayed,
double cost,
double packingCost,
int stock,
bool hasExpressDelivery,
int maxOrderCount,
bool isEnable,
Guid brandId,
Guid categoryId)
{
@ -37,8 +39,10 @@ public partial class Product : ApiEntity
BeDisplayed = beDisplayed;
Cost = cost;
PackingCost = packingCost;
Stock = stock;
HasExpressDelivery = hasExpressDelivery;
MaxOrderCount = maxOrderCount;
IsEnable = isEnable;
BrandId = brandId;
CategoryId = categoryId;
}
@ -52,6 +56,7 @@ public partial class Product : ApiEntity
public bool IsEnable { get; internal set; }
public bool BeDisplayed { get; internal set; }
public double PackingCost { get; internal set; }
public int Stock { get; internal set; }
public float Rate { get; internal set; }
public int ReviewCount { get; internal set; }
public int Viewed { get; internal set; }

View File

@ -2,6 +2,8 @@
public enum Gender
{
[Display(Name = "مرد")]
Male,
[Display(Name = "بانو")]
Female
}

View File

@ -26,6 +26,7 @@ namespace NetinaShop.Domain.Mappers
Cost = p1.Cost,
BeDisplayed = p1.BeDisplayed,
PackingCost = p1.PackingCost,
Stock = p1.Stock,
HasExpressDelivery = p1.HasExpressDelivery,
MaxOrderCount = p1.MaxOrderCount,
BrandId = p1.BrandId,
@ -54,6 +55,7 @@ namespace NetinaShop.Domain.Mappers
result.Cost = p5.Cost;
result.BeDisplayed = p5.BeDisplayed;
result.PackingCost = p5.PackingCost;
result.Stock = p5.Stock;
result.HasExpressDelivery = p5.HasExpressDelivery;
result.MaxOrderCount = p5.MaxOrderCount;
result.BrandId = p5.BrandId;
@ -77,6 +79,7 @@ namespace NetinaShop.Domain.Mappers
Cost = p13.Cost,
BeDisplayed = p13.BeDisplayed,
PackingCost = p13.PackingCost,
Stock = p13.Stock,
HasExpressDelivery = p13.HasExpressDelivery,
MaxOrderCount = p13.MaxOrderCount,
BrandId = p13.BrandId,
@ -132,6 +135,7 @@ namespace NetinaShop.Domain.Mappers
MaxOrderCount = p17.MaxOrderCount,
Cost = p17.Cost,
PackingCost = p17.PackingCost,
Stock = p17.Stock,
BrandId = p17.BrandId,
BrandName = p17.Brand == null ? null : p17.Brand.Name,
CategoryId = p17.CategoryId,
@ -162,6 +166,7 @@ namespace NetinaShop.Domain.Mappers
result.MaxOrderCount = p21.MaxOrderCount;
result.Cost = p21.Cost;
result.PackingCost = p21.PackingCost;
result.Stock = p21.Stock;
result.BrandId = p21.BrandId;
result.BrandName = p21.Brand == null ? null : p21.Brand.Name;
result.CategoryId = p21.CategoryId;
@ -187,6 +192,7 @@ namespace NetinaShop.Domain.Mappers
MaxOrderCount = p29.MaxOrderCount,
Cost = p29.Cost,
PackingCost = p29.PackingCost,
Stock = p29.Stock,
BrandId = p29.BrandId,
BrandName = p29.Brand.Name,
CategoryId = p29.CategoryId,
@ -291,41 +297,28 @@ namespace NetinaShop.Domain.Mappers
}
public static ProductSDto AdaptToSDto(this Product p40)
{
if (p40 == null)
return p40 == null ? null : new ProductSDto()
{
return null;
}
ProductSDto result = new ProductSDto();
result.PersianName = p40.PersianName;
result.EnglishName = p40.EnglishName;
result.Summery = p40.Summery;
result.ExpertCheck = p40.ExpertCheck;
result.Tags = p40.Tags;
result.Warranty = p40.Warranty;
result.Cost = p40.Cost;
result.IsEnable = p40.IsEnable;
result.BeDisplayed = p40.BeDisplayed;
result.PackingCost = p40.PackingCost;
result.Rate = p40.Rate;
result.ReviewCount = p40.ReviewCount;
result.Viewed = p40.Viewed;
result.CategoryId = p40.CategoryId;
result.BrandId = p40.BrandId;
result.Id = p40.Id;
result.CreatedAt = p40.CreatedAt;
if (!(p40.Brand == null))
{
result.BrandName = p40.Brand == null ? null : p40.Brand.Name;
}
if (!(p40.Category == null))
{
result.CategoryName = p40.Category == null ? null : p40.Category.Name;
}
return result;
PersianName = p40.PersianName,
EnglishName = p40.EnglishName,
Summery = p40.Summery,
ExpertCheck = p40.ExpertCheck,
Tags = p40.Tags,
Warranty = p40.Warranty,
Cost = p40.Cost,
IsEnable = p40.IsEnable,
BeDisplayed = p40.BeDisplayed,
PackingCost = p40.PackingCost,
Rate = p40.Rate,
ReviewCount = p40.ReviewCount,
Viewed = p40.Viewed,
CategoryId = p40.CategoryId,
BrandId = p40.BrandId,
BrandName = p40.Brand == null ? null : p40.Brand.Name,
CategoryName = p40.Category == null ? null : p40.Category.Name,
Id = p40.Id,
CreatedAt = p40.CreatedAt
};
}
public static ProductSDto AdaptTo(this Product p41, ProductSDto p42)
{
@ -350,18 +343,10 @@ namespace NetinaShop.Domain.Mappers
result.Viewed = p41.Viewed;
result.CategoryId = p41.CategoryId;
result.BrandId = p41.BrandId;
result.BrandName = p41.Brand == null ? null : p41.Brand.Name;
result.CategoryName = p41.Category == null ? null : p41.Category.Name;
result.Id = p41.Id;
result.CreatedAt = p41.CreatedAt;
if (!(p41.Brand == null))
{
result.BrandName = p41.Brand == null ? null : p41.Brand.Name;
}
if (!(p41.Category == null))
{
result.CategoryName = p41.Category == null ? null : p41.Category.Name;
}
return result;
}

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
@ -14,9 +14,9 @@
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="8.0.1" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
</ItemGroup>
</ItemGroup>-->
<!--<PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
@ -32,7 +32,7 @@
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
</ItemGroup>-->
</ItemGroup>
<ItemGroup>

View File

@ -38,7 +38,7 @@ public class UpdateDiscountCommandHandler : IRequestHandler<UpdateDiscountComman
break;
case DiscountType.Product:
var productEnt = await _repositoryWrapper.SetRepository<CategoryDiscount>().TableNoTracking.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken);
var productEnt = await _repositoryWrapper.SetRepository<ProductDiscount>().TableNoTracking.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken);
if (productEnt == null)
throw new AppException("Discount not found", ApiResultStatusCode.NotFound);

View File

@ -14,7 +14,10 @@ public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand,
public async Task<ProductLDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var ent = Product.Create(request.PersianName, request.EnglishName, request.Summery, request.ExpertCheck,
request.Tags, request.Warranty,request.BeDisplayed,request.Cost,request.PackingCost,request.HasExpressDelivery,request.MaxOrderCount,
request.Tags, request.Warranty,request.BeDisplayed,request.Cost,request.PackingCost,
request.HasExpressDelivery,
request.Stock,
request.MaxOrderCount,
request.BrandId,request.CategoryId);
foreach (var specification in request.Specifications)

View File

@ -24,6 +24,7 @@ public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand,
request.Cost,
request.PackingCost,
request.HasExpressDelivery,
request.Stock,
request.MaxOrderCount,
request.BrandId,
request.CategoryId);

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NetinaShop.Repository.Migrations
{
/// <inheritdoc />
public partial class EditProductIsEnable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "Stock",
schema: "public",
table: "Products",
type: "integer",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Stock",
schema: "public",
table: "Products");
}
}
}

View File

@ -721,6 +721,9 @@ namespace NetinaShop.Repository.Migrations
b.Property<int>("ReviewCount")
.HasColumnType("integer");
b.Property<int>("Stock")
.HasColumnType("integer");
b.Property<string>("Summery")
.IsRequired()
.HasColumnType("text");

View File

@ -105,12 +105,12 @@ try
var price = postMetas.FirstOrDefault(pm => pm.post_id == wordPressPostDto.ID && pm.meta_key == "_price");
if (price != null && double.TryParse(price.meta_value, out double dPrice))
product = new CreateProductCommand(wordPressPostDto.post_title, string.Empty, wordPressPostDto.post_content,
wordPressPostDto.post_excerpt, string.Empty, string.Empty, true, dPrice, 0,
wordPressPostDto.post_excerpt, string.Empty, string.Empty, true, dPrice, 0,10,
false,10,false,brandId, categoryId,
new DiscountSDto(),new List<SpecificationSDto>(), new List<StorageFileSDto>());
else
product = new CreateProductCommand(wordPressPostDto.post_title, string.Empty, wordPressPostDto.post_content,
wordPressPostDto.post_excerpt, string.Empty, string.Empty, true, 0, 0,false,10,
wordPressPostDto.post_excerpt, string.Empty, string.Empty, true, 0, 0,10,false,10,
false,brandId,categoryId,
new DiscountSDto(),new List<SpecificationSDto>(), new List<StorageFileSDto>());