Generator Changes at 9/27/2025 8:46:36 AM
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.CreateNewUser;
|
||||
public record CreateNewUserCommand : IRequest<CreateNewUserResponseDto>
|
||||
{
|
||||
//نام
|
||||
public string? FirstName { get; init; }
|
||||
//نام خانوادگی
|
||||
public string? LastName { get; init; }
|
||||
//شماره موبایل
|
||||
public string Mobile { get; init; }
|
||||
//کد ملی
|
||||
public string? NationalCode { get; init; }
|
||||
//آدرس آواتار
|
||||
public string? AvatarPath { get; init; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.CreateNewUser;
|
||||
public class CreateNewUserCommandHandler : IRequestHandler<CreateNewUserCommand, CreateNewUserResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewUserCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewUserResponseDto> Handle(CreateNewUserCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<User>();
|
||||
await _context.Users.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewUserEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewUserResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.CreateNewUser;
|
||||
public class CreateNewUserCommandValidator : AbstractValidator<CreateNewUserCommand>
|
||||
{
|
||||
public CreateNewUserCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Mobile)
|
||||
.NotEmpty();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewUserCommand>.CreateWithOptions((CreateNewUserCommand)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.UserCQ.Commands.CreateNewUser;
|
||||
public class CreateNewUserResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.DeleteUser;
|
||||
public record DeleteUserCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.DeleteUser;
|
||||
public class DeleteUserCommandHandler : IRequestHandler<DeleteUserCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteUserCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Users
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(User), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.Users.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteUserEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.DeleteUser;
|
||||
public class DeleteUserCommandValidator : AbstractValidator<DeleteUserCommand>
|
||||
{
|
||||
public DeleteUserCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteUserCommand>.CreateWithOptions((DeleteUserCommand)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.UserCQ.Commands.UpdateUser;
|
||||
public record UpdateUserCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
//نام
|
||||
public string? FirstName { get; init; }
|
||||
//نام خانوادگی
|
||||
public string? LastName { get; init; }
|
||||
//شماره موبایل
|
||||
public string Mobile { get; init; }
|
||||
//کد ملی
|
||||
public string? NationalCode { get; init; }
|
||||
//آدرس آواتار
|
||||
public string? AvatarPath { get; init; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.UpdateUser;
|
||||
public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateUserCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Users
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(User), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.Users.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateUserEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Commands.UpdateUser;
|
||||
public class UpdateUserCommandValidator : AbstractValidator<UpdateUserCommand>
|
||||
{
|
||||
public UpdateUserCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Mobile)
|
||||
.NotEmpty();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateUserCommand>.CreateWithOptions((UpdateUserCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.UserCQ.EventHandlers;
|
||||
|
||||
public class CreateNewUserEventHandler : INotificationHandler<CreateNewUserEvent>
|
||||
{
|
||||
private readonly ILogger<CreateNewUserEventHandler> _logger;
|
||||
|
||||
public CreateNewUserEventHandler(ILogger<CreateNewUserEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewUserEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.UserCQ.EventHandlers;
|
||||
|
||||
public class DeleteUserEventHandler : INotificationHandler<DeleteUserEvent>
|
||||
{
|
||||
private readonly ILogger<DeleteUserEventHandler> _logger;
|
||||
|
||||
public DeleteUserEventHandler(ILogger<DeleteUserEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteUserEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.UserCQ.EventHandlers;
|
||||
|
||||
public class UpdateUserEventHandler : INotificationHandler<UpdateUserEvent>
|
||||
{
|
||||
private readonly ILogger<UpdateUserEventHandler> _logger;
|
||||
|
||||
public UpdateUserEventHandler(ILogger<UpdateUserEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateUserEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Queries.GetAllUserByFilter;
|
||||
public record GetAllUserByFilterQuery : IRequest<GetAllUserByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllUserByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllUserByFilterFilter
|
||||
{
|
||||
//شناسه
|
||||
public long? Id { get; set; }
|
||||
//نام
|
||||
public string? FirstName { get; set; }
|
||||
//نام خانوادگی
|
||||
public string? LastName { get; set; }
|
||||
//شماره موبایل
|
||||
public string? Mobile { get; set; }
|
||||
//کد ملی
|
||||
public string? NationalCode { get; set; }
|
||||
//آدرس آواتار
|
||||
public string? AvatarPath { get; set; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Queries.GetAllUserByFilter;
|
||||
public class GetAllUserByFilterQueryHandler : IRequestHandler<GetAllUserByFilterQuery, GetAllUserByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllUserByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllUserByFilterResponseDto> Handle(GetAllUserByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.Users
|
||||
.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.FirstName == null || x.FirstName.Contains(request.Filter.FirstName))
|
||||
.Where(x => request.Filter.LastName == null || x.LastName.Contains(request.Filter.LastName))
|
||||
.Where(x => request.Filter.Mobile == null || x.Mobile.Contains(request.Filter.Mobile))
|
||||
.Where(x => request.Filter.NationalCode == null || x.NationalCode.Contains(request.Filter.NationalCode))
|
||||
.Where(x => request.Filter.AvatarPath == null || x.AvatarPath.Contains(request.Filter.AvatarPath))
|
||||
.Where(x => request.Filter.ParentId == null || x.ParentId == request.Filter.ParentId)
|
||||
;
|
||||
}
|
||||
return new GetAllUserByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllUserByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Queries.GetAllUserByFilter;
|
||||
public class GetAllUserByFilterQueryValidator : AbstractValidator<GetAllUserByFilterQuery>
|
||||
{
|
||||
public GetAllUserByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllUserByFilterQuery>.CreateWithOptions((GetAllUserByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Queries.GetAllUserByFilter;
|
||||
public class GetAllUserByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllUserByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllUserByFilterResponseModel
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//نام
|
||||
public string? FirstName { get; set; }
|
||||
//نام خانوادگی
|
||||
public string? LastName { get; set; }
|
||||
//شماره موبایل
|
||||
public string Mobile { get; set; }
|
||||
//کد ملی
|
||||
public string? NationalCode { get; set; }
|
||||
//آدرس آواتار
|
||||
public string? AvatarPath { get; set; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Queries.GetUser;
|
||||
public record GetUserQuery : IRequest<GetUserResponseDto>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Queries.GetUser;
|
||||
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, GetUserResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetUserQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetUserResponseDto> Handle(GetUserQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetUserResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(User), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserCQ.Queries.GetUser;
|
||||
public class GetUserQueryValidator : AbstractValidator<GetUserQuery>
|
||||
{
|
||||
public GetUserQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetUserQuery>.CreateWithOptions((GetUserQuery)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.UserCQ.Queries.GetUser;
|
||||
public class GetUserResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//نام
|
||||
public string? FirstName { get; set; }
|
||||
//نام خانوادگی
|
||||
public string? LastName { get; set; }
|
||||
//شماره موبایل
|
||||
public string Mobile { get; set; }
|
||||
//کد ملی
|
||||
public string? NationalCode { get; set; }
|
||||
//آدرس آواتار
|
||||
public string? AvatarPath { get; set; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; set; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user