57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Performance.Promotion.Cqrs;
|
|
|
|
public class UpdatePromotionRequest : CreatePromotionRequest
|
|
{
|
|
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 UpdatePromotionResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public bool Success { get; set; }
|
|
}
|
|
|
|
public record UpdatePromotionCommand(UpdatePromotionRequest Data) : IRequest<UpdatePromotionResponse>;
|
|
|
|
public class UpdatePromotionHandler : IRequestHandler<UpdatePromotionCommand, UpdatePromotionResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UpdatePromotionHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<UpdatePromotionResponse> Handle(UpdatePromotionCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.Promotion
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
return new UpdatePromotionResponse { Id = request.Data.Id, Success = false };
|
|
}
|
|
|
|
entity.EmployeeId = request.Data.EmployeeId;
|
|
entity.FromGrade = request.Data.FromGrade;
|
|
entity.ToGrade = request.Data.ToGrade;
|
|
entity.EffectiveDate = request.Data.EffectiveDate;
|
|
entity.PromotionNote = request.Data.PromotionNote;
|
|
entity.Status = request.Data.Status;
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new UpdatePromotionResponse
|
|
{
|
|
Id = entity.Id,
|
|
Success = true
|
|
};
|
|
}
|
|
} |