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,35 @@
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.ManualPaymentCQ.Commands.CreateManualPayment;
|
||||
|
||||
/// <summary>
|
||||
/// دستور ثبت پرداخت دستی توسط Admin
|
||||
/// </summary>
|
||||
public class CreateManualPaymentCommand : IRequest<long>
|
||||
{
|
||||
/// <summary>
|
||||
/// شناسه کاربری که پرداخت برای او ثبت میشود
|
||||
/// </summary>
|
||||
public long UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// مبلغ تراکنش (ریال)
|
||||
/// </summary>
|
||||
public long Amount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// نوع تراکنش دستی
|
||||
/// </summary>
|
||||
public ManualPaymentType Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// توضیحات (اجباری)
|
||||
/// </summary>
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// شماره مرجع یا شماره فیش (اختیاری)
|
||||
/// </summary>
|
||||
public string? ReferenceNumber { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using CMSMicroservice.Application.Common.Exceptions;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Entities;
|
||||
using CMSMicroservice.Domain.Entities.Payment;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ManualPaymentCQ.Commands.CreateManualPayment;
|
||||
|
||||
public class CreateManualPaymentCommandHandler : IRequestHandler<CreateManualPaymentCommand, long>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
private readonly ILogger<CreateManualPaymentCommandHandler> _logger;
|
||||
|
||||
public CreateManualPaymentCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
ICurrentUserService currentUser,
|
||||
ILogger<CreateManualPaymentCommandHandler> logger)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<long> Handle(
|
||||
CreateManualPaymentCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Creating manual payment for UserId: {UserId}, Amount: {Amount}, Type: {Type}",
|
||||
request.UserId,
|
||||
request.Amount,
|
||||
request.Type
|
||||
);
|
||||
|
||||
// 1. بررسی وجود کاربر
|
||||
var user = await _context.Users
|
||||
.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
_logger.LogWarning("User not found: {UserId}", request.UserId);
|
||||
throw new NotFoundException(nameof(User), request.UserId);
|
||||
}
|
||||
|
||||
// 2. بررسی Admin فعلی
|
||||
var currentUserId = _currentUser.UserId;
|
||||
if (string.IsNullOrEmpty(currentUserId))
|
||||
{
|
||||
throw new UnauthorizedAccessException("کاربر احراز هویت نشده است");
|
||||
}
|
||||
|
||||
if (!long.TryParse(currentUserId, out var requestedById))
|
||||
{
|
||||
throw new UnauthorizedAccessException("شناسه کاربر نامعتبر است");
|
||||
}
|
||||
|
||||
// 3. ایجاد ManualPayment
|
||||
var manualPayment = new ManualPayment
|
||||
{
|
||||
UserId = request.UserId,
|
||||
Amount = request.Amount,
|
||||
Type = request.Type,
|
||||
Description = request.Description,
|
||||
ReferenceNumber = request.ReferenceNumber,
|
||||
Status = ManualPaymentStatus.Pending,
|
||||
RequestedBy = requestedById
|
||||
};
|
||||
|
||||
_context.ManualPayments.Add(manualPayment);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Manual payment created successfully. Id: {Id}, UserId: {UserId}, RequestedBy: {RequestedBy}",
|
||||
manualPayment.Id,
|
||||
request.UserId,
|
||||
requestedById
|
||||
);
|
||||
|
||||
return manualPayment.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(
|
||||
ex,
|
||||
"Error creating manual payment for UserId: {UserId}",
|
||||
request.UserId
|
||||
);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace CMSMicroservice.Application.ManualPaymentCQ.Commands.CreateManualPayment;
|
||||
|
||||
public class CreateManualPaymentCommandValidator : AbstractValidator<CreateManualPaymentCommand>
|
||||
{
|
||||
public CreateManualPaymentCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.UserId)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("شناسه کاربر باید بزرگتر از صفر باشد");
|
||||
|
||||
RuleFor(x => x.Amount)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("مبلغ باید بزرگتر از صفر باشد")
|
||||
.LessThanOrEqualTo(1_000_000_000)
|
||||
.WithMessage("مبلغ نمیتواند بیشتر از 1 میلیارد ریال باشد");
|
||||
|
||||
RuleFor(x => x.Type)
|
||||
.IsInEnum()
|
||||
.WithMessage("نوع تراکنش نامعتبر است");
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.NotEmpty()
|
||||
.WithMessage("توضیحات الزامی است")
|
||||
.MaximumLength(1000)
|
||||
.WithMessage("توضیحات نمیتواند بیشتر از 1000 کاراکتر باشد");
|
||||
|
||||
RuleFor(x => x.ReferenceNumber)
|
||||
.MaximumLength(100)
|
||||
.WithMessage("شماره مرجع نمیتواند بیشتر از 100 کاراکتر باشد")
|
||||
.When(x => !string.IsNullOrEmpty(x.ReferenceNumber));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user