public async Task <Result <Nothing, Error> > Handle(DeleteNote.Command request, CancellationToken cancellationToken) { var deletingUser = await _userAccessor.GetUser(); var result = await _aggregateStore.Update <SchoolAggregate, SchoolId, Result <Nothing, Error> >( SchoolId.With(request.SchoolId), CommandId.New, async (aggregate, token) => { var notes = aggregate.Notes.Where(x => x.NoteId == request.NoteId).ToList(); var note = aggregate.Notes.SingleOrDefault(x => x.NoteId == request.NoteId); return (await _authService.AuthorizeAsResult(await _userAccessor.GetClaimsPrincipal(), note, AuthorizationPolicies.OwningCoordinatorOnly) .Tap(result => aggregate.DeleteNote(request, deletingUser, _clock.GetCurrentInstant()))); }, cancellationToken); return(result.Unwrap()); }
public Result <Nothing, Error> DeleteNote(DeleteNote.Command command, ApplicationUser deletingUser, Instant now) { Guard.Against.Null(command, nameof(command)); Guard.Against.Null(deletingUser, nameof(deletingUser)); Guard.Against.Default(now, nameof(now)); ValidateIdMatchOrThrow(command.SchoolId); return(Validate(new DeleteNote.Validator(), command) .Ensure( _ => this.IsNew == false, new Error.ResourceNotFound(Messages.School_not_found)) .Ensure( _ => this.Notes.Any(x => x.NoteId == command.NoteId), new Error.DomainError(DeleteNote_Messages.Note_does_not_exist) ) .Tap( _ => Emit(new NoteDeleted( timestamp: now, noteId: command.NoteId, deletingUserId: deletingUser.Id)) )); }
public async Task <IActionResult> DeleteNote(DeleteNote.Command command) { var result = await _engine.Execute(command); return(result.MatchToActionResult(success => Ok())); }