feat: add a day command
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m24s
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m24s
This commit is contained in:
parent
c82b29e90e
commit
2bb7573ca0
12
Application/Cqrs/Day/Commands/CreateDay/CreateDayCommand.cs
Normal file
12
Application/Cqrs/Day/Commands/CreateDay/CreateDayCommand.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using MediatR;
|
||||
using System;
|
||||
|
||||
namespace Mirea.Api.DataAccess.Application.Cqrs.Day.Commands.CreateDay;
|
||||
|
||||
public class CreateDayCommand : IRequest<int>
|
||||
{
|
||||
public required DayOfWeek Index { get; set; }
|
||||
public required int PairNumber { get; set; }
|
||||
public required int LessonId { get; set; }
|
||||
public required int GroupId { get; set; }
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
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.Day.Commands.CreateDay;
|
||||
|
||||
public class CreateDayCommandHandler(IDayDbContext dbContext) : IRequestHandler<CreateDayCommand, int>
|
||||
{
|
||||
public async Task<int> Handle(CreateDayCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await dbContext.Days
|
||||
.FirstOrDefaultAsync(d =>
|
||||
d.LessonId == request.LessonId
|
||||
&& d.PairNumber == request.PairNumber
|
||||
&& d.GroupId == request.GroupId
|
||||
&& d.Index == request.Index,
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
if (entity != null)
|
||||
throw new RecordExistException(typeof(Domain.Schedule.Day), entity.Id);
|
||||
|
||||
var day = new Domain.Schedule.Day()
|
||||
{
|
||||
Index = request.Index,
|
||||
PairNumber = request.PairNumber,
|
||||
GroupId = request.GroupId,
|
||||
LessonId = request.LessonId
|
||||
};
|
||||
|
||||
var result = await dbContext.Days.AddAsync(day, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return result.Entity.Id;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
using MediatR;
|
||||
|
||||
namespace Mirea.Api.DataAccess.Application.Cqrs.Day.Commands.DeleteDay;
|
||||
|
||||
public class DeleteDayCommand : IRequest
|
||||
{
|
||||
public required int Id { get; set; }
|
||||
}
|
@ -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.Day.Commands.DeleteDay;
|
||||
|
||||
public class DeleteDayCommandHandler(ICampusDbContext dbContext) : IRequestHandler<DeleteDayCommand>
|
||||
{
|
||||
public async Task Handle(DeleteDayCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await dbContext.Campuses.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Day), nameof(Domain.Schedule.Day.Id), request.Id);
|
||||
|
||||
dbContext.Campuses.Remove(entity);
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user