Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries. - Added Create, Delete, Get, and Update validators for Product Tags. - Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags. - Each service class integrates with CQRS for command and query handling. - Established mapping profiles for Product Galleries.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.OrderManagementCQ.Commands.CancelOrderByAdmin;
|
||||
|
||||
/// <summary>
|
||||
/// دستور لغو سفارش توسط Admin
|
||||
/// </summary>
|
||||
public class CancelOrderByAdminCommand : IRequest<Unit>
|
||||
{
|
||||
public long OrderId { get; set; }
|
||||
public string CancelReason { get; set; } = string.Empty;
|
||||
public bool RefundToWallet { get; set; } = true;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.OrderManagementCQ.Commands.CancelOrderByAdmin;
|
||||
|
||||
public class CancelOrderByAdminCommandHandler : IRequestHandler<CancelOrderByAdminCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
private readonly ILogger<CancelOrderByAdminCommandHandler> _logger;
|
||||
|
||||
public CancelOrderByAdminCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
ICurrentUserService currentUser,
|
||||
ILogger<CancelOrderByAdminCommandHandler> logger)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(CancelOrderByAdminCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// بررسی Admin
|
||||
if (string.IsNullOrEmpty(_currentUser.UserId))
|
||||
{
|
||||
throw new UnauthorizedAccessException("کاربر احراز هویت نشده است");
|
||||
}
|
||||
|
||||
var order = await _context.UserOrders
|
||||
.Include(x => x.User)
|
||||
.ThenInclude(x => x.UserWallets)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.OrderId && !x.IsDeleted, cancellationToken);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"سفارش با شناسه {request.OrderId} یافت نشد");
|
||||
}
|
||||
|
||||
if (order.DeliveryStatus == DeliveryStatus.Cancelled)
|
||||
{
|
||||
throw new InvalidOperationException("این سفارش قبلاً لغو شده است");
|
||||
}
|
||||
|
||||
if (order.DeliveryStatus == DeliveryStatus.Delivered)
|
||||
{
|
||||
throw new InvalidOperationException("سفارش تحویل داده شده را نمیتوان لغو کرد");
|
||||
}
|
||||
|
||||
// تغییر وضعیت به لغو شده
|
||||
order.DeliveryStatus = DeliveryStatus.Cancelled;
|
||||
order.DeliveryDescription = $"لغو توسط Admin: {request.CancelReason}";
|
||||
|
||||
// بازگشت وجه به کیف پول
|
||||
if (request.RefundToWallet && order.PaymentMethod == PaymentMethod.Wallet)
|
||||
{
|
||||
var wallet = order.User.UserWallets.FirstOrDefault();
|
||||
if (wallet != null)
|
||||
{
|
||||
var walletLog = new UserWalletChangeLog
|
||||
{
|
||||
WalletId = wallet.Id,
|
||||
CurrentBalance = wallet.Balance,
|
||||
CurrentNetworkBalance = wallet.NetworkBalance,
|
||||
CurrentDiscountBalance = wallet.DiscountBalance,
|
||||
ChangeValue = order.Amount,
|
||||
ChangeDiscountValue = 0,
|
||||
IsIncrease = true,
|
||||
RefrenceId = order.Id
|
||||
};
|
||||
|
||||
wallet.Balance += order.Amount;
|
||||
|
||||
await _context.UserWalletChangeLogs.AddAsync(walletLog, cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Refund processed. OrderId: {OrderId}, Amount: {Amount}, UserId: {UserId}",
|
||||
order.Id,
|
||||
order.Amount,
|
||||
order.UserId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Order cancelled by admin. OrderId: {OrderId}, Reason: {Reason}, Refunded: {Refunded}, Admin: {AdminId}",
|
||||
order.Id,
|
||||
request.CancelReason,
|
||||
request.RefundToWallet,
|
||||
_currentUser.UserId
|
||||
);
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace CMSMicroservice.Application.OrderManagementCQ.Commands.CancelOrderByAdmin;
|
||||
|
||||
public class CancelOrderByAdminCommandValidator : AbstractValidator<CancelOrderByAdminCommand>
|
||||
{
|
||||
public CancelOrderByAdminCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.OrderId)
|
||||
.GreaterThan(0).WithMessage("شناسه سفارش نامعتبر است");
|
||||
|
||||
RuleFor(x => x.CancelReason)
|
||||
.NotEmpty().WithMessage("دلیل لغو الزامی است")
|
||||
.MaximumLength(500).WithMessage("دلیل لغو نمیتواند بیشتر از 500 کاراکتر باشد");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.OrderManagementCQ.Commands.UpdateOrderStatus;
|
||||
|
||||
/// <summary>
|
||||
/// دستور تغییر وضعیت ارسال سفارش (Admin)
|
||||
/// </summary>
|
||||
public class UpdateOrderStatusCommand : IRequest<Unit>
|
||||
{
|
||||
public long OrderId { get; set; }
|
||||
public DeliveryStatus NewStatus { get; set; }
|
||||
public string? TrackingCode { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.OrderManagementCQ.Commands.UpdateOrderStatus;
|
||||
|
||||
public class UpdateOrderStatusCommandHandler : IRequestHandler<UpdateOrderStatusCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
private readonly ILogger<UpdateOrderStatusCommandHandler> _logger;
|
||||
|
||||
public UpdateOrderStatusCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
ICurrentUserService currentUser,
|
||||
ILogger<UpdateOrderStatusCommandHandler> logger)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateOrderStatusCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// بررسی Admin
|
||||
if (string.IsNullOrEmpty(_currentUser.UserId))
|
||||
{
|
||||
throw new UnauthorizedAccessException("کاربر احراز هویت نشده است");
|
||||
}
|
||||
|
||||
var order = await _context.UserOrders
|
||||
.FirstOrDefaultAsync(x => x.Id == request.OrderId && !x.IsDeleted, cancellationToken);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
throw new KeyNotFoundException($"سفارش با شناسه {request.OrderId} یافت نشد");
|
||||
}
|
||||
|
||||
var oldStatus = order.DeliveryStatus;
|
||||
|
||||
order.DeliveryStatus = request.NewStatus;
|
||||
|
||||
if (!string.IsNullOrEmpty(request.TrackingCode))
|
||||
{
|
||||
order.TrackingCode = request.TrackingCode.Trim();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(request.Description))
|
||||
{
|
||||
order.DeliveryDescription = request.Description.Trim();
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Order status updated by admin. OrderId: {OrderId}, OldStatus: {OldStatus}, NewStatus: {NewStatus}, Admin: {AdminId}",
|
||||
order.Id,
|
||||
oldStatus,
|
||||
request.NewStatus,
|
||||
_currentUser.UserId
|
||||
);
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace CMSMicroservice.Application.OrderManagementCQ.Commands.UpdateOrderStatus;
|
||||
|
||||
public class UpdateOrderStatusCommandValidator : AbstractValidator<UpdateOrderStatusCommand>
|
||||
{
|
||||
public UpdateOrderStatusCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.OrderId)
|
||||
.GreaterThan(0).WithMessage("شناسه سفارش نامعتبر است");
|
||||
|
||||
RuleFor(x => x.NewStatus)
|
||||
.IsInEnum().WithMessage("وضعیت ارسال نامعتبر است");
|
||||
|
||||
RuleFor(x => x.TrackingCode)
|
||||
.MaximumLength(50).WithMessage("کد رهگیری نمیتواند بیشتر از 50 کاراکتر باشد")
|
||||
.When(x => !string.IsNullOrEmpty(x.TrackingCode));
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(500).WithMessage("توضیحات نمیتواند بیشتر از 500 کاراکتر باشد")
|
||||
.When(x => !string.IsNullOrEmpty(x.Description));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user