initial commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public class CreateTransferRequest
|
||||
{
|
||||
public string EmployeeId { get; set; } = string.Empty;
|
||||
public string FromBranchId { get; set; } = string.Empty;
|
||||
public string FromDepartmentId { get; set; } = string.Empty;
|
||||
public string FromDesignationId { get; set; } = string.Empty;
|
||||
public string ToBranchId { get; set; } = string.Empty;
|
||||
public string ToDepartmentId { get; set; } = string.Empty;
|
||||
public string ToDesignationId { get; set; } = string.Empty;
|
||||
public DateTime? TransferDate { get; set; }
|
||||
public string TransferNote { get; set; } = string.Empty;
|
||||
public string Status { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CreateTransferResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateTransferCommand(CreateTransferRequest Data) : IRequest<CreateTransferResponse>;
|
||||
|
||||
public class CreateTransferHandler : IRequestHandler<CreateTransferCommand, CreateTransferResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateTransferHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateTransferResponse> Handle(CreateTransferCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.Transfer);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"TRF/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Transfer
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
EmployeeId = request.Data.EmployeeId,
|
||||
FromBranchId = request.Data.FromBranchId,
|
||||
FromDepartmentId = request.Data.FromDepartmentId,
|
||||
FromDesignationId = request.Data.FromDesignationId,
|
||||
ToBranchId = request.Data.ToBranchId,
|
||||
ToDepartmentId = request.Data.ToDepartmentId,
|
||||
ToDesignationId = request.Data.ToDesignationId,
|
||||
TransferDate = request.Data.TransferDate,
|
||||
TransferNote = request.Data.TransferNote,
|
||||
Status = request.Data.Status
|
||||
};
|
||||
|
||||
_context.Transfer.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateTransferResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public class CreateTransferValidator : AbstractValidator<CreateTransferRequest>
|
||||
{
|
||||
public CreateTransferValidator()
|
||||
{
|
||||
RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("Employee is required");
|
||||
RuleFor(x => x.ToBranchId).NotEmpty().WithMessage("Destination Branch is required");
|
||||
RuleFor(x => x.ToDepartmentId).NotEmpty().WithMessage("Destination Department is required");
|
||||
RuleFor(x => x.TransferDate).NotEmpty().WithMessage("Transfer Date is required");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public record DeleteTransferByIdRequest(string Id);
|
||||
|
||||
public record DeleteTransferByIdCommand(DeleteTransferByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteTransferByIdHandler : IRequestHandler<DeleteTransferByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteTransferByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteTransferByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Transfer
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Transfer.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public class GetTransferByIdResponse : UpdateTransferRequest { }
|
||||
|
||||
public record GetTransferByIdQuery(string Id) : IRequest<GetTransferByIdResponse?>;
|
||||
|
||||
public class GetTransferByIdHandler : IRequestHandler<GetTransferByIdQuery, GetTransferByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetTransferByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetTransferByIdResponse?> Handle(GetTransferByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Transfer
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Employee)
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetTransferByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
EmployeeId = x.EmployeeId,
|
||||
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
|
||||
FromBranchId = x.FromBranchId,
|
||||
FromDepartmentId = x.FromDepartmentId,
|
||||
FromDesignationId = x.FromDesignationId,
|
||||
ToBranchId = x.ToBranchId,
|
||||
ToDepartmentId = x.ToDepartmentId,
|
||||
ToDesignationId = x.ToDesignationId,
|
||||
TransferDate = x.TransferDate,
|
||||
TransferNote = x.TransferNote,
|
||||
Status = x.Status,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public class GetTransferListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? EmployeeId { get; set; }
|
||||
public string? EmployeeName { get; set; }
|
||||
public string? FromDept { get; set; }
|
||||
public string? FromLocation { get; set; }
|
||||
public string? ToDept { get; set; }
|
||||
public string? ToLocation { get; set; }
|
||||
public DateTime? TransferDate { get; set; }
|
||||
public string? Status { get; set; }
|
||||
}
|
||||
|
||||
public record GetTransferListQuery() : IRequest<List<GetTransferListResponse>>;
|
||||
|
||||
public class GetTransferListHandler : IRequestHandler<GetTransferListQuery, List<GetTransferListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetTransferListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetTransferListResponse>> Handle(GetTransferListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Transfer
|
||||
.AsNoTracking()
|
||||
.NotDeletedOnly()
|
||||
.Include(x => x.Employee)
|
||||
.Include(x => x.FromDepartment)
|
||||
.Include(x => x.ToDepartment)
|
||||
.Include(x => x.FromBranch)
|
||||
.Include(x => x.ToBranch)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetTransferListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
EmployeeId = x.EmployeeId,
|
||||
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
|
||||
FromDept = x.FromDepartment != null ? x.FromDepartment.Name : string.Empty,
|
||||
FromLocation = x.FromBranch != null ? x.FromBranch.Name : string.Empty,
|
||||
ToDept = x.ToDepartment != null ? x.ToDepartment.Name : string.Empty,
|
||||
ToLocation = x.ToBranch != null ? x.ToBranch.Name : string.Empty,
|
||||
TransferDate = x.TransferDate,
|
||||
Status = x.Status
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public class LookupResponse
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public record GetBranchLookupQuery() : IRequest<List<LookupResponse>>;
|
||||
public record GetDepartmentLookupQuery() : IRequest<List<LookupResponse>>;
|
||||
public record GetDesignationLookupQuery() : IRequest<List<LookupResponse>>;
|
||||
|
||||
public class GetTransferLookupsHandler :
|
||||
IRequestHandler<GetBranchLookupQuery, List<LookupResponse>>,
|
||||
IRequestHandler<GetDepartmentLookupQuery, List<LookupResponse>>,
|
||||
IRequestHandler<GetDesignationLookupQuery, List<LookupResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetTransferLookupsHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<LookupResponse>> Handle(GetBranchLookupQuery request, CancellationToken ct)
|
||||
{
|
||||
return await _context.Branch.AsNoTracking().NotDeletedOnly()
|
||||
.Select(x => new LookupResponse { Id = x.Id, Name = x.Name ?? "" }).ToListAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<List<LookupResponse>> Handle(GetDepartmentLookupQuery request, CancellationToken ct)
|
||||
{
|
||||
return await _context.Department.AsNoTracking().NotDeletedOnly()
|
||||
.Select(x => new LookupResponse { Id = x.Id, Name = x.Name ?? "" }).ToListAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<List<LookupResponse>> Handle(GetDesignationLookupQuery request, CancellationToken ct)
|
||||
{
|
||||
return await _context.Designation.AsNoTracking().NotDeletedOnly()
|
||||
.Select(x => new LookupResponse { Id = x.Id, Name = x.Name ?? "" }).ToListAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public class UpdateTransferRequest : CreateTransferRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { 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 UpdateTransferResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateTransferCommand(UpdateTransferRequest Data) : IRequest<UpdateTransferResponse>;
|
||||
|
||||
public class UpdateTransferHandler : IRequestHandler<UpdateTransferCommand, UpdateTransferResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateTransferHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateTransferResponse> Handle(UpdateTransferCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Transfer
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return new UpdateTransferResponse { Success = false };
|
||||
|
||||
entity.EmployeeId = request.Data.EmployeeId;
|
||||
entity.FromBranchId = request.Data.FromBranchId;
|
||||
entity.FromDepartmentId = request.Data.FromDepartmentId;
|
||||
entity.FromDesignationId = request.Data.FromDesignationId;
|
||||
entity.ToBranchId = request.Data.ToBranchId;
|
||||
entity.ToDepartmentId = request.Data.ToDepartmentId;
|
||||
entity.ToDesignationId = request.Data.ToDesignationId;
|
||||
entity.TransferDate = request.Data.TransferDate;
|
||||
entity.TransferNote = request.Data.TransferNote;
|
||||
entity.Status = request.Data.Status;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateTransferResponse { Id = entity.Id, Success = true };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Features.Performance.Transfer.Cqrs;
|
||||
|
||||
public class UpdateTransferValidator : AbstractValidator<UpdateTransferRequest>
|
||||
{
|
||||
public UpdateTransferValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty().WithMessage("ID is required for update");
|
||||
RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("Employee is required");
|
||||
RuleFor(x => x.ToBranchId).NotEmpty().WithMessage("Destination Branch is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user