initial commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.EmployeeCategory.Cqrs;
|
||||
|
||||
public class CreateEmployeeCategoryRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateEmployeeCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateEmployeeCategoryCommand(CreateEmployeeCategoryRequest Data) : IRequest<CreateEmployeeCategoryResponse>;
|
||||
|
||||
public class CreateEmployeeCategoryHandler : IRequestHandler<CreateEmployeeCategoryCommand, CreateEmployeeCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateEmployeeCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateEmployeeCategoryResponse> Handle(CreateEmployeeCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.EmployeeCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Employee Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = new Data.Entities.EmployeeCategory
|
||||
{
|
||||
Name = request.Data.Name,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.EmployeeCategory.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateEmployeeCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.EmployeeCategory.Cqrs;
|
||||
|
||||
public class CreateEmployeeCategoryValidator : AbstractValidator<CreateEmployeeCategoryRequest>
|
||||
{
|
||||
public CreateEmployeeCategoryValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Category Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.EmployeeCategory.Cqrs;
|
||||
|
||||
public record DeleteEmployeeCategoryByIdRequest(string Id);
|
||||
|
||||
public record DeleteEmployeeCategoryByIdCommand(DeleteEmployeeCategoryByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteEmployeeCategoryByIdHandler : IRequestHandler<DeleteEmployeeCategoryByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteEmployeeCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteEmployeeCategoryByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.EmployeeCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.EmployeeCategory.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.EmployeeCategory.Cqrs;
|
||||
|
||||
public class GetEmployeeCategoryByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { 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 GetEmployeeCategoryByIdQuery(string Id) : IRequest<GetEmployeeCategoryByIdResponse?>;
|
||||
|
||||
public class GetEmployeeCategoryByIdHandler : IRequestHandler<GetEmployeeCategoryByIdQuery, GetEmployeeCategoryByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetEmployeeCategoryByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetEmployeeCategoryByIdResponse?> Handle(GetEmployeeCategoryByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.EmployeeCategory
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetEmployeeCategoryByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.EmployeeCategory.Cqrs;
|
||||
|
||||
public class GetEmployeeCategoryListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { 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 GetEmployeeCategoryListQuery() : IRequest<List<GetEmployeeCategoryListResponse>>;
|
||||
|
||||
public class GetEmployeeCategoryListHandler : IRequestHandler<GetEmployeeCategoryListQuery, List<GetEmployeeCategoryListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetEmployeeCategoryListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetEmployeeCategoryListResponse>> Handle(GetEmployeeCategoryListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.EmployeeCategory
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetEmployeeCategoryListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.EmployeeCategory.Cqrs;
|
||||
|
||||
public class UpdateEmployeeCategoryRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { 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 UpdateEmployeeCategoryResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateEmployeeCategoryCommand(UpdateEmployeeCategoryRequest Data) : IRequest<UpdateEmployeeCategoryResponse>;
|
||||
|
||||
public class UpdateEmployeeCategoryHandler : IRequestHandler<UpdateEmployeeCategoryCommand, UpdateEmployeeCategoryResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateEmployeeCategoryHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateEmployeeCategoryResponse> Handle(UpdateEmployeeCategoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.EmployeeCategory
|
||||
.AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Employee Category", request.Data.Name ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.EmployeeCategory
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateEmployeeCategoryResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateEmployeeCategoryResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Organization.EmployeeCategory.Cqrs;
|
||||
|
||||
public class UpdateEmployeeCategoryValidator : AbstractValidator<UpdateEmployeeCategoryRequest>
|
||||
{
|
||||
public UpdateEmployeeCategoryValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Category Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(GlobalConsts.StringLengthMedium);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user