public async Task <Result <CreateUseCase.Response, Domain.Error> > Handle(CreateUseCase.Command request, CancellationToken cancellationToken) { var editions = await _dbContext.Editions.AsNoTracking().ToListAsync(); var dateIntervals = editions.Select(x => ToInterval(x.StartDate, x.EndDate)).ToList(); var requestInterval = ToInterval(request.StartDate, request.EndDate); if (dateIntervals.Any(x => x.Overlaps(requestInterval))) { return(Result.Failure <CreateUseCase.Response, Domain.Error>(new Domain.Error.DomainError("New Edition overlaps old editions"))); } var edition = new Edition() { StartDate = request.StartDate, EndDate = request.EndDate, Name = request.Name }; edition.CumulativeStatistics.CityCount = request.CumulativeStatistics.CityCount; edition.CumulativeStatistics.LecturerCount = request.CumulativeStatistics.LecturerCount; edition.CumulativeStatistics.LessonCount = request.CumulativeStatistics.LessonCount; edition.CumulativeStatistics.SchoolCount = request.CumulativeStatistics.SchoolCount; edition.CumulativeStatistics.StudentCount = request.CumulativeStatistics.StudentCount; _dbContext.Set <Edition>().Add(edition); var rows = await _dbContext.SaveChangesAsync(); return(Result.Success <CreateUseCase.Response, Domain.Error>(new CreateUseCase.Response() { Id = edition.ID })); }
public async Task <IActionResult> Create(CreateUseCase.Command command) { var result = await _engine.Execute(command); return(result.MatchToActionResult(s => Created(Routes.v1.Editions.DetailsFor(s.Id), null))); }