initial commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Leave.LeaveCategory.Cqrs;
|
||||
|
||||
public class CreateLeaveCategoryRequest
|
||||
{
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public int Quota { get; set; }
|
||||
public bool IsPaidLeave { get; set; }
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string Status { get; set; } = string.Empty;
|
||||
public bool AllowCarryForward { get; set; }
|
||||
public int MaxCarryForward { get; set; }
|
||||
public int MinServiceMonths { get; set; }
|
||||
}
|
||||
|
||||
public class CreateLeaveCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateLeaveCategoryCommand(CreateLeaveCategoryRequest Data) : IRequest<CreateLeaveCategoryResponse>;
|
||||
|
||||
public class CreateLeaveCategoryHandler : IRequestHandler<CreateLeaveCategoryCommand, CreateLeaveCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateLeaveCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateLeaveCategoryResponse> Handle(CreateLeaveCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.LeaveCategory
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Leave Category", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Data.Entities.LeaveCategory);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.LeaveCategory
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
Quota = request.Data.Quota,
|
||||
IsPaidLeave = request.Data.IsPaidLeave,
|
||||
Description = request.Data.Description,
|
||||
Status = request.Data.Status,
|
||||
AllowCarryForward = request.Data.AllowCarryForward,
|
||||
MaxCarryForward = request.Data.MaxCarryForward,
|
||||
MinServiceMonths = request.Data.MinServiceMonths
|
||||
};
|
||||
|
||||
_context.LeaveCategory.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateLeaveCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Leave.LeaveCategory.Cqrs;
|
||||
|
||||
public class CreateLeaveCategoryValidator : AbstractValidator<CreateLeaveCategoryRequest>
|
||||
{
|
||||
public CreateLeaveCategoryValidator()
|
||||
{
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Category Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Category Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Quota)
|
||||
.GreaterThanOrEqualTo(0).WithMessage("Quota cannot be negative");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Leave.LeaveCategory.Cqrs;
|
||||
|
||||
public record DeleteLeaveCategoryByIdRequest(string Id);
|
||||
|
||||
public record DeleteLeaveCategoryByIdCommand(DeleteLeaveCategoryByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteLeaveCategoryByIdHandler : IRequestHandler<DeleteLeaveCategoryByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteLeaveCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteLeaveCategoryByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.LeaveCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.LeaveCategory.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Leave.LeaveCategory.Cqrs;
|
||||
|
||||
public class GetLeaveCategoryByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public int Quota { get; set; }
|
||||
public bool IsPaidLeave { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public bool AllowCarryForward { get; set; }
|
||||
public int MaxCarryForward { get; set; }
|
||||
public int MinServiceMonths { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetLeaveCategoryByIdQuery(string Id) : IRequest<GetLeaveCategoryByIdResponse?>;
|
||||
|
||||
public class GetLeaveCategoryByIdHandler : IRequestHandler<GetLeaveCategoryByIdQuery, GetLeaveCategoryByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetLeaveCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetLeaveCategoryByIdResponse?> Handle(GetLeaveCategoryByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.LeaveCategory
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetLeaveCategoryByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Quota = x.Quota,
|
||||
IsPaidLeave = x.IsPaidLeave,
|
||||
Description = x.Description,
|
||||
Status = x.Status,
|
||||
AllowCarryForward = x.AllowCarryForward,
|
||||
MaxCarryForward = x.MaxCarryForward,
|
||||
MinServiceMonths = x.MinServiceMonths,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Leave.LeaveCategory.Cqrs;
|
||||
|
||||
public class GetLeaveCategoryListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public int Quota { get; set; }
|
||||
public bool IsPaidLeave { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? Status { get; set; }
|
||||
}
|
||||
|
||||
public record GetLeaveCategoryListQuery() : IRequest<List<GetLeaveCategoryListResponse>>;
|
||||
|
||||
public class GetLeaveCategoryListHandler : IRequestHandler<GetLeaveCategoryListQuery, List<GetLeaveCategoryListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetLeaveCategoryListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetLeaveCategoryListResponse>> Handle(GetLeaveCategoryListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.LeaveCategory
|
||||
.AsNoTracking()
|
||||
.NotDeletedOnly()
|
||||
.OrderBy(x => x.Code)
|
||||
.Select(x => new GetLeaveCategoryListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Quota = x.Quota,
|
||||
IsPaidLeave = x.IsPaidLeave,
|
||||
Description = x.Description,
|
||||
Status = x.Status
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Leave.LeaveCategory.Cqrs;
|
||||
|
||||
public class UpdateLeaveCategoryRequest : CreateLeaveCategoryRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateLeaveCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateLeaveCategoryCommand(UpdateLeaveCategoryRequest Data) : IRequest<UpdateLeaveCategoryResponse>;
|
||||
|
||||
public class UpdateLeaveCategoryHandler : IRequestHandler<UpdateLeaveCategoryCommand, UpdateLeaveCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateLeaveCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateLeaveCategoryResponse> Handle(UpdateLeaveCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.LeaveCategory
|
||||
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Leave Category", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.LeaveCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateLeaveCategoryResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Code = request.Data.Code;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Quota = request.Data.Quota;
|
||||
entity.IsPaidLeave = request.Data.IsPaidLeave;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.Status = request.Data.Status;
|
||||
entity.AllowCarryForward = request.Data.AllowCarryForward;
|
||||
entity.MaxCarryForward = request.Data.MaxCarryForward;
|
||||
entity.MinServiceMonths = request.Data.MinServiceMonths;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateLeaveCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Leave.LeaveCategory.Cqrs;
|
||||
|
||||
public class UpdateLeaveCategoryValidator : AbstractValidator<UpdateLeaveCategoryRequest>
|
||||
{
|
||||
public UpdateLeaveCategoryValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Category Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Category Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user