initial commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Performance.Appraisal.Cqrs;
|
||||
|
||||
public class CreateAppraisalRequest
|
||||
{
|
||||
public string EmployeeId { get; set; } = string.Empty;
|
||||
public string LastRating { get; set; } = string.Empty;
|
||||
public decimal CurrentSalary { get; set; }
|
||||
public decimal IncrementPercentage { get; set; }
|
||||
public decimal NewSalary { get; set; }
|
||||
public string AppraisalType { get; set; } = string.Empty;
|
||||
public DateTime EffectiveDate { get; set; }
|
||||
public string AppraisalNote { get; set; } = string.Empty;
|
||||
public string Status { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CreateAppraisalResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateAppraisalCommand(CreateAppraisalRequest Data) : IRequest<CreateAppraisalResponse>;
|
||||
|
||||
public class CreateAppraisalHandler : IRequestHandler<CreateAppraisalCommand, CreateAppraisalResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateAppraisalHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateAppraisalResponse> Handle(CreateAppraisalCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.Appraisal);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Appraisal
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
EmployeeId = request.Data.EmployeeId,
|
||||
LastRating = request.Data.LastRating,
|
||||
CurrentSalary = request.Data.CurrentSalary,
|
||||
IncrementPercentage = request.Data.IncrementPercentage,
|
||||
NewSalary = request.Data.NewSalary,
|
||||
AppraisalType = request.Data.AppraisalType,
|
||||
EffectiveDate = request.Data.EffectiveDate,
|
||||
AppraisalNote = request.Data.AppraisalNote,
|
||||
Status = request.Data.Status
|
||||
};
|
||||
|
||||
_context.Appraisal.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateAppraisalResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Performance.Appraisal.Cqrs;
|
||||
|
||||
public class CreateAppraisalValidator : AbstractValidator<CreateAppraisalRequest>
|
||||
{
|
||||
public CreateAppraisalValidator()
|
||||
{
|
||||
RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("Employee is required");
|
||||
RuleFor(x => x.AppraisalType).NotEmpty().WithMessage("Appraisal Type is required");
|
||||
RuleFor(x => x.EffectiveDate).NotEmpty().WithMessage("Effective Date is required");
|
||||
RuleFor(x => x.CurrentSalary).GreaterThanOrEqualTo(0);
|
||||
RuleFor(x => x.IncrementPercentage).GreaterThanOrEqualTo(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Appraisal.Cqrs;
|
||||
|
||||
public record DeleteAppraisalByIdRequest(string Id);
|
||||
|
||||
public record DeleteAppraisalByIdCommand(DeleteAppraisalByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteAppraisalByIdHandler : IRequestHandler<DeleteAppraisalByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteAppraisalByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteAppraisalByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Appraisal
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Appraisal.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Appraisal.Cqrs;
|
||||
|
||||
public class GetAppraisalByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? EmployeeId { get; set; }
|
||||
public string? EmployeeCode { get; set; }
|
||||
public string? EmployeeName { get; set; }
|
||||
public string? LastRating { get; set; }
|
||||
public decimal CurrentSalary { get; set; }
|
||||
public decimal IncrementPercentage { get; set; }
|
||||
public decimal NewSalary { get; set; }
|
||||
public string? AppraisalType { get; set; }
|
||||
public DateTime EffectiveDate { get; set; }
|
||||
public string? AppraisalNote { 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 GetAppraisalByIdQuery(string Id) : IRequest<GetAppraisalByIdResponse?>;
|
||||
|
||||
public class GetAppraisalByIdHandler : IRequestHandler<GetAppraisalByIdQuery, GetAppraisalByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetAppraisalByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetAppraisalByIdResponse?> Handle(GetAppraisalByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Appraisal
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Employee)
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetAppraisalByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
EmployeeId = x.EmployeeId,
|
||||
EmployeeCode = x.Employee != null ? x.Employee.Code : string.Empty,
|
||||
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
|
||||
LastRating = x.LastRating,
|
||||
CurrentSalary = x.CurrentSalary,
|
||||
IncrementPercentage = x.IncrementPercentage,
|
||||
NewSalary = x.NewSalary,
|
||||
AppraisalType = x.AppraisalType,
|
||||
EffectiveDate = x.EffectiveDate,
|
||||
AppraisalNote = x.AppraisalNote,
|
||||
Status = x.Status,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Appraisal.Cqrs;
|
||||
|
||||
public class GetAppraisalListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? EmployeeId { get; set; }
|
||||
public string? EmployeeCode { get; set; }
|
||||
public string? EmployeeName { get; set; }
|
||||
public string? LastRating { get; set; }
|
||||
public decimal CurrentSalary { get; set; }
|
||||
public decimal IncrementPercentage { get; set; }
|
||||
public decimal NewSalary { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public DateTime EffectiveDate { get; set; }
|
||||
}
|
||||
|
||||
public record GetAppraisalListQuery() : IRequest<List<GetAppraisalListResponse>>;
|
||||
|
||||
public class GetAppraisalListHandler : IRequestHandler<GetAppraisalListQuery, List<GetAppraisalListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetAppraisalListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetAppraisalListResponse>> Handle(GetAppraisalListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Appraisal
|
||||
.AsNoTracking()
|
||||
.NotDeletedOnly()
|
||||
.Include(x => x.Employee)
|
||||
.OrderByDescending(x => x.EffectiveDate)
|
||||
.Select(x => new GetAppraisalListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
EmployeeId = x.EmployeeId,
|
||||
EmployeeCode = x.Employee != null ? x.Employee.Code : string.Empty,
|
||||
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
|
||||
LastRating = x.LastRating,
|
||||
CurrentSalary = x.CurrentSalary,
|
||||
IncrementPercentage = x.IncrementPercentage,
|
||||
NewSalary = x.NewSalary,
|
||||
Status = x.Status,
|
||||
EffectiveDate = x.EffectiveDate
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Appraisal.Cqrs;
|
||||
|
||||
public class UpdateAppraisalRequest : CreateAppraisalRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? EmployeeCode { get; set; }
|
||||
public string? EmployeeName { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateAppraisalResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateAppraisalCommand(UpdateAppraisalRequest Data) : IRequest<UpdateAppraisalResponse>;
|
||||
|
||||
public class UpdateAppraisalHandler : IRequestHandler<UpdateAppraisalCommand, UpdateAppraisalResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateAppraisalHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateAppraisalResponse> Handle(UpdateAppraisalCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Appraisal
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateAppraisalResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.EmployeeId = request.Data.EmployeeId;
|
||||
entity.LastRating = request.Data.LastRating;
|
||||
entity.CurrentSalary = request.Data.CurrentSalary;
|
||||
entity.IncrementPercentage = request.Data.IncrementPercentage;
|
||||
entity.NewSalary = request.Data.NewSalary;
|
||||
entity.AppraisalType = request.Data.AppraisalType;
|
||||
entity.EffectiveDate = request.Data.EffectiveDate;
|
||||
entity.AppraisalNote = request.Data.AppraisalNote;
|
||||
entity.Status = request.Data.Status;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateAppraisalResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Performance.Appraisal.Cqrs;
|
||||
|
||||
public class UpdateAppraisalValidator : AbstractValidator<UpdateAppraisalRequest>
|
||||
{
|
||||
public UpdateAppraisalValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty().WithMessage("ID is required for update");
|
||||
RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("Employee is required");
|
||||
RuleFor(x => x.AppraisalType).NotEmpty().WithMessage("Appraisal Type is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user