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.Grade.Cqrs;
|
||||
|
||||
public class CreateGradeRequest
|
||||
{
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public decimal SalaryFrom { get; set; }
|
||||
public decimal SalaryTo { get; set; }
|
||||
public bool IsOverTimeEligible { get; set; }
|
||||
public string? Status { get; set; } = "Active";
|
||||
}
|
||||
|
||||
public class CreateGradeResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateGradeCommand(CreateGradeRequest Data) : IRequest<CreateGradeResponse>;
|
||||
|
||||
public class CreateGradeHandler : IRequestHandler<CreateGradeCommand, CreateGradeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateGradeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateGradeResponse> Handle(CreateGradeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Grade
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Salary Grade", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.Grade);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Grade
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description,
|
||||
SalaryFrom = request.Data.SalaryFrom,
|
||||
SalaryTo = request.Data.SalaryTo,
|
||||
IsOverTimeEligible = request.Data.IsOverTimeEligible,
|
||||
Status = request.Data.Status
|
||||
};
|
||||
|
||||
_context.Grade.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateGradeResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Grade.Cqrs;
|
||||
|
||||
public class CreateGradeValidator : AbstractValidator<CreateGradeRequest>
|
||||
{
|
||||
public CreateGradeValidator()
|
||||
{
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Grade Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Grade Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.SalaryFrom)
|
||||
.GreaterThanOrEqualTo(0).WithMessage("Salary From must be 0 or greater");
|
||||
|
||||
RuleFor(x => x.SalaryTo)
|
||||
.GreaterThanOrEqualTo(x => x.SalaryFrom).WithMessage("Salary To must be greater than or equal to Salary From");
|
||||
|
||||
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.Grade.Cqrs;
|
||||
|
||||
public record DeleteGradeByIdRequest(string Id);
|
||||
|
||||
public record DeleteGradeByIdCommand(DeleteGradeByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteGradeByIdHandler : IRequestHandler<DeleteGradeByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteGradeByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteGradeByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Grade
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Grade.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.Grade.Cqrs;
|
||||
|
||||
public class GetGradeByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public decimal SalaryFrom { get; set; }
|
||||
public decimal SalaryTo { get; set; }
|
||||
public bool IsOverTimeEligible { 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 GetGradeByIdQuery(string Id) : IRequest<GetGradeByIdResponse?>;
|
||||
|
||||
public class GetGradeByIdHandler : IRequestHandler<GetGradeByIdQuery, GetGradeByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetGradeByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetGradeByIdResponse?> Handle(GetGradeByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Grade
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetGradeByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
SalaryFrom = x.SalaryFrom,
|
||||
SalaryTo = x.SalaryTo,
|
||||
IsOverTimeEligible = x.IsOverTimeEligible,
|
||||
Status = x.Status,
|
||||
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.Payroll.Grade.Cqrs;
|
||||
|
||||
public class GetGradeListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public decimal SalaryFrom { get; set; }
|
||||
public decimal SalaryTo { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool IsOverTimeEligible { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
}
|
||||
|
||||
public record GetGradeListQuery() : IRequest<List<GetGradeListResponse>>;
|
||||
|
||||
public class GetGradeListHandler : IRequestHandler<GetGradeListQuery, List<GetGradeListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetGradeListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetGradeListResponse>> Handle(GetGradeListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Grade
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Code)
|
||||
.Select(x => new GetGradeListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
SalaryFrom = x.SalaryFrom,
|
||||
SalaryTo = x.SalaryTo,
|
||||
Description = x.Description,
|
||||
IsOverTimeEligible = x.IsOverTimeEligible,
|
||||
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.Grade.Cqrs;
|
||||
|
||||
public class UpdateGradeRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public decimal SalaryFrom { get; set; }
|
||||
public decimal SalaryTo { get; set; }
|
||||
public bool IsOverTimeEligible { 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 UpdateGradeResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateGradeCommand(UpdateGradeRequest Data) : IRequest<UpdateGradeResponse>;
|
||||
|
||||
public class UpdateGradeHandler : IRequestHandler<UpdateGradeCommand, UpdateGradeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateGradeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateGradeResponse> Handle(UpdateGradeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Grade
|
||||
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Salary Grade", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Grade
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateGradeResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Code = request.Data.Code;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.SalaryFrom = request.Data.SalaryFrom;
|
||||
entity.SalaryTo = request.Data.SalaryTo;
|
||||
entity.IsOverTimeEligible = request.Data.IsOverTimeEligible;
|
||||
entity.Status = request.Data.Status;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateGradeResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Payroll.Grade.Cqrs;
|
||||
|
||||
public class UpdateGradeValidator : AbstractValidator<UpdateGradeRequest>
|
||||
{
|
||||
public UpdateGradeValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Grade Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Grade Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.SalaryFrom)
|
||||
.GreaterThanOrEqualTo(0).WithMessage("Salary From must be 0 or greater");
|
||||
|
||||
RuleFor(x => x.SalaryTo)
|
||||
.GreaterThanOrEqualTo(x => x.SalaryFrom).WithMessage("Salary To must be greater than or equal to Salary From");
|
||||
|
||||
RuleFor(x => x.Status)
|
||||
.NotEmpty().WithMessage("Status is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user