65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Leave.LeaveRequest.Cqrs;
|
|
|
|
public class UpdateLeaveRequestRequest : CreateLeaveRequestRequest
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? EmployeeName { get; set; }
|
|
public string? LeaveType { get; set; }
|
|
public DateTimeOffset? CreatedAt { get; set; }
|
|
public string? CreatedBy { get; set; }
|
|
public DateTimeOffset? UpdatedAt { get; set; }
|
|
public string? UpdatedBy { get; set; }
|
|
public string? RejectReason { get; set; }
|
|
public string? ApproverId { get; set; }
|
|
public DateTime? ActionDate { get; set; }
|
|
}
|
|
|
|
public class UpdateLeaveRequestResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public bool Success { get; set; }
|
|
}
|
|
|
|
public record UpdateLeaveRequestCommand(UpdateLeaveRequestRequest Data) : IRequest<UpdateLeaveRequestResponse>;
|
|
|
|
public class UpdateLeaveRequestHandler : IRequestHandler<UpdateLeaveRequestCommand, UpdateLeaveRequestResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UpdateLeaveRequestHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<UpdateLeaveRequestResponse> Handle(UpdateLeaveRequestCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.LeaveRequest
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
return new UpdateLeaveRequestResponse { Id = request.Data.Id, Success = false };
|
|
}
|
|
|
|
entity.EmployeeId = request.Data.EmployeeId;
|
|
entity.LeaveCategoryId = request.Data.LeaveCategoryId;
|
|
entity.StartDate = request.Data.StartDate;
|
|
entity.EndDate = request.Data.EndDate;
|
|
entity.Days = request.Data.Days;
|
|
entity.Reason = request.Data.Reason;
|
|
entity.Status = request.Data.Status;
|
|
entity.AttachmentPath = request.Data.AttachmentPath;
|
|
entity.RejectReason = request.Data.RejectReason;
|
|
entity.ApproverId = request.Data.ApproverId;
|
|
entity.ActionDate = request.Data.ActionDate;
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new UpdateLeaveRequestResponse
|
|
{
|
|
Id = entity.Id,
|
|
Success = true
|
|
};
|
|
}
|
|
} |