initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
||||
|
||||
public class CreateIncomeRequest
|
||||
{
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public bool IsTaxable { get; set; } = true;
|
||||
public string? CalculationBase { get; set; }
|
||||
public string? Status { get; set; } = "Active";
|
||||
}
|
||||
|
||||
public class CreateIncomeResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateIncomeCommand(CreateIncomeRequest Data) : IRequest<CreateIncomeResponse>;
|
||||
|
||||
public class CreateIncomeHandler : IRequestHandler<CreateIncomeCommand, CreateIncomeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateIncomeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateIncomeResponse> Handle(CreateIncomeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Income
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Income Component", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Income);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Income
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
Type = request.Data.Type,
|
||||
IsTaxable = request.Data.IsTaxable,
|
||||
CalculationBase = request.Data.CalculationBase,
|
||||
Status = request.Data.Status
|
||||
};
|
||||
|
||||
_context.Income.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateIncomeResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
||||
|
||||
public class CreateIncomeValidator : AbstractValidator<CreateIncomeRequest>
|
||||
{
|
||||
public CreateIncomeValidator()
|
||||
{
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Type)
|
||||
.NotEmpty().WithMessage("Type is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CalculationBase)
|
||||
.NotEmpty().WithMessage("Calculation Base is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Status)
|
||||
.NotEmpty().WithMessage("Status is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
||||
|
||||
public record DeleteIncomeByIdRequest(string Id);
|
||||
|
||||
public record DeleteIncomeByIdCommand(DeleteIncomeByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteIncomeByIdHandler : IRequestHandler<DeleteIncomeByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteIncomeByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteIncomeByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Income
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Income.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
||||
|
||||
public class GetIncomeByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public bool IsTaxable { get; set; }
|
||||
public string? CalculationBase { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetIncomeByIdQuery(string Id) : IRequest<GetIncomeByIdResponse?>;
|
||||
|
||||
public class GetIncomeByIdHandler : IRequestHandler<GetIncomeByIdQuery, GetIncomeByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetIncomeByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetIncomeByIdResponse?> Handle(GetIncomeByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Income
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetIncomeByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
Type = x.Type,
|
||||
IsTaxable = x.IsTaxable,
|
||||
CalculationBase = x.CalculationBase,
|
||||
Status = x.Status,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
||||
|
||||
public class GetIncomeListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public bool IsTaxable { get; set; }
|
||||
public string? CalculationBase { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
}
|
||||
|
||||
public record GetIncomeListQuery() : IRequest<List<GetIncomeListResponse>>;
|
||||
|
||||
public class GetIncomeListHandler : IRequestHandler<GetIncomeListQuery, List<GetIncomeListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetIncomeListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetIncomeListResponse>> Handle(GetIncomeListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Income
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Code)
|
||||
.Select(x => new GetIncomeListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Type = x.Type,
|
||||
IsTaxable = x.IsTaxable,
|
||||
CalculationBase = x.CalculationBase,
|
||||
Status = x.Status,
|
||||
CreatedAt = x.CreatedAt
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
||||
|
||||
public class UpdateIncomeRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public bool IsTaxable { get; set; }
|
||||
public string? CalculationBase { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateIncomeResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateIncomeCommand(UpdateIncomeRequest Data) : IRequest<UpdateIncomeResponse>;
|
||||
|
||||
public class UpdateIncomeHandler : IRequestHandler<UpdateIncomeCommand, UpdateIncomeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateIncomeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateIncomeResponse> Handle(UpdateIncomeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Income
|
||||
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Income Component", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Income
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateIncomeResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Code = request.Data.Code;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.Type = request.Data.Type;
|
||||
entity.IsTaxable = request.Data.IsTaxable;
|
||||
entity.CalculationBase = request.Data.CalculationBase;
|
||||
entity.Status = request.Data.Status;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateIncomeResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Income.Cqrs;
|
||||
|
||||
public class UpdateIncomeValidator : AbstractValidator<UpdateIncomeRequest>
|
||||
{
|
||||
public UpdateIncomeValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Type)
|
||||
.NotEmpty().WithMessage("Type is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CalculationBase)
|
||||
.NotEmpty().WithMessage("Calculation Base is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Status)
|
||||
.NotEmpty().WithMessage("Status is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user