feat: add a faculty command
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m19s

This commit is contained in:
Polianin Nikita 2024-01-25 17:39:07 +03:00
parent b9b34a29e8
commit 4fe25005af
4 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,9 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Commands.CreateFaculty;
public class CreateFacultyCommand : IRequest<int>
{
public required string Name { get; set; }
public int? CampusId { get; set; }
}

View File

@ -0,0 +1,30 @@
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.Faculty.Commands.CreateFaculty;
public class CreateFacultyCommandHandler(IFacultyDbContext dbContext) : IRequestHandler<CreateFacultyCommand, int>
{
public async Task<int> Handle(CreateFacultyCommand request, CancellationToken cancellationToken)
{
var entity = await dbContext.Faculties.FirstOrDefaultAsync(f => f.Name == request.Name, cancellationToken: cancellationToken);
if (entity != null)
throw new RecordExistException(typeof(Domain.Schedule.Faculty), nameof(Domain.Schedule.Faculty.Name), entity.Id);
var faculty = new Domain.Schedule.Faculty()
{
Name = request.Name,
CampusId = request.CampusId
};
var result = await dbContext.Faculties.AddAsync(faculty, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
return result.Entity.Id;
}
}

View File

@ -0,0 +1,9 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Commands.UpdateFaculty;
public class UpdateFacultyCommand : IRequest
{
public required int Id { get; set; }
public required int CampusId { get; set; }
}

View File

@ -0,0 +1,20 @@
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.Faculty.Commands.UpdateFaculty;
public class UpdateFacultyCommandHandler(IFacultyDbContext dbContext) : IRequestHandler<UpdateFacultyCommand>
{
public async Task Handle(UpdateFacultyCommand request, CancellationToken cancellationToken)
{
var entity = await dbContext.Faculties.FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Faculty), nameof(Domain.Schedule.Faculty.Id), request.Id);
entity.CampusId = request.CampusId;
await dbContext.SaveChangesAsync(cancellationToken);
}
}