initial commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.ExpenseManager.Commands;
|
||||
|
||||
public class CreateExpenseResult
|
||||
{
|
||||
public Expense? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateExpenseRequest : IRequest<CreateExpenseResult>
|
||||
{
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? ExpenseDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public double? Amount { get; init; }
|
||||
public string? CampaignId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateExpenseValidator : AbstractValidator<CreateExpenseRequest>
|
||||
{
|
||||
public CreateExpenseValidator()
|
||||
{
|
||||
RuleFor(x => x.Title).NotEmpty();
|
||||
RuleFor(x => x.ExpenseDate).NotNull();
|
||||
RuleFor(x => x.Amount).NotNull();
|
||||
RuleFor(x => x.CampaignId).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateExpenseHandler : IRequestHandler<CreateExpenseRequest, CreateExpenseResult>
|
||||
{
|
||||
private readonly ICommandRepository<Expense> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
|
||||
public CreateExpenseHandler(
|
||||
ICommandRepository<Expense> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
}
|
||||
|
||||
public async Task<CreateExpenseResult> Handle(CreateExpenseRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Expense
|
||||
{
|
||||
CreatedById = request.CreatedById,
|
||||
Number = _numberSequenceService.GenerateNumber(nameof(Expense), "", "EXP"),
|
||||
Title = request.Title,
|
||||
Description = request.Description,
|
||||
ExpenseDate = request.ExpenseDate,
|
||||
Status = (ExpenseStatus)int.Parse(request.Status!),
|
||||
Amount = request.Amount,
|
||||
CampaignId = request.CampaignId
|
||||
};
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateExpenseResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.ExpenseManager.Commands;
|
||||
|
||||
public class DeleteExpenseResult
|
||||
{
|
||||
public Expense? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteExpenseRequest : IRequest<DeleteExpenseResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteExpenseValidator : AbstractValidator<DeleteExpenseRequest>
|
||||
{
|
||||
public DeleteExpenseValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteExpenseHandler : IRequestHandler<DeleteExpenseRequest, DeleteExpenseResult>
|
||||
{
|
||||
private readonly ICommandRepository<Expense> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteExpenseHandler(
|
||||
ICommandRepository<Expense> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteExpenseResult> Handle(DeleteExpenseRequest 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 DeleteExpenseResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.ExpenseManager.Commands;
|
||||
|
||||
public class UpdateExpenseResult
|
||||
{
|
||||
public Expense? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateExpenseRequest : IRequest<UpdateExpenseResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? ExpenseDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public double? Amount { get; init; }
|
||||
public string? CampaignId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateExpenseValidator : AbstractValidator<UpdateExpenseRequest>
|
||||
{
|
||||
public UpdateExpenseValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Title).NotEmpty();
|
||||
RuleFor(x => x.ExpenseDate).NotNull();
|
||||
RuleFor(x => x.Amount).NotNull();
|
||||
RuleFor(x => x.CampaignId).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateExpenseHandler : IRequestHandler<UpdateExpenseRequest, UpdateExpenseResult>
|
||||
{
|
||||
private readonly ICommandRepository<Expense> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateExpenseHandler(
|
||||
ICommandRepository<Expense> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateExpenseResult> Handle(UpdateExpenseRequest 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.Title = request.Title;
|
||||
entity.Description = request.Description;
|
||||
entity.ExpenseDate = request.ExpenseDate;
|
||||
entity.Status = (ExpenseStatus)int.Parse(request.Status!);
|
||||
entity.Amount = request.Amount;
|
||||
entity.CampaignId = request.CampaignId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateExpenseResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user