コード例 #1
0
        public Result <Guid, Error> AddNote(AddNote.Command command, ApplicationUser author, Instant now)
        {
            Guard.Against.Null(command, nameof(command));
            Guard.Against.Null(author, nameof(author));
            Guard.Against.Default(now, nameof(now));
            ValidateIdMatchOrThrow(command.SchoolId);

            System.Diagnostics.Debug.Assert(command.Content != null);

            return(Validate(new AddNote.Validator(), command)
                   .Ensure(
                       _ => this.IsNew == false,
                       new Error.ResourceNotFound(Messages.School_not_found))
                   .Map(
                       _ => {
                var noteId = Guid.NewGuid();
                Emit(new NoteAdded(
                         timestamp: now,
                         noteId: noteId,
                         authorId: author.Id,
                         content: command.Content));
                return noteId;
            }
                       ));
        }
コード例 #2
0
        public async Task <Result <Guid, Error> > Handle(AddNote.Command request, CancellationToken cancellationToken)
        {
            var author = await _userAccessor.GetUser();

            var result = await _aggregateStore.Update <SchoolAggregate, SchoolId, Result <Guid, Error> >(
                SchoolId.With(request.SchoolId), CommandId.New,
                (aggregate) => aggregate.AddNote(request, author, _clock.GetCurrentInstant()),
                cancellationToken);

            return(result.Unwrap());
        }
コード例 #3
0
        public async Task <Result <Nothing, Error> > Handle(AddNote.Command request, CancellationToken cancellationToken)
        {
            var maybeTraining = await _trainingRepo.GetById(request.TrainingId);

            if (maybeTraining.HasNoValue)
            {
                return(Result.Failure <Nothing, Error>(new Error.ResourceNotFound(ErrorMessages.TrainingNotFound)));
            }

            var training = maybeTraining.Value;
            var user     = await _userAccessor.GetUser();

            var now = _clock.GetCurrentInstant();

            var result = await training.AddNote(user.Id, request.Content, now)
                         .Tap(async() => await _trainingRepo.Update(training));

            return(result
                   .Match(
                       onSuccess: () => Result.Success <Nothing, Error>(Nothing.Value),
                       error => Result.Failure <Nothing, Error>(new Error.BadRequest(error))
                       ));
        }
コード例 #4
0
        public async Task <IActionResult> AddNote(AddNote.Command command)
        {
            var result = await _engine.Execute(command);

            return(result.MatchToActionResult(success => Ok(success)));
        }