Merge branch 'main' into stage
This commit is contained in:
32588
docs/model.ndm2
32588
docs/model.ndm2
File diff suppressed because it is too large
Load Diff
25204
docs/model1.ndm2
Normal file
25204
docs/model1.ndm2
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,17 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.2.2" />
|
||||
<PackageReference Include="Mapster" Version="7.3.0" />
|
||||
<PackageReference Include="Mapster" Version="7.4.0" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.11" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.10" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CMSMicroservice.Domain\CMSMicroservice.Domain.csproj" />
|
||||
|
||||
@@ -11,12 +11,30 @@ public class ValidationException : Exception
|
||||
}
|
||||
|
||||
public ValidationException(IEnumerable<ValidationFailure> failures)
|
||||
: this()
|
||||
: base(BuildMessage(failures, out var errors))
|
||||
{
|
||||
Errors = failures
|
||||
.GroupBy(e => e.PropertyName, e => e.ErrorMessage)
|
||||
.ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray());
|
||||
Errors = errors;
|
||||
}
|
||||
|
||||
public IDictionary<string, string[]> Errors { get; }
|
||||
|
||||
private static string BuildMessage(IEnumerable<ValidationFailure> failures, out IDictionary<string, string[]> errors)
|
||||
{
|
||||
var list = failures?.Where(f => !string.IsNullOrWhiteSpace(f.ErrorMessage)).ToList() ?? new List<ValidationFailure>();
|
||||
|
||||
errors = list
|
||||
.GroupBy(e => e.PropertyName, e => e.ErrorMessage)
|
||||
.ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray());
|
||||
|
||||
var items = list
|
||||
.Select(f => string.IsNullOrWhiteSpace(f.PropertyName)
|
||||
? f.ErrorMessage
|
||||
: $"{f.PropertyName}: {f.ErrorMessage}")
|
||||
.Where(s => !string.IsNullOrWhiteSpace(s))
|
||||
.ToArray();
|
||||
|
||||
return items.Length > 0
|
||||
? string.Join(" | ", items)
|
||||
: "One or more validation failures have occurred.";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,15 @@ public interface IApplicationDbContext
|
||||
DbSet<Role> Roles { get; }
|
||||
DbSet<UserRole> UserRoles { get; }
|
||||
DbSet<OtpToken> OtpTokens { get; }
|
||||
DbSet<UserOrder> UserOrders { get; }
|
||||
DbSet<User> Users { get; }
|
||||
DbSet<UserWallet> UserWallets { get; }
|
||||
DbSet<UserWalletChangeLog> UserWalletChangeLogs { get; }
|
||||
DbSet<UserCarts> UserCartss { get; }
|
||||
DbSet<ProductGallerys> ProductGalleryss { get; }
|
||||
DbSet<UserOrder> UserOrders { get; }
|
||||
DbSet<FactorDetails> FactorDetailss { get; }
|
||||
DbSet<Products> Productss { get; }
|
||||
DbSet<ProductImages> ProductImagess { get; }
|
||||
DbSet<Transactions> Transactionss { get; }
|
||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
namespace CMSMicroservice.Application.Common.Interfaces;
|
||||
public interface IGenerateJwtToken
|
||||
{
|
||||
Task<string> GenerateJwtToken(User user);
|
||||
Task<string> GenerateJwtToken(User user,int? expiryDays=null);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace CMSMicroservice.Application.Common.Interfaces;
|
||||
public interface IHashService
|
||||
{
|
||||
// Computes a deterministic SHA-256 hex string for the given input (legacy helper)
|
||||
string ComputeSha256Hex(string input);
|
||||
// Compares plain input with a stored hex hash (case-insensitive) (legacy helper)
|
||||
bool VerifySha256Hex(string input, string? expectedHexHash);
|
||||
|
||||
// Computes HMAC-SHA256 hex using a provided secret (for OTP and similar)
|
||||
string ComputeHmacSha256Hex(string input, string secret);
|
||||
// Verifies input against expected HMAC-SHA256 hex using the secret
|
||||
bool VerifyHmacSha256Hex(string input, string? expectedHexHash, string secret);
|
||||
|
||||
// Creates a salted password hash using PBKDF2; returns an encoded string containing algorithm params and salt
|
||||
string HashPassword(string password);
|
||||
// Verifies a password against the stored hash; supports both PBKDF2 format and legacy SHA-256 hex
|
||||
bool VerifyPassword(string password, string? storedHash);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class FactorDetailsProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class ProductGallerysProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class ProductImagesProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class ProductsProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class TransactionsProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class UserCartsProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class UserWalletChangeLogProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace CMSMicroservice.Application.Common.Mappings;
|
||||
|
||||
public class UserWalletProfile : IRegister
|
||||
{
|
||||
void IRegister.Register(TypeAdapterConfig config)
|
||||
{
|
||||
//config.NewConfig<Source,Destination>()
|
||||
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.CreateNewFactorDetails;
|
||||
public record CreateNewFactorDetailsCommand : IRequest<CreateNewFactorDetailsResponseDto>
|
||||
{
|
||||
//
|
||||
public long ProductId { get; init; }
|
||||
//
|
||||
public int Count { get; init; }
|
||||
//
|
||||
public long UnitPrice { get; init; }
|
||||
//
|
||||
public int UnitDiscount { get; init; }
|
||||
//
|
||||
public long OrderId { get; init; }
|
||||
//
|
||||
public long UnitDiscountPrice { get; init; }
|
||||
//
|
||||
public bool IsChangePrice { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.CreateNewFactorDetails;
|
||||
public class CreateNewFactorDetailsCommandHandler : IRequestHandler<CreateNewFactorDetailsCommand, CreateNewFactorDetailsResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewFactorDetailsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewFactorDetailsResponseDto> Handle(CreateNewFactorDetailsCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<FactorDetails>();
|
||||
await _context.FactorDetailss.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewFactorDetailsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewFactorDetailsResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.CreateNewFactorDetails;
|
||||
public class CreateNewFactorDetailsCommandValidator : AbstractValidator<CreateNewFactorDetailsCommand>
|
||||
{
|
||||
public CreateNewFactorDetailsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.ProductId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Count)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UnitPrice)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UnitDiscount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.OrderId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UnitDiscountPrice)
|
||||
.NotNull();
|
||||
RuleFor(model => model.IsChangePrice)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewFactorDetailsCommand>.CreateWithOptions((CreateNewFactorDetailsCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.CreateNewFactorDetails;
|
||||
public class CreateNewFactorDetailsResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.DeleteFactorDetails;
|
||||
public record DeleteFactorDetailsCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.DeleteFactorDetails;
|
||||
public class DeleteFactorDetailsCommandHandler : IRequestHandler<DeleteFactorDetailsCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteFactorDetailsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteFactorDetailsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.FactorDetailss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(FactorDetails), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.FactorDetailss.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteFactorDetailsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.DeleteFactorDetails;
|
||||
public class DeleteFactorDetailsCommandValidator : AbstractValidator<DeleteFactorDetailsCommand>
|
||||
{
|
||||
public DeleteFactorDetailsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteFactorDetailsCommand>.CreateWithOptions((DeleteFactorDetailsCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.UpdateFactorDetails;
|
||||
public record UpdateFactorDetailsCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
//
|
||||
public long ProductId { get; init; }
|
||||
//
|
||||
public int Count { get; init; }
|
||||
//
|
||||
public long UnitPrice { get; init; }
|
||||
//
|
||||
public int UnitDiscount { get; init; }
|
||||
//
|
||||
public long OrderId { get; init; }
|
||||
//
|
||||
public long UnitDiscountPrice { get; init; }
|
||||
//
|
||||
public bool IsChangePrice { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.UpdateFactorDetails;
|
||||
public class UpdateFactorDetailsCommandHandler : IRequestHandler<UpdateFactorDetailsCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateFactorDetailsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateFactorDetailsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.FactorDetailss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(FactorDetails), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.FactorDetailss.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateFactorDetailsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Commands.UpdateFactorDetails;
|
||||
public class UpdateFactorDetailsCommandValidator : AbstractValidator<UpdateFactorDetailsCommand>
|
||||
{
|
||||
public UpdateFactorDetailsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ProductId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Count)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UnitPrice)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UnitDiscount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.OrderId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UnitDiscountPrice)
|
||||
.NotNull();
|
||||
RuleFor(model => model.IsChangePrice)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateFactorDetailsCommand>.CreateWithOptions((UpdateFactorDetailsCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.EventHandlers;
|
||||
|
||||
public class CreateNewFactorDetailsEventHandler : INotificationHandler<CreateNewFactorDetailsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
CreateNewFactorDetailsEventHandler> _logger;
|
||||
|
||||
public CreateNewFactorDetailsEventHandler(ILogger<CreateNewFactorDetailsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewFactorDetailsEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.EventHandlers;
|
||||
|
||||
public class DeleteFactorDetailsEventHandler : INotificationHandler<DeleteFactorDetailsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
DeleteFactorDetailsEventHandler> _logger;
|
||||
|
||||
public DeleteFactorDetailsEventHandler(ILogger<DeleteFactorDetailsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteFactorDetailsEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.EventHandlers;
|
||||
|
||||
public class UpdateFactorDetailsEventHandler : INotificationHandler<UpdateFactorDetailsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
UpdateFactorDetailsEventHandler> _logger;
|
||||
|
||||
public UpdateFactorDetailsEventHandler(ILogger<UpdateFactorDetailsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateFactorDetailsEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetAllFactorDetailsByFilter;
|
||||
public record GetAllFactorDetailsByFilterQuery : IRequest<GetAllFactorDetailsByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllFactorDetailsByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllFactorDetailsByFilterFilter
|
||||
{
|
||||
//
|
||||
public long? Id { get; set; }
|
||||
//
|
||||
public long? ProductId { get; set; }
|
||||
//
|
||||
public int? Count { get; set; }
|
||||
//
|
||||
public long? UnitPrice { get; set; }
|
||||
//
|
||||
public int? UnitDiscount { get; set; }
|
||||
//
|
||||
public long? OrderId { get; set; }
|
||||
//
|
||||
public long? UnitDiscountPrice { get; set; }
|
||||
//
|
||||
public bool? IsChangePrice { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetAllFactorDetailsByFilter;
|
||||
public class GetAllFactorDetailsByFilterQueryHandler : IRequestHandler<GetAllFactorDetailsByFilterQuery, GetAllFactorDetailsByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllFactorDetailsByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllFactorDetailsByFilterResponseDto> Handle(GetAllFactorDetailsByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.FactorDetailss
|
||||
.ApplyOrder(sortBy: request.SortBy)
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
if (request.Filter is not null)
|
||||
{
|
||||
query = query
|
||||
.Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
|
||||
.Where(x => request.Filter.ProductId == null || x.ProductId == request.Filter.ProductId)
|
||||
.Where(x => request.Filter.Count == null || x.Count == request.Filter.Count)
|
||||
.Where(x => request.Filter.UnitPrice == null || x.UnitPrice == request.Filter.UnitPrice)
|
||||
.Where(x => request.Filter.UnitDiscount == null || x.UnitDiscount == request.Filter.UnitDiscount)
|
||||
.Where(x => request.Filter.OrderId == null || x.OrderId == request.Filter.OrderId)
|
||||
.Where(x => request.Filter.UnitDiscountPrice == null || x.UnitDiscountPrice == request.Filter.UnitDiscountPrice)
|
||||
.Where(x => request.Filter.IsChangePrice == null || x.IsChangePrice == request.Filter.IsChangePrice)
|
||||
;
|
||||
}
|
||||
return new GetAllFactorDetailsByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllFactorDetailsByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetAllFactorDetailsByFilter;
|
||||
public class GetAllFactorDetailsByFilterQueryValidator : AbstractValidator<GetAllFactorDetailsByFilterQuery>
|
||||
{
|
||||
public GetAllFactorDetailsByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllFactorDetailsByFilterQuery>.CreateWithOptions((GetAllFactorDetailsByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetAllFactorDetailsByFilter;
|
||||
public class GetAllFactorDetailsByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllFactorDetailsByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllFactorDetailsByFilterResponseModel
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public long ProductId { get; set; }
|
||||
//
|
||||
public int Count { get; set; }
|
||||
//
|
||||
public long UnitPrice { get; set; }
|
||||
//
|
||||
public int UnitDiscount { get; set; }
|
||||
//
|
||||
public long OrderId { get; set; }
|
||||
//
|
||||
public long UnitDiscountPrice { get; set; }
|
||||
//
|
||||
public bool IsChangePrice { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetFactorDetails;
|
||||
public record GetFactorDetailsQuery : IRequest<GetFactorDetailsResponseDto>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetFactorDetails;
|
||||
public class GetFactorDetailsQueryHandler : IRequestHandler<GetFactorDetailsQuery, GetFactorDetailsResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetFactorDetailsQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetFactorDetailsResponseDto> Handle(GetFactorDetailsQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.FactorDetailss
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetFactorDetailsResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(FactorDetails), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetFactorDetails;
|
||||
public class GetFactorDetailsQueryValidator : AbstractValidator<GetFactorDetailsQuery>
|
||||
{
|
||||
public GetFactorDetailsQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetFactorDetailsQuery>.CreateWithOptions((GetFactorDetailsQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace CMSMicroservice.Application.FactorDetailsCQ.Queries.GetFactorDetails;
|
||||
public class GetFactorDetailsResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public long ProductId { get; set; }
|
||||
//
|
||||
public int Count { get; set; }
|
||||
//
|
||||
public long UnitPrice { get; set; }
|
||||
//
|
||||
public int UnitDiscount { get; set; }
|
||||
//
|
||||
public long OrderId { get; set; }
|
||||
//
|
||||
public long UnitDiscountPrice { get; set; }
|
||||
//
|
||||
public bool IsChangePrice { get; set; }
|
||||
|
||||
}
|
||||
@@ -7,11 +7,13 @@ public class CreateNewOtpTokenCommandHandler : IRequestHandler<CreateNewOtpToken
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IConfiguration _cfg;
|
||||
private readonly IHashService _hashService;
|
||||
|
||||
public CreateNewOtpTokenCommandHandler(IApplicationDbContext context, IConfiguration cfg)
|
||||
public CreateNewOtpTokenCommandHandler(IApplicationDbContext context, IConfiguration cfg, IHashService hashService)
|
||||
{
|
||||
_context = context;
|
||||
_cfg = cfg;
|
||||
_hashService = hashService;
|
||||
}
|
||||
|
||||
const int CodeLength = 6;
|
||||
@@ -41,7 +43,8 @@ public class CreateNewOtpTokenCommandHandler : IRequestHandler<CreateNewOtpToken
|
||||
|
||||
// تولید کد
|
||||
var code = GenerateNumericCode(CodeLength);
|
||||
var codeHash = Hash(code);
|
||||
var secret = _cfg["Otp:Secret"] ?? throw new InvalidOperationException("Otp:Secret not set");
|
||||
var codeHash = _hashService.ComputeHmacSha256Hex(code, secret);
|
||||
|
||||
var entity = new OtpToken
|
||||
{
|
||||
@@ -75,13 +78,4 @@ public class CreateNewOtpTokenCommandHandler : IRequestHandler<CreateNewOtpToken
|
||||
foreach (var b in bytes) sb.Append((b % 10).ToString());
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private string Hash(string code)
|
||||
{
|
||||
// HMAC با secret اپ (نیازی به ذخیره salt جدا نیست)
|
||||
var secret = _cfg["Otp:Secret"] ?? throw new InvalidOperationException("Otp:Secret not set");
|
||||
using var h = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
|
||||
var hash = h.ComputeHash(Encoding.UTF8.GetBytes(code));
|
||||
return Convert.ToHexString(hash);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
namespace CMSMicroservice.Application.OtpTokenCQ.Commands.VerifyOtpToken;
|
||||
public class VerifyOtpTokenCommandHandler : IRequestHandler<VerifyOtpTokenCommand, VerifyOtpTokenResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IConfiguration _cfg;
|
||||
private readonly IHashService _hashService;
|
||||
|
||||
public VerifyOtpTokenCommandHandler(IApplicationDbContext context, IConfiguration cfg)
|
||||
public VerifyOtpTokenCommandHandler(IApplicationDbContext context, IConfiguration cfg, IHashService hashService)
|
||||
{
|
||||
_context = context;
|
||||
_cfg = cfg;
|
||||
_hashService = hashService;
|
||||
}
|
||||
|
||||
const int MaxAttempts = 5; // محدودیت تلاش
|
||||
@@ -21,7 +21,6 @@ public class VerifyOtpTokenCommandHandler : IRequestHandler<VerifyOtpTokenComman
|
||||
var purpose = request.Purpose?.ToLowerInvariant() ?? "signup";
|
||||
var now = DateTime.Now;
|
||||
|
||||
|
||||
var otp = await _context.OtpTokens
|
||||
.Where(o => o.Mobile == mobile && o.Purpose == purpose && !o.IsUsed && o.ExpiresAt > now)
|
||||
.OrderByDescending(o => o.Created)
|
||||
@@ -33,7 +32,8 @@ public class VerifyOtpTokenCommandHandler : IRequestHandler<VerifyOtpTokenComman
|
||||
|
||||
otp.Attempts++;
|
||||
|
||||
if (!VerifyHash(request.Code, otp.CodeHash))
|
||||
var secret = _cfg["Otp:Secret"] ?? throw new InvalidOperationException("Otp:Secret not set");
|
||||
if (!_hashService.VerifyHmacSha256Hex(request.Code, otp.CodeHash, secret))
|
||||
{
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return new VerifyOtpTokenResponseDto() { Success = false, Message = "کد نادرست است." };
|
||||
@@ -68,6 +68,12 @@ public class VerifyOtpTokenCommandHandler : IRequestHandler<VerifyOtpTokenComman
|
||||
ParentId = parent.Id
|
||||
};
|
||||
await _context.Users.AddAsync(user, cancellationToken);
|
||||
await _context.UserRoles.AddAsync(new()
|
||||
{
|
||||
UserId = user.Id,
|
||||
RoleId = 1,//UserRoleEnum.User
|
||||
|
||||
},cancellationToken);
|
||||
user.AddDomainEvent(new CreateNewUserEvent(user));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
@@ -90,15 +96,4 @@ public class VerifyOtpTokenCommandHandler : IRequestHandler<VerifyOtpTokenComman
|
||||
RemainingSeconds = (otp.ExpiresAt - now).Seconds
|
||||
};
|
||||
}
|
||||
|
||||
private string Hash(string code)
|
||||
{
|
||||
// HMAC با secret اپ (نیازی به ذخیره salt جدا نیست)
|
||||
var secret = _cfg["Otp:Secret"] ?? throw new InvalidOperationException("Otp:Secret not set");
|
||||
using var h = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
|
||||
var hash = h.ComputeHash(Encoding.UTF8.GetBytes(code));
|
||||
return Convert.ToHexString(hash);
|
||||
}
|
||||
private bool VerifyHash(string code, string hexHash) => Hash(code).Equals(hexHash, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.CreateNewProductGallerys;
|
||||
public record CreateNewProductGallerysCommand : IRequest<CreateNewProductGallerysResponseDto>
|
||||
{
|
||||
//
|
||||
public long ProductImageId { get; init; }
|
||||
//
|
||||
public long ProductId { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.CreateNewProductGallerys;
|
||||
public class CreateNewProductGallerysCommandHandler : IRequestHandler<CreateNewProductGallerysCommand, CreateNewProductGallerysResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewProductGallerysCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewProductGallerysResponseDto> Handle(CreateNewProductGallerysCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<ProductGallerys>();
|
||||
await _context.ProductGalleryss.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewProductGallerysEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewProductGallerysResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.CreateNewProductGallerys;
|
||||
public class CreateNewProductGallerysCommandValidator : AbstractValidator<CreateNewProductGallerysCommand>
|
||||
{
|
||||
public CreateNewProductGallerysCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.ProductImageId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ProductId)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewProductGallerysCommand>.CreateWithOptions((CreateNewProductGallerysCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.CreateNewProductGallerys;
|
||||
public class CreateNewProductGallerysResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.DeleteProductGallerys;
|
||||
public record DeleteProductGallerysCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.DeleteProductGallerys;
|
||||
public class DeleteProductGallerysCommandHandler : IRequestHandler<DeleteProductGallerysCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteProductGallerysCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteProductGallerysCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.ProductGalleryss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductGallerys), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.ProductGalleryss.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteProductGallerysEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.DeleteProductGallerys;
|
||||
public class DeleteProductGallerysCommandValidator : AbstractValidator<DeleteProductGallerysCommand>
|
||||
{
|
||||
public DeleteProductGallerysCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteProductGallerysCommand>.CreateWithOptions((DeleteProductGallerysCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.UpdateProductGallerys;
|
||||
public record UpdateProductGallerysCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
//
|
||||
public long ProductImageId { get; init; }
|
||||
//
|
||||
public long ProductId { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.UpdateProductGallerys;
|
||||
public class UpdateProductGallerysCommandHandler : IRequestHandler<UpdateProductGallerysCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateProductGallerysCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateProductGallerysCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.ProductGalleryss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductGallerys), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.ProductGalleryss.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateProductGallerysEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Commands.UpdateProductGallerys;
|
||||
public class UpdateProductGallerysCommandValidator : AbstractValidator<UpdateProductGallerysCommand>
|
||||
{
|
||||
public UpdateProductGallerysCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ProductImageId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ProductId)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateProductGallerysCommand>.CreateWithOptions((UpdateProductGallerysCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.EventHandlers;
|
||||
|
||||
public class CreateNewProductGallerysEventHandler : INotificationHandler<CreateNewProductGallerysEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
CreateNewProductGallerysEventHandler> _logger;
|
||||
|
||||
public CreateNewProductGallerysEventHandler(ILogger<CreateNewProductGallerysEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewProductGallerysEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.EventHandlers;
|
||||
|
||||
public class DeleteProductGallerysEventHandler : INotificationHandler<DeleteProductGallerysEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
DeleteProductGallerysEventHandler> _logger;
|
||||
|
||||
public DeleteProductGallerysEventHandler(ILogger<DeleteProductGallerysEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteProductGallerysEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.EventHandlers;
|
||||
|
||||
public class UpdateProductGallerysEventHandler : INotificationHandler<UpdateProductGallerysEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
UpdateProductGallerysEventHandler> _logger;
|
||||
|
||||
public UpdateProductGallerysEventHandler(ILogger<UpdateProductGallerysEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateProductGallerysEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetAllProductGallerysByFilter;
|
||||
public record GetAllProductGallerysByFilterQuery : IRequest<GetAllProductGallerysByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllProductGallerysByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllProductGallerysByFilterFilter
|
||||
{
|
||||
//
|
||||
public long? Id { get; set; }
|
||||
//
|
||||
public long? ProductImageId { get; set; }
|
||||
//
|
||||
public long? ProductId { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetAllProductGallerysByFilter;
|
||||
public class GetAllProductGallerysByFilterQueryHandler : IRequestHandler<GetAllProductGallerysByFilterQuery, GetAllProductGallerysByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllProductGallerysByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllProductGallerysByFilterResponseDto> Handle(GetAllProductGallerysByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.ProductGalleryss
|
||||
.ApplyOrder(sortBy: request.SortBy)
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
if (request.Filter is not null)
|
||||
{
|
||||
query = query
|
||||
.Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
|
||||
.Where(x => request.Filter.ProductImageId == null || x.ProductImageId == request.Filter.ProductImageId)
|
||||
.Where(x => request.Filter.ProductId == null || x.ProductId == request.Filter.ProductId)
|
||||
;
|
||||
}
|
||||
return new GetAllProductGallerysByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllProductGallerysByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetAllProductGallerysByFilter;
|
||||
public class GetAllProductGallerysByFilterQueryValidator : AbstractValidator<GetAllProductGallerysByFilterQuery>
|
||||
{
|
||||
public GetAllProductGallerysByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllProductGallerysByFilterQuery>.CreateWithOptions((GetAllProductGallerysByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetAllProductGallerysByFilter;
|
||||
public class GetAllProductGallerysByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllProductGallerysByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllProductGallerysByFilterResponseModel
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public long ProductImageId { get; set; }
|
||||
//
|
||||
public long ProductId { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetProductGallerys;
|
||||
public record GetProductGallerysQuery : IRequest<GetProductGallerysResponseDto>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetProductGallerys;
|
||||
public class GetProductGallerysQueryHandler : IRequestHandler<GetProductGallerysQuery, GetProductGallerysResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetProductGallerysQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetProductGallerysResponseDto> Handle(GetProductGallerysQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.ProductGalleryss
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetProductGallerysResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(ProductGallerys), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetProductGallerys;
|
||||
public class GetProductGallerysQueryValidator : AbstractValidator<GetProductGallerysQuery>
|
||||
{
|
||||
public GetProductGallerysQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetProductGallerysQuery>.CreateWithOptions((GetProductGallerysQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace CMSMicroservice.Application.ProductGallerysCQ.Queries.GetProductGallerys;
|
||||
public class GetProductGallerysResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public long ProductImageId { get; set; }
|
||||
//
|
||||
public long ProductId { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.CreateNewProductImages;
|
||||
public record CreateNewProductImagesCommand : IRequest<CreateNewProductImagesResponseDto>
|
||||
{
|
||||
//
|
||||
public string Title { get; init; }
|
||||
//
|
||||
public string ImagePath { get; init; }
|
||||
//
|
||||
public string ImageThumbnailPath { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.CreateNewProductImages;
|
||||
public class CreateNewProductImagesCommandHandler : IRequestHandler<CreateNewProductImagesCommand, CreateNewProductImagesResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewProductImagesCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewProductImagesResponseDto> Handle(CreateNewProductImagesCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<ProductImages>();
|
||||
await _context.ProductImagess.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewProductImagesEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewProductImagesResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.CreateNewProductImages;
|
||||
public class CreateNewProductImagesCommandValidator : AbstractValidator<CreateNewProductImagesCommand>
|
||||
{
|
||||
public CreateNewProductImagesCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ImagePath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ImageThumbnailPath)
|
||||
.NotEmpty();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewProductImagesCommand>.CreateWithOptions((CreateNewProductImagesCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.CreateNewProductImages;
|
||||
public class CreateNewProductImagesResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.DeleteProductImages;
|
||||
public record DeleteProductImagesCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.DeleteProductImages;
|
||||
public class DeleteProductImagesCommandHandler : IRequestHandler<DeleteProductImagesCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteProductImagesCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteProductImagesCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.ProductImagess
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductImages), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.ProductImagess.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteProductImagesEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.DeleteProductImages;
|
||||
public class DeleteProductImagesCommandValidator : AbstractValidator<DeleteProductImagesCommand>
|
||||
{
|
||||
public DeleteProductImagesCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteProductImagesCommand>.CreateWithOptions((DeleteProductImagesCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.UpdateProductImages;
|
||||
public record UpdateProductImagesCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
//
|
||||
public string Title { get; init; }
|
||||
//
|
||||
public string ImagePath { get; init; }
|
||||
//
|
||||
public string ImageThumbnailPath { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.UpdateProductImages;
|
||||
public class UpdateProductImagesCommandHandler : IRequestHandler<UpdateProductImagesCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateProductImagesCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateProductImagesCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.ProductImagess
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductImages), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.ProductImagess.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateProductImagesEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.UpdateProductImages;
|
||||
public class UpdateProductImagesCommandValidator : AbstractValidator<UpdateProductImagesCommand>
|
||||
{
|
||||
public UpdateProductImagesCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ImagePath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ImageThumbnailPath)
|
||||
.NotEmpty();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateProductImagesCommand>.CreateWithOptions((UpdateProductImagesCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.EventHandlers;
|
||||
|
||||
public class CreateNewProductImagesEventHandler : INotificationHandler<CreateNewProductImagesEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
CreateNewProductImagesEventHandler> _logger;
|
||||
|
||||
public CreateNewProductImagesEventHandler(ILogger<CreateNewProductImagesEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewProductImagesEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.EventHandlers;
|
||||
|
||||
public class DeleteProductImagesEventHandler : INotificationHandler<DeleteProductImagesEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
DeleteProductImagesEventHandler> _logger;
|
||||
|
||||
public DeleteProductImagesEventHandler(ILogger<DeleteProductImagesEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteProductImagesEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.EventHandlers;
|
||||
|
||||
public class UpdateProductImagesEventHandler : INotificationHandler<UpdateProductImagesEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
UpdateProductImagesEventHandler> _logger;
|
||||
|
||||
public UpdateProductImagesEventHandler(ILogger<UpdateProductImagesEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateProductImagesEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetAllProductImagesByFilter;
|
||||
public record GetAllProductImagesByFilterQuery : IRequest<GetAllProductImagesByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllProductImagesByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllProductImagesByFilterFilter
|
||||
{
|
||||
//
|
||||
public long? Id { get; set; }
|
||||
//
|
||||
public string? Title { get; set; }
|
||||
//
|
||||
public string? ImagePath { get; set; }
|
||||
//
|
||||
public string? ImageThumbnailPath { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetAllProductImagesByFilter;
|
||||
public class GetAllProductImagesByFilterQueryHandler : IRequestHandler<GetAllProductImagesByFilterQuery, GetAllProductImagesByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllProductImagesByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllProductImagesByFilterResponseDto> Handle(GetAllProductImagesByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.ProductImagess
|
||||
.ApplyOrder(sortBy: request.SortBy)
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
if (request.Filter is not null)
|
||||
{
|
||||
query = query
|
||||
.Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
|
||||
.Where(x => request.Filter.Title == null || x.Title.Contains(request.Filter.Title))
|
||||
.Where(x => request.Filter.ImagePath == null || x.ImagePath.Contains(request.Filter.ImagePath))
|
||||
.Where(x => request.Filter.ImageThumbnailPath == null || x.ImageThumbnailPath.Contains(request.Filter.ImageThumbnailPath))
|
||||
;
|
||||
}
|
||||
return new GetAllProductImagesByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllProductImagesByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetAllProductImagesByFilter;
|
||||
public class GetAllProductImagesByFilterQueryValidator : AbstractValidator<GetAllProductImagesByFilterQuery>
|
||||
{
|
||||
public GetAllProductImagesByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllProductImagesByFilterQuery>.CreateWithOptions((GetAllProductImagesByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetAllProductImagesByFilter;
|
||||
public class GetAllProductImagesByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllProductImagesByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllProductImagesByFilterResponseModel
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public string Title { get; set; }
|
||||
//
|
||||
public string ImagePath { get; set; }
|
||||
//
|
||||
public string ImageThumbnailPath { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetProductImages;
|
||||
public record GetProductImagesQuery : IRequest<GetProductImagesResponseDto>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetProductImages;
|
||||
public class GetProductImagesQueryHandler : IRequestHandler<GetProductImagesQuery, GetProductImagesResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetProductImagesQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetProductImagesResponseDto> Handle(GetProductImagesQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.ProductImagess
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetProductImagesResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(ProductImages), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetProductImages;
|
||||
public class GetProductImagesQueryValidator : AbstractValidator<GetProductImagesQuery>
|
||||
{
|
||||
public GetProductImagesQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetProductImagesQuery>.CreateWithOptions((GetProductImagesQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace CMSMicroservice.Application.ProductImagesCQ.Queries.GetProductImages;
|
||||
public class GetProductImagesResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public string Title { get; set; }
|
||||
//
|
||||
public string ImagePath { get; set; }
|
||||
//
|
||||
public string ImageThumbnailPath { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.CreateNewProducts;
|
||||
public record CreateNewProductsCommand : IRequest<CreateNewProductsResponseDto>
|
||||
{
|
||||
//
|
||||
public string Title { get; init; }
|
||||
//
|
||||
public string Description { get; init; }
|
||||
//
|
||||
public string ShortInfomation { get; init; }
|
||||
//
|
||||
public string FullInformation { get; init; }
|
||||
//
|
||||
public long Price { get; init; }
|
||||
//
|
||||
public int Discount { get; init; }
|
||||
//
|
||||
public int Rate { get; init; }
|
||||
//
|
||||
public string ImagePath { get; init; }
|
||||
//
|
||||
public string ThumbnailPath { get; init; }
|
||||
//
|
||||
public int SaleCount { get; init; }
|
||||
//
|
||||
public int ViewCount { get; init; }
|
||||
//
|
||||
public int RemainingCount { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.CreateNewProducts;
|
||||
public class CreateNewProductsCommandHandler : IRequestHandler<CreateNewProductsCommand, CreateNewProductsResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewProductsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewProductsResponseDto> Handle(CreateNewProductsCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<Products>();
|
||||
await _context.Productss.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewProductsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewProductsResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.CreateNewProducts;
|
||||
public class CreateNewProductsCommandValidator : AbstractValidator<CreateNewProductsCommand>
|
||||
{
|
||||
public CreateNewProductsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Description)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ShortInfomation)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.FullInformation)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Price)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Discount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Rate)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ImagePath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ThumbnailPath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.SaleCount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ViewCount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.RemainingCount)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewProductsCommand>.CreateWithOptions((CreateNewProductsCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.CreateNewProducts;
|
||||
public class CreateNewProductsResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.DeleteProducts;
|
||||
public record DeleteProductsCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.DeleteProducts;
|
||||
public class DeleteProductsCommandHandler : IRequestHandler<DeleteProductsCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteProductsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteProductsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Productss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Products), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.Productss.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteProductsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.DeleteProducts;
|
||||
public class DeleteProductsCommandValidator : AbstractValidator<DeleteProductsCommand>
|
||||
{
|
||||
public DeleteProductsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteProductsCommand>.CreateWithOptions((DeleteProductsCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.UpdateProducts;
|
||||
public record UpdateProductsCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
//
|
||||
public string Title { get; init; }
|
||||
//
|
||||
public string Description { get; init; }
|
||||
//
|
||||
public string ShortInfomation { get; init; }
|
||||
//
|
||||
public string FullInformation { get; init; }
|
||||
//
|
||||
public long Price { get; init; }
|
||||
//
|
||||
public int Discount { get; init; }
|
||||
//
|
||||
public int Rate { get; init; }
|
||||
//
|
||||
public string ImagePath { get; init; }
|
||||
//
|
||||
public string ThumbnailPath { get; init; }
|
||||
//
|
||||
public int SaleCount { get; init; }
|
||||
//
|
||||
public int ViewCount { get; init; }
|
||||
//
|
||||
public int RemainingCount { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.UpdateProducts;
|
||||
public class UpdateProductsCommandHandler : IRequestHandler<UpdateProductsCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateProductsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateProductsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Productss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Products), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.Productss.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateProductsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.UpdateProducts;
|
||||
public class UpdateProductsCommandValidator : AbstractValidator<UpdateProductsCommand>
|
||||
{
|
||||
public UpdateProductsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Description)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ShortInfomation)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.FullInformation)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Price)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Discount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Rate)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ImagePath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ThumbnailPath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.SaleCount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ViewCount)
|
||||
.NotNull();
|
||||
RuleFor(model => model.RemainingCount)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateProductsCommand>.CreateWithOptions((UpdateProductsCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductsCQ.EventHandlers;
|
||||
|
||||
public class CreateNewProductsEventHandler : INotificationHandler<CreateNewProductsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
CreateNewProductsEventHandler> _logger;
|
||||
|
||||
public CreateNewProductsEventHandler(ILogger<CreateNewProductsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewProductsEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductsCQ.EventHandlers;
|
||||
|
||||
public class DeleteProductsEventHandler : INotificationHandler<DeleteProductsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
DeleteProductsEventHandler> _logger;
|
||||
|
||||
public DeleteProductsEventHandler(ILogger<DeleteProductsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteProductsEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductsCQ.EventHandlers;
|
||||
|
||||
public class UpdateProductsEventHandler : INotificationHandler<UpdateProductsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
UpdateProductsEventHandler> _logger;
|
||||
|
||||
public UpdateProductsEventHandler(ILogger<UpdateProductsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateProductsEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetAllProductsByFilter;
|
||||
public record GetAllProductsByFilterQuery : IRequest<GetAllProductsByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllProductsByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllProductsByFilterFilter
|
||||
{
|
||||
//
|
||||
public long? Id { get; set; }
|
||||
//
|
||||
public string? Title { get; set; }
|
||||
//
|
||||
public string? Description { get; set; }
|
||||
//
|
||||
public string? ShortInfomation { get; set; }
|
||||
//
|
||||
public string? FullInformation { get; set; }
|
||||
//
|
||||
public long? Price { get; set; }
|
||||
//
|
||||
public int? Discount { get; set; }
|
||||
//
|
||||
public int? Rate { get; set; }
|
||||
//
|
||||
public string? ImagePath { get; set; }
|
||||
//
|
||||
public string? ThumbnailPath { get; set; }
|
||||
//
|
||||
public int? SaleCount { get; set; }
|
||||
//
|
||||
public int? ViewCount { get; set; }
|
||||
//
|
||||
public int? RemainingCount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetAllProductsByFilter;
|
||||
public class GetAllProductsByFilterQueryHandler : IRequestHandler<GetAllProductsByFilterQuery, GetAllProductsByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllProductsByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllProductsByFilterResponseDto> Handle(GetAllProductsByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.Productss
|
||||
.ApplyOrder(sortBy: request.SortBy)
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
if (request.Filter is not null)
|
||||
{
|
||||
query = query
|
||||
.Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
|
||||
.Where(x => request.Filter.Title == null || x.Title.Contains(request.Filter.Title))
|
||||
.Where(x => request.Filter.Description == null || x.Description.Contains(request.Filter.Description))
|
||||
.Where(x => request.Filter.ShortInfomation == null || x.ShortInfomation.Contains(request.Filter.ShortInfomation))
|
||||
.Where(x => request.Filter.FullInformation == null || x.FullInformation.Contains(request.Filter.FullInformation))
|
||||
.Where(x => request.Filter.Price == null || x.Price == request.Filter.Price)
|
||||
.Where(x => request.Filter.Discount == null || x.Discount == request.Filter.Discount)
|
||||
.Where(x => request.Filter.Rate == null || x.Rate == request.Filter.Rate)
|
||||
.Where(x => request.Filter.ImagePath == null || x.ImagePath.Contains(request.Filter.ImagePath))
|
||||
.Where(x => request.Filter.ThumbnailPath == null || x.ThumbnailPath.Contains(request.Filter.ThumbnailPath))
|
||||
.Where(x => request.Filter.SaleCount == null || x.SaleCount == request.Filter.SaleCount)
|
||||
.Where(x => request.Filter.ViewCount == null || x.ViewCount == request.Filter.ViewCount)
|
||||
.Where(x => request.Filter.RemainingCount == null || x.RemainingCount == request.Filter.RemainingCount)
|
||||
;
|
||||
}
|
||||
return new GetAllProductsByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllProductsByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetAllProductsByFilter;
|
||||
public class GetAllProductsByFilterQueryValidator : AbstractValidator<GetAllProductsByFilterQuery>
|
||||
{
|
||||
public GetAllProductsByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllProductsByFilterQuery>.CreateWithOptions((GetAllProductsByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetAllProductsByFilter;
|
||||
public class GetAllProductsByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllProductsByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllProductsByFilterResponseModel
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public string Title { get; set; }
|
||||
//
|
||||
public string Description { get; set; }
|
||||
//
|
||||
public string ShortInfomation { get; set; }
|
||||
//
|
||||
public string FullInformation { get; set; }
|
||||
//
|
||||
public long Price { get; set; }
|
||||
//
|
||||
public int Discount { get; set; }
|
||||
//
|
||||
public int Rate { get; set; }
|
||||
//
|
||||
public string ImagePath { get; set; }
|
||||
//
|
||||
public string ThumbnailPath { get; set; }
|
||||
//
|
||||
public int SaleCount { get; set; }
|
||||
//
|
||||
public int ViewCount { get; set; }
|
||||
//
|
||||
public int RemainingCount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetProducts;
|
||||
public record GetProductsQuery : IRequest<GetProductsResponseDto>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetProducts;
|
||||
public class GetProductsQueryHandler : IRequestHandler<GetProductsQuery, GetProductsResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetProductsQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetProductsResponseDto> Handle(GetProductsQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.Productss
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetProductsResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(Products), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetProducts;
|
||||
public class GetProductsQueryValidator : AbstractValidator<GetProductsQuery>
|
||||
{
|
||||
public GetProductsQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetProductsQuery>.CreateWithOptions((GetProductsQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user