initial commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TaxManager.Commands;
|
||||
|
||||
public class CreateTaxResult
|
||||
{
|
||||
public Tax? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateTaxRequest : IRequest<CreateTaxResult>
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public double? Percentage { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateTaxValidator : AbstractValidator<CreateTaxRequest>
|
||||
{
|
||||
public CreateTaxValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.Percentage).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateTaxHandler : IRequestHandler<CreateTaxRequest, CreateTaxResult>
|
||||
{
|
||||
private readonly ICommandRepository<Tax> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public CreateTaxHandler(
|
||||
ICommandRepository<Tax> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<CreateTaxResult> Handle(CreateTaxRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Tax();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Percentage = request.Percentage;
|
||||
entity.Description = request.Description;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateTaxResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TaxManager.Commands;
|
||||
|
||||
public class DeleteTaxResult
|
||||
{
|
||||
public Tax? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteTaxRequest : IRequest<DeleteTaxResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteTaxValidator : AbstractValidator<DeleteTaxRequest>
|
||||
{
|
||||
public DeleteTaxValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteTaxHandler : IRequestHandler<DeleteTaxRequest, DeleteTaxResult>
|
||||
{
|
||||
private readonly ICommandRepository<Tax> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteTaxHandler(
|
||||
ICommandRepository<Tax> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteTaxResult> Handle(DeleteTaxRequest 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 DeleteTaxResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TaxManager.Commands;
|
||||
|
||||
public class UpdateTaxResult
|
||||
{
|
||||
public Tax? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTaxRequest : IRequest<UpdateTaxResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public double? Percentage { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateTaxValidator : AbstractValidator<UpdateTaxRequest>
|
||||
{
|
||||
public UpdateTaxValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.Percentage).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateTaxHandler : IRequestHandler<UpdateTaxRequest, UpdateTaxResult>
|
||||
{
|
||||
private readonly ICommandRepository<Tax> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateTaxHandler(
|
||||
ICommandRepository<Tax> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateTaxResult> Handle(UpdateTaxRequest 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.Name = request.Name;
|
||||
entity.Percentage = request.Percentage;
|
||||
entity.Description = request.Description;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateTaxResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.TaxManager.Queries;
|
||||
|
||||
public record GetTaxListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public double? Percentage { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetTaxListProfile : Profile
|
||||
{
|
||||
public GetTaxListProfile()
|
||||
{
|
||||
CreateMap<Tax, GetTaxListDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetTaxListResult
|
||||
{
|
||||
public List<GetTaxListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetTaxListRequest : IRequest<GetTaxListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetTaxListHandler : IRequestHandler<GetTaxListRequest, GetTaxListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetTaxListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetTaxListResult> Handle(GetTaxListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Tax
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetTaxListDto>>(entities);
|
||||
|
||||
return new GetTaxListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user