initial commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
||||
|
||||
public class CreateTaxRequest
|
||||
{
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public decimal PercentageValue { get; set; }
|
||||
public string? Category { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateTaxResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateTaxCommand(CreateTaxRequest Data) : IRequest<CreateTaxResponse>;
|
||||
|
||||
public class CreateTaxHandler : IRequestHandler<CreateTaxCommand, CreateTaxResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateTaxHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateTaxResponse> Handle(CreateTaxCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Tax
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Tax", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Tax);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Tax
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
PercentageValue = request.Data.PercentageValue,
|
||||
Category = request.Data.Category,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.Tax.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateTaxResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
||||
|
||||
public class CreateTaxValidator : AbstractValidator<CreateTaxRequest>
|
||||
{
|
||||
public CreateTaxValidator()
|
||||
{
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Tax Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Tax Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Category)
|
||||
.NotEmpty().WithMessage("Category is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
||||
|
||||
public record DeleteTaxByIdRequest(string Id);
|
||||
|
||||
public class DeleteTaxByIdHandler : IRequestHandler<DeleteTaxByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteTaxByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteTaxByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Tax
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Tax.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
public record DeleteTaxByIdCommand(DeleteTaxByIdRequest Data) : IRequest<bool>;
|
||||
@@ -0,0 +1,49 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
||||
|
||||
public class GetTaxByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public decimal PercentageValue { get; set; }
|
||||
public string? Category { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetTaxByIdQuery(string Id) : IRequest<GetTaxByIdResponse?>;
|
||||
|
||||
public class GetTaxByIdHandler : IRequestHandler<GetTaxByIdQuery, GetTaxByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetTaxByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetTaxByIdResponse?> Handle(GetTaxByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Tax
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetTaxByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
PercentageValue = x.PercentageValue,
|
||||
Category = x.Category,
|
||||
Description = x.Description,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
||||
|
||||
public class GetTaxListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public decimal PercentageValue { get; set; }
|
||||
public string? Category { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetTaxListQuery() : IRequest<List<GetTaxListResponse>>;
|
||||
|
||||
public class GetTaxListHandler : IRequestHandler<GetTaxListQuery, List<GetTaxListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetTaxListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetTaxListResponse>> Handle(GetTaxListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Tax
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Code)
|
||||
.Select(x => new GetTaxListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
PercentageValue = x.PercentageValue,
|
||||
Category = x.Category,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
||||
|
||||
public class UpdateTaxRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public decimal PercentageValue { get; set; }
|
||||
public string? Category { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTaxResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateTaxCommand(UpdateTaxRequest Data) : IRequest<UpdateTaxResponse>;
|
||||
|
||||
public class UpdateTaxHandler : IRequestHandler<UpdateTaxCommand, UpdateTaxResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateTaxHandler(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<UpdateTaxResponse> Handle(UpdateTaxCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Tax
|
||||
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Tax", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Tax
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateTaxResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Code = request.Data.Code;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.PercentageValue = request.Data.PercentageValue;
|
||||
entity.Category = request.Data.Category;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateTaxResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Setting.Tax.Cqrs;
|
||||
|
||||
public class UpdateTaxValidator : AbstractValidator<UpdateTaxRequest>
|
||||
{
|
||||
public UpdateTaxValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Tax Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Tax Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Category)
|
||||
.NotEmpty().WithMessage("Category is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user