initial commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Organization.Department.Cqrs;
|
||||
|
||||
public class UpdateDepartmentRequest : CreateDepartmentRequest
|
||||
{
|
||||
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 UpdateDepartmentResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateDepartmentCommand(UpdateDepartmentRequest Data) : IRequest<UpdateDepartmentResponse>;
|
||||
|
||||
public class UpdateDepartmentHandler : IRequestHandler<UpdateDepartmentCommand, UpdateDepartmentResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateDepartmentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateDepartmentResponse> Handle(UpdateDepartmentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Department
|
||||
.AnyAsync(x => x.CostCenter == request.Data.CostCenter && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Department", request.Data.CostCenter ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Department
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateDepartmentResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.CostCenter = request.Data.CostCenter;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.HeadOfDeptartment = request.Data.HeadOfDeptartment;
|
||||
entity.OtherInformation1 = request.Data.OtherInformation1;
|
||||
entity.OtherInformation2 = request.Data.OtherInformation2;
|
||||
entity.OtherInformation3 = request.Data.OtherInformation3;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateDepartmentResponse { Id = entity.Id, Success = true };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user