initial commit
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.PurchaseOrderManager.Commands;
|
||||
|
||||
public class CreatePurchaseOrderResult
|
||||
{
|
||||
public PurchaseOrder? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePurchaseOrderRequest : IRequest<CreatePurchaseOrderResult>
|
||||
{
|
||||
public DateTime? OrderDate { get; init; }
|
||||
public string? OrderStatus { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? VendorId { get; init; }
|
||||
public string? TaxId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreatePurchaseOrderValidator : AbstractValidator<CreatePurchaseOrderRequest>
|
||||
{
|
||||
public CreatePurchaseOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.OrderDate).NotEmpty();
|
||||
RuleFor(x => x.OrderStatus).NotEmpty();
|
||||
RuleFor(x => x.VendorId).NotEmpty();
|
||||
RuleFor(x => x.TaxId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreatePurchaseOrderHandler : IRequestHandler<CreatePurchaseOrderRequest, CreatePurchaseOrderResult>
|
||||
{
|
||||
private readonly ICommandRepository<PurchaseOrder> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
private readonly PurchaseOrderService _purchaseOrderService;
|
||||
|
||||
public CreatePurchaseOrderHandler(
|
||||
ICommandRepository<PurchaseOrder> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService,
|
||||
PurchaseOrderService purchaseOrderService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
_purchaseOrderService = purchaseOrderService;
|
||||
}
|
||||
|
||||
public async Task<CreatePurchaseOrderResult> Handle(CreatePurchaseOrderRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new PurchaseOrder();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Number = _numberSequenceService.GenerateNumber(nameof(PurchaseOrder), "", "PO");
|
||||
entity.OrderDate = request.OrderDate;
|
||||
entity.OrderStatus = (PurchaseOrderStatus)int.Parse(request.OrderStatus!);
|
||||
entity.Description = request.Description;
|
||||
entity.VendorId = request.VendorId;
|
||||
entity.TaxId = request.TaxId;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
_purchaseOrderService.Recalculate(entity.Id);
|
||||
|
||||
return new CreatePurchaseOrderResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.PurchaseOrderManager.Commands;
|
||||
|
||||
public class DeletePurchaseOrderResult
|
||||
{
|
||||
public PurchaseOrder? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePurchaseOrderRequest : IRequest<DeletePurchaseOrderResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeletePurchaseOrderValidator : AbstractValidator<DeletePurchaseOrderRequest>
|
||||
{
|
||||
public DeletePurchaseOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeletePurchaseOrderHandler : IRequestHandler<DeletePurchaseOrderRequest, DeletePurchaseOrderResult>
|
||||
{
|
||||
private readonly ICommandRepository<PurchaseOrder> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeletePurchaseOrderHandler(
|
||||
ICommandRepository<PurchaseOrder> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeletePurchaseOrderResult> Handle(DeletePurchaseOrderRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.DeletedById;
|
||||
|
||||
_repository.Delete(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new DeletePurchaseOrderResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.PurchaseOrderManager.Commands;
|
||||
|
||||
public class UpdatePurchaseOrderResult
|
||||
{
|
||||
public PurchaseOrder? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdatePurchaseOrderRequest : IRequest<UpdatePurchaseOrderResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public DateTime? OrderDate { get; init; }
|
||||
public string? OrderStatus { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? VendorId { get; init; }
|
||||
public string? TaxId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdatePurchaseOrderValidator : AbstractValidator<UpdatePurchaseOrderRequest>
|
||||
{
|
||||
public UpdatePurchaseOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.OrderDate).NotEmpty();
|
||||
RuleFor(x => x.OrderStatus).NotEmpty();
|
||||
RuleFor(x => x.VendorId).NotEmpty();
|
||||
RuleFor(x => x.TaxId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdatePurchaseOrderHandler : IRequestHandler<UpdatePurchaseOrderRequest, UpdatePurchaseOrderResult>
|
||||
{
|
||||
private readonly ICommandRepository<PurchaseOrder> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly PurchaseOrderService _purchaseOrderService;
|
||||
|
||||
public UpdatePurchaseOrderHandler(
|
||||
ICommandRepository<PurchaseOrder> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
PurchaseOrderService purchaseOrderService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_purchaseOrderService = purchaseOrderService;
|
||||
}
|
||||
|
||||
public async Task<UpdatePurchaseOrderResult> Handle(UpdatePurchaseOrderRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.UpdatedById;
|
||||
|
||||
entity.OrderDate = request.OrderDate;
|
||||
entity.OrderStatus = (PurchaseOrderStatus)int.Parse(request.OrderStatus!);
|
||||
entity.Description = request.Description;
|
||||
entity.VendorId = request.VendorId;
|
||||
entity.TaxId = request.TaxId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
_purchaseOrderService.Recalculate(entity.Id);
|
||||
|
||||
return new UpdatePurchaseOrderResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user