Files
blazor-saas-hrm/Features/Organization/Designation/Cqrs/UpdateDesignationHandler.cs
T
2026-07-21 14:22:06 +07:00

62 lines
2.1 KiB
C#

using Indotalent.ConfigBackEnd.Exceptions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Organization.Designation.Cqrs;
public class UpdateDesignationRequest : CreateDesignationRequest
{
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 UpdateDesignationResponse
{
public string? Id { get; set; }
public bool Success { get; set; }
}
public record UpdateDesignationCommand(UpdateDesignationRequest Data) : IRequest<UpdateDesignationResponse>;
public class UpdateDesignationHandler : IRequestHandler<UpdateDesignationCommand, UpdateDesignationResponse>
{
private readonly AppDbContext _context;
public UpdateDesignationHandler(AppDbContext context) => _context = context;
public async Task<UpdateDesignationResponse> Handle(UpdateDesignationCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Designation
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
if (entity == null) return new UpdateDesignationResponse { Id = request.Data.Id, Success = false };
var isExists = await _context.Designation
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
if (isExists)
{
throw new AlreadyExistsException("Designation", request.Data.Code ?? string.Empty);
}
entity.Code = request.Data.Code;
entity.Name = request.Data.Name;
entity.Description = request.Data.Description;
entity.Level = request.Data.Level;
entity.OtherInformation1 = request.Data.OtherInformation1;
entity.OtherInformation2 = request.Data.OtherInformation2;
entity.OtherInformation3 = request.Data.OtherInformation3;
await _context.SaveChangesAsync(cancellationToken);
return new UpdateDesignationResponse
{
Id = entity.Id,
Success = true
};
}
}