Generator Changes at 9/27/2025 8:46:36 AM
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.CreateNewUserAddress;
|
||||
public record CreateNewUserAddressCommand : IRequest<CreateNewUserAddressResponseDto>
|
||||
{
|
||||
//شناسه کاربر
|
||||
public long UserId { get; init; }
|
||||
//عنوان
|
||||
public string Title { get; init; }
|
||||
//آدرس
|
||||
public string Address { get; init; }
|
||||
//کدپستی
|
||||
public string PostalCode { get; init; }
|
||||
//پیشفرض؟
|
||||
public bool IsDefault { get; init; }
|
||||
//شناسه شهر
|
||||
public long CityId { get; init; }
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.CreateNewUserAddress;
|
||||
public class CreateNewUserAddressCommandHandler : IRequestHandler<CreateNewUserAddressCommand, CreateNewUserAddressResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewUserAddressCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewUserAddressResponseDto> Handle(CreateNewUserAddressCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<UserAddress>();
|
||||
await _context.UserAddresss.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewUserAddressEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewUserAddressResponseDto>();
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.CreateNewUserAddress;
|
||||
public class CreateNewUserAddressCommandValidator : AbstractValidator<CreateNewUserAddressCommand>
|
||||
{
|
||||
public CreateNewUserAddressCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.UserId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Address)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.PostalCode)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.IsDefault)
|
||||
.NotNull();
|
||||
RuleFor(model => model.CityId)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewUserAddressCommand>.CreateWithOptions((CreateNewUserAddressCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.CreateNewUserAddress;
|
||||
public class CreateNewUserAddressResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.DeleteUserAddress;
|
||||
public record DeleteUserAddressCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.DeleteUserAddress;
|
||||
public class DeleteUserAddressCommandHandler : IRequestHandler<DeleteUserAddressCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteUserAddressCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteUserAddressCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UserAddresss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserAddress), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.UserAddresss.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteUserAddressEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.DeleteUserAddress;
|
||||
public class DeleteUserAddressCommandValidator : AbstractValidator<DeleteUserAddressCommand>
|
||||
{
|
||||
public DeleteUserAddressCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteUserAddressCommand>.CreateWithOptions((DeleteUserAddressCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.UpdateUserAddress;
|
||||
public record UpdateUserAddressCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
//شناسه کاربر
|
||||
public long UserId { get; init; }
|
||||
//عنوان
|
||||
public string Title { get; init; }
|
||||
//آدرس
|
||||
public string Address { get; init; }
|
||||
//کدپستی
|
||||
public string PostalCode { get; init; }
|
||||
//پیشفرض؟
|
||||
public bool IsDefault { get; init; }
|
||||
//شناسه شهر
|
||||
public long CityId { get; init; }
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.UpdateUserAddress;
|
||||
public class UpdateUserAddressCommandHandler : IRequestHandler<UpdateUserAddressCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateUserAddressCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateUserAddressCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UserAddresss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserAddress), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.UserAddresss.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateUserAddressEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.UpdateUserAddress;
|
||||
public class UpdateUserAddressCommandValidator : AbstractValidator<UpdateUserAddressCommand>
|
||||
{
|
||||
public UpdateUserAddressCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UserId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Address)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.PostalCode)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.IsDefault)
|
||||
.NotNull();
|
||||
RuleFor(model => model.CityId)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateUserAddressCommand>.CreateWithOptions((UpdateUserAddressCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.EventHandlers;
|
||||
|
||||
public class CreateNewUserAddressEventHandler : INotificationHandler<CreateNewUserAddressEvent>
|
||||
{
|
||||
private readonly ILogger<CreateNewUserAddressEventHandler> _logger;
|
||||
|
||||
public CreateNewUserAddressEventHandler(ILogger<CreateNewUserAddressEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewUserAddressEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.EventHandlers;
|
||||
|
||||
public class DeleteUserAddressEventHandler : INotificationHandler<DeleteUserAddressEvent>
|
||||
{
|
||||
private readonly ILogger<DeleteUserAddressEventHandler> _logger;
|
||||
|
||||
public DeleteUserAddressEventHandler(ILogger<DeleteUserAddressEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteUserAddressEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.EventHandlers;
|
||||
|
||||
public class UpdateUserAddressEventHandler : INotificationHandler<UpdateUserAddressEvent>
|
||||
{
|
||||
private readonly ILogger<UpdateUserAddressEventHandler> _logger;
|
||||
|
||||
public UpdateUserAddressEventHandler(ILogger<UpdateUserAddressEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateUserAddressEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetAllUserAddressByFilter;
|
||||
public record GetAllUserAddressByFilterQuery : IRequest<GetAllUserAddressByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllUserAddressByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllUserAddressByFilterFilter
|
||||
{
|
||||
//شناسه
|
||||
public long? Id { get; set; }
|
||||
//شناسه کاربر
|
||||
public long? UserId { get; set; }
|
||||
//عنوان
|
||||
public string? Title { get; set; }
|
||||
//آدرس
|
||||
public string? Address { get; set; }
|
||||
//کدپستی
|
||||
public string? PostalCode { get; set; }
|
||||
//پیشفرض؟
|
||||
public bool? IsDefault { get; set; }
|
||||
//شناسه شهر
|
||||
public long? CityId { get; set; }
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetAllUserAddressByFilter;
|
||||
public class GetAllUserAddressByFilterQueryHandler : IRequestHandler<GetAllUserAddressByFilterQuery, GetAllUserAddressByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllUserAddressByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllUserAddressByFilterResponseDto> Handle(GetAllUserAddressByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.UserAddresss
|
||||
.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.UserId == null || x.UserId == request.Filter.UserId)
|
||||
.Where(x => request.Filter.Title == null || x.Title.Contains(request.Filter.Title))
|
||||
.Where(x => request.Filter.Address == null || x.Address.Contains(request.Filter.Address))
|
||||
.Where(x => request.Filter.PostalCode == null || x.PostalCode.Contains(request.Filter.PostalCode))
|
||||
.Where(x => request.Filter.IsDefault == null || x.IsDefault == request.Filter.IsDefault)
|
||||
.Where(x => request.Filter.CityId == null || x.CityId == request.Filter.CityId)
|
||||
;
|
||||
}
|
||||
return new GetAllUserAddressByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllUserAddressByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetAllUserAddressByFilter;
|
||||
public class GetAllUserAddressByFilterQueryValidator : AbstractValidator<GetAllUserAddressByFilterQuery>
|
||||
{
|
||||
public GetAllUserAddressByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllUserAddressByFilterQuery>.CreateWithOptions((GetAllUserAddressByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetAllUserAddressByFilter;
|
||||
public class GetAllUserAddressByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllUserAddressByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllUserAddressByFilterResponseModel
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//شناسه کاربر
|
||||
public long UserId { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
//آدرس
|
||||
public string Address { get; set; }
|
||||
//کدپستی
|
||||
public string PostalCode { get; set; }
|
||||
//پیشفرض؟
|
||||
public bool IsDefault { get; set; }
|
||||
//شناسه شهر
|
||||
public long CityId { get; set; }
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetUserAddress;
|
||||
public record GetUserAddressQuery : IRequest<GetUserAddressResponseDto>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetUserAddress;
|
||||
public class GetUserAddressQueryHandler : IRequestHandler<GetUserAddressQuery, GetUserAddressResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetUserAddressQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetUserAddressResponseDto> Handle(GetUserAddressQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.UserAddresss
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetUserAddressResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(UserAddress), request.Id);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetUserAddress;
|
||||
public class GetUserAddressQueryValidator : AbstractValidator<GetUserAddressQuery>
|
||||
{
|
||||
public GetUserAddressQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetUserAddressQuery>.CreateWithOptions((GetUserAddressQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Queries.GetUserAddress;
|
||||
public class GetUserAddressResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//شناسه کاربر
|
||||
public long UserId { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
//آدرس
|
||||
public string Address { get; set; }
|
||||
//کدپستی
|
||||
public string PostalCode { get; set; }
|
||||
//پیشفرض؟
|
||||
public bool IsDefault { get; set; }
|
||||
//شناسه شهر
|
||||
public long CityId { get; set; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user