MireaBackend/Application/Cqrs/Campus/Commands/UpdateCampus/UpdateCampusCommandHandler.cs

23 lines
1.0 KiB
C#
Raw Normal View History

2024-01-25 17:44:44 +03:00
using MediatR;
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Common.Exceptions;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using System.Threading;
using System.Threading.Tasks;
namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Commands.UpdateCampus;
public class UpdateCampusCommandHandler(ICampusDbContext dbContext) : IRequestHandler<UpdateCampusCommand>
{
public async Task Handle(UpdateCampusCommand request, CancellationToken cancellationToken)
{
var entity = await dbContext.Campuses.FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Campus), nameof(Domain.Schedule.Campus.Id), request.Id);
if (!string.IsNullOrEmpty(request.FullName))
entity.FullName = request.FullName;
if (!string.IsNullOrEmpty(request.Address))
entity.Address = request.Address;
await dbContext.SaveChangesAsync(cancellationToken);
}
}