public static ActionResult ValidationProblem(this ControllerBase controller, ValidationFailureCommandResult commandResult) { if (commandResult.ValidationErrors.Count > 0) { var dictionary = new ModelStateDictionary(); foreach (var(key, errors) in commandResult.ValidationErrors) { foreach (var error in errors) { dictionary.AddModelError(key, error); } } return(controller.ValidationProblem(commandResult.Reason, modelStateDictionary: dictionary)); } return(controller.Problem(commandResult.Reason, statusCode: StatusCodes.Status400BadRequest)); }
public async Task <(ICommandResult result, IReadOnlyList <IDomainEvent> events)> ExecuteAsync(ScheduleSession command, CancellationToken cancellationToken = default) { var events = new List <IDomainEvent>(); if (!command.Approver.HasRight(UserRight.ScheduleSession)) { return(new DeniedCommandResult($"Right {nameof(UserRight.ScheduleSession)} required"), DomainEvent.None); } var session = await _sessionRepository.GetAsync(command.Session); if (session == null) { return(new NotFoundCommandResult($"Session {command.Session} was not found"), DomainEvent.None); } Schedule?schedule; try { schedule = Schedule.Create(command.StartTime, command.EndTime); } catch (ArgumentException e) { var result = new ValidationFailureCommandResult($"Schedule is invalid: {e.Message}."); result.AddValidationError(e.ParamName, e.Message); return(result, DomainEvent.None); } try { session.SetSchedule(schedule); events.Add(new SessionScheduled(session.Id)); } catch (NotSupportedException e) { return(new ValidationFailureCommandResult(e.Message), DomainEvent.None); } await _sessionRepository.UpdateAsync(session); return(new SuccessCommandResult(), events); }
public async Task <(ICommandResult result, IReadOnlyList <IDomainEvent> events)> ExecuteAsync(RequestSession command, CancellationToken cancellationToken = default) { Schedule?schedule; try { schedule = Schedule.Create(command.StartTime, command.EndTime); } catch (ArgumentException e) { var result = new ValidationFailureCommandResult($"Schedule is invalid: {e.Message}."); result.AddValidationError(e.ParamName, e.Message); return(result, DomainEvent.None); } if (command.Speakers.Any()) { var peopleExists = await Task.WhenAll(command.Speakers.Select(async s => { var name = await _personProjectionStore.GetNameAsync(s); return(id: s, exists: name != null); })); ValidationFailureCommandResult?vfcr = null; foreach (var p in peopleExists.Where(p => !p.exists)) { vfcr ??= new ValidationFailureCommandResult("Speakers are invalid: unknown speakers"); vfcr.AddValidationError(nameof(RequestSession.Speakers), $"{p.id} is unknown"); } if (vfcr != null) { return(vfcr, DomainEvent.None); } } var session = new Session(SessionId.New(), command.Speakers, command.Title, command.Description, command.Tags, schedule, SessionStatus.Requested); await _sessionRepository.AddAsync(session).ConfigureAwait(false); return(new SuccessCommandResult(), new[] { new SessionRequested(session.Id) }); }
public async Task <IActionResult> RequestSessionAsync([FromServices] RequestSessionCommandHandler handler, [FromBody] RequestSessionModel requestSessionModel, CancellationToken cancellationToken) { var command = new RequestSession( new List <PersonId>(requestSessionModel.Speakers.Select(g => (PersonId)g)), requestSessionModel.Title, requestSessionModel.Description, requestSessionModel.Tags, requestSessionModel.StartTime, requestSessionModel.EndTime); var(result, events) = await handler.ExecuteAsync(command, cancellationToken); await _eventDispatcher.DispatchAsync(events); return(result switch { SuccessCommandResult src => Accepted(), ValidationFailureCommandResult vfcr => this.ValidationProblem(vfcr), FailureCommandResult fcr => this.Problem(fcr), _ => Problem() });