Generator Changes at 11/12/2025 1:32:03 AM +03:30
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.CreateNewUserCarts;
|
||||
public record CreateNewUserCartsCommand : IRequest<CreateNewUserCartsResponseDto>
|
||||
{
|
||||
//
|
||||
public long ProductId { get; init; }
|
||||
//
|
||||
public long UserId { get; init; }
|
||||
//
|
||||
public int Count { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.CreateNewUserCarts;
|
||||
public class CreateNewUserCartsCommandHandler : IRequestHandler<CreateNewUserCartsCommand, CreateNewUserCartsResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewUserCartsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewUserCartsResponseDto> Handle(CreateNewUserCartsCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<UserCarts>();
|
||||
await _context.UserCartss.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewUserCartsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewUserCartsResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.CreateNewUserCarts;
|
||||
public class CreateNewUserCartsCommandValidator : AbstractValidator<CreateNewUserCartsCommand>
|
||||
{
|
||||
public CreateNewUserCartsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.ProductId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UserId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Count)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewUserCartsCommand>.CreateWithOptions((CreateNewUserCartsCommand)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.UserCartsCQ.Commands.CreateNewUserCarts;
|
||||
public class CreateNewUserCartsResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.DeleteUserCarts;
|
||||
public record DeleteUserCartsCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.DeleteUserCarts;
|
||||
public class DeleteUserCartsCommandHandler : IRequestHandler<DeleteUserCartsCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteUserCartsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteUserCartsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UserCartss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserCarts), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.UserCartss.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteUserCartsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.DeleteUserCarts;
|
||||
public class DeleteUserCartsCommandValidator : AbstractValidator<DeleteUserCartsCommand>
|
||||
{
|
||||
public DeleteUserCartsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteUserCartsCommand>.CreateWithOptions((DeleteUserCartsCommand)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.UserCartsCQ.Commands.UpdateUserCarts;
|
||||
public record UpdateUserCartsCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
//
|
||||
public long ProductId { get; init; }
|
||||
//
|
||||
public long UserId { get; init; }
|
||||
//
|
||||
public int Count { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.UpdateUserCarts;
|
||||
public class UpdateUserCartsCommandHandler : IRequestHandler<UpdateUserCartsCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateUserCartsCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateUserCartsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UserCartss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserCarts), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.UserCartss.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateUserCartsEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Commands.UpdateUserCarts;
|
||||
public class UpdateUserCartsCommandValidator : AbstractValidator<UpdateUserCartsCommand>
|
||||
{
|
||||
public UpdateUserCartsCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.ProductId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UserId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Count)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateUserCartsCommand>.CreateWithOptions((UpdateUserCartsCommand)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.UserCartsCQ.EventHandlers;
|
||||
|
||||
public class CreateNewUserCartsEventHandler : INotificationHandler<CreateNewUserCartsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
CreateNewUserCartsEventHandler> _logger;
|
||||
|
||||
public CreateNewUserCartsEventHandler(ILogger<CreateNewUserCartsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewUserCartsEvent 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.UserCartsCQ.EventHandlers;
|
||||
|
||||
public class DeleteUserCartsEventHandler : INotificationHandler<DeleteUserCartsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
DeleteUserCartsEventHandler> _logger;
|
||||
|
||||
public DeleteUserCartsEventHandler(ILogger<DeleteUserCartsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteUserCartsEvent 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.UserCartsCQ.EventHandlers;
|
||||
|
||||
public class UpdateUserCartsEventHandler : INotificationHandler<UpdateUserCartsEvent>
|
||||
{
|
||||
private readonly ILogger<
|
||||
UpdateUserCartsEventHandler> _logger;
|
||||
|
||||
public UpdateUserCartsEventHandler(ILogger<UpdateUserCartsEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateUserCartsEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Queries.GetAllUserCartsByFilter;
|
||||
public record GetAllUserCartsByFilterQuery : IRequest<GetAllUserCartsByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllUserCartsByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllUserCartsByFilterFilter
|
||||
{
|
||||
//
|
||||
public long? Id { get; set; }
|
||||
//
|
||||
public long? ProductId { get; set; }
|
||||
//
|
||||
public long? UserId { get; set; }
|
||||
//
|
||||
public int? Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Queries.GetAllUserCartsByFilter;
|
||||
public class GetAllUserCartsByFilterQueryHandler : IRequestHandler<GetAllUserCartsByFilterQuery, GetAllUserCartsByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllUserCartsByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllUserCartsByFilterResponseDto> Handle(GetAllUserCartsByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.UserCartss
|
||||
.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.UserId == null || x.UserId == request.Filter.UserId)
|
||||
.Where(x => request.Filter.Count == null || x.Count == request.Filter.Count)
|
||||
;
|
||||
}
|
||||
return new GetAllUserCartsByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllUserCartsByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Queries.GetAllUserCartsByFilter;
|
||||
public class GetAllUserCartsByFilterQueryValidator : AbstractValidator<GetAllUserCartsByFilterQuery>
|
||||
{
|
||||
public GetAllUserCartsByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllUserCartsByFilterQuery>.CreateWithOptions((GetAllUserCartsByFilterQuery)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.UserCartsCQ.Queries.GetAllUserCartsByFilter;
|
||||
public class GetAllUserCartsByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllUserCartsByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllUserCartsByFilterResponseModel
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public long ProductId { get; set; }
|
||||
//
|
||||
public long UserId { get; set; }
|
||||
//
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Queries.GetUserCarts;
|
||||
public record GetUserCartsQuery : IRequest<GetUserCartsResponseDto>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Queries.GetUserCarts;
|
||||
public class GetUserCartsQueryHandler : IRequestHandler<GetUserCartsQuery, GetUserCartsResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetUserCartsQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetUserCartsResponseDto> Handle(GetUserCartsQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.UserCartss
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetUserCartsResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(UserCarts), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserCartsCQ.Queries.GetUserCarts;
|
||||
public class GetUserCartsQueryValidator : AbstractValidator<GetUserCartsQuery>
|
||||
{
|
||||
public GetUserCartsQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetUserCartsQuery>.CreateWithOptions((GetUserCartsQuery)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.UserCartsCQ.Queries.GetUserCarts;
|
||||
public class GetUserCartsResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
//
|
||||
public long ProductId { get; set; }
|
||||
//
|
||||
public long UserId { get; set; }
|
||||
//
|
||||
public int Count { get; set; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user