public async Task Perform(ReadOnlyMemory <byte> elevatorAction)
        {
            var action = JsonSerializer.Deserialize <ElevatorCommandBase>(elevatorAction.Span);

            var elevator = await _aggregateRepository.LoadAsync <Elevator>(action.ElevatorId);

            switch (action.ActionType)
            {
            case ElevatorCommandType.CloseDoor:
                await CloseDoors(action.ElevatorId);

                break;

            case ElevatorCommandType.OpenDoor:
                await OpenDoors(action.ElevatorId);

                break;

            case ElevatorCommandType.GoToFloor:
                var goToFloor = JsonSerializer.Deserialize <GoToFloorCommand>(elevatorAction.Span);
                await GoToFloor(goToFloor);

                break;
            }

            await _aggregateRepository.SaveAsync(elevator);
        }
Ejemplo n.º 2
0
        public async Task CreateAsync(string emailAddress)
        {
            var user = new User(Guid.NewGuid(), emailAddress);

            _subscriber.Subscribe <UserSubmittedEvent>(async domainEvent => await HandleAsync(_userSubmittedEventHandlers, domainEvent));
            await _userRepository.SaveAsync(user);
        }
        public async Task HandleAsync(CreateProductCommand command, CancellationToken cancellationToken = new CancellationToken())
        {
            var aggregate = new ProductAggregate(command);

            IsHandled = true;
            await _repo.SaveAsync(aggregate, cancellationToken);
        }
Ejemplo n.º 4
0
        public async Task SaveAsync <AR>(AR aggregateRoot) where AR : IAggregateRoot
        {
            await aggregateRepository.SaveAsync <AR>(aggregateRoot).ConfigureAwait(false);

            if (ReferenceEquals(null, aggregateRoot.UncommittedEvents) || aggregateRoot.UncommittedEvents.Any() == false)
            {
                return;
            }

            var events = aggregateRoot.UncommittedEvents.ToList();

            for (int i = 0; i < events.Count; i++)
            {
                var theEvent    = events[i];
                var entityEvent = theEvent as EntityEvent;
                if (ReferenceEquals(null, entityEvent) == false)
                {
                    theEvent = entityEvent.Event;
                }


                eventPublisher.Publish(theEvent, BuildHeaders(aggregateRoot, i));
            }

            var publicEvents = aggregateRoot.UncommittedPublicEvents.ToList();

            for (int i = 0; i < publicEvents.Count; i++)
            {
                publicEventPublisher.Publish(publicEvents[i], BuildHeaders(aggregateRoot, i));
            }
        }
Ejemplo n.º 5
0
        public async Task ProcessAsync(string aggregateId, Action <TAggregate> command, CancellationToken cancellationToken = default)
        {
            var aggregate = await _aggregateRepository.GetAsync(aggregateId, cancellationToken);

            command?.Invoke(aggregate);
            await _aggregateRepository.SaveAsync(aggregate, cancellationToken);
        }
        public async Task ExecuteAsync(LogoutCommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{nameof(LogoutCommandHandler)}.{nameof(ExecuteAsync)} was cancelled before execution");
                cancellationToken.ThrowIfCancellationRequested();
            }

            await _signInManager.SignOutAsync();

            var logout = await _interaction.GetLogoutContextAsync(command.LogoutId);

            await _persistedGrantService.RemoveAllGrantsAsync(command.Subject, logout?.ClientId);

            var aggregate = await _aggregateRepository.GetAsync <User, UserState>(command.Subject, cancellationToken);

            var expectedVersion = aggregate.Version;

            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            aggregate.Logout();

            await _aggregateRepository.SaveAsync(aggregate, command, expectedVersion, cancellationToken);
        }
Ejemplo n.º 7
0
    public async Task <Guid> Handle(RegisterUserCommand request, CancellationToken cancellationToken)
    {
        var user = User.Initialize(_userChecker, _passwordService, request.Email, request.Password,
                                   request.Name);

        await _aggregateRepository.SaveAsync(user);

        return(user.Id);
    }
    public async Task <Guid> Handle(AddNoteCommentCommand request, CancellationToken cancellationToken)
    {
        var note = await _aggregateRepository.LoadAsync <Note>(request.NoteId);

        var comment = note.AddComment(_currentUser.Id, request.Text);

        await _aggregateRepository.SaveAsync(comment);

        return(comment.Id);
    }
    public async Task <Unit> Handle(UpdateProfileCommand request, CancellationToken cancellationToken)
    {
        var user = await _aggregateRepository.LoadAsync <User>(_currentUser.Id);

        user.UpdateProfile(request.Name, request.Avatar, request.Bio);

        await _aggregateRepository.SaveAsync(user);

        return(Unit.Value);
    }
Ejemplo n.º 10
0
        public async Task <Unit> Handle(JoinMeetupCommand request, CancellationToken cancellationToken)
        {
            var aggregate = await _aggregateRepository.LoadAsync <Meetup>(request.MeetupId.ToString(), cancellationToken);

            aggregate.Join(request.ParticipantId);

            await _aggregateRepository.SaveAsync(aggregate);

            return(Unit.Value);
        }
Ejemplo n.º 11
0
    public async Task <Unit> Handle(EditNoteCommand request, CancellationToken cancellationToken)
    {
        var note = await _aggregateRepository.LoadAsync <Note>(request.NoteId);

        note.Edit(_currentUser.Id, request.Title, request.Content, request.Comment);

        await _aggregateRepository.SaveAsync(note);

        return(Unit.Value);
    }
Ejemplo n.º 12
0
    public async Task <Unit> Handle(UpdateUserRolesCommand request, CancellationToken cancellationToken)
    {
        var user = await _aggregateRepository.LoadAsync <User>(request.UserId);

        user.UpdateRoles(request.Roles);

        await _aggregateRepository.SaveAsync(user);

        return(Unit.Value);
    }
Ejemplo n.º 13
0
        public async Task <Unit> Handle(CancelMeetupCommand request, CancellationToken cancellationToken)
        {
            var aggregate = await _aggregateRepository.LoadAsync <Meetup>(request.MeetupId.ToString(), cancellationToken);

            aggregate.Cancel(request.OrganizerId);

            await _aggregateRepository.SaveAsync(aggregate);

            return(Unit.Value);
        }
Ejemplo n.º 14
0
    public async Task <Guid> Handle(AddCommentReplyCommand request, CancellationToken cancellationToken)
    {
        var comment = await _aggregateRepository.LoadAsync <Comment>(request.CommentId);

        var replyComment = comment.Reply(_currentUser.Id, request.Text);

        await _aggregateRepository.SaveAsync(replyComment);

        return(replyComment.Id);
    }
Ejemplo n.º 15
0
        public async Task <Unit> Handle(CreateMeetupCommand request, CancellationToken cancellationToken)
        {
            var aggregate = await _aggregateRepository.LoadAsync <Meetup>(request.MeetupId.ToString(), cancellationToken);

            await aggregate.Register(request.MeetupId, request.OrganizerId, request.Subject, request.When, request.Description, request.Address, _meetupPolicy);

            await _aggregateRepository.SaveAsync(aggregate);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(AddCommentCommand request, CancellationToken cancellationToken)
        {
            var aggregate = await _aggregateRepository.LoadAsync <Meetup>(request.MeetupId.ToString(), cancellationToken);

            aggregate.AddComment(request.CommentatorId, request.Comment);

            await _aggregateRepository.SaveAsync(aggregate);

            return(Unit.Value);
        }
Ejemplo n.º 17
0
    public async Task <Guid> Handle(CloneNoteCommand request, CancellationToken cancellationToken)
    {
        var note = await _aggregateRepository.LoadAsync <Note>(request.NoteId);

        var cloneNote = note.Clone(_currentUser.Id, request.SpaceId);

        await _aggregateRepository.SaveAsync(cloneNote);

        return(cloneNote.Id);
    }
    public async Task <Unit> Handle(ChangePasswordCommand request, CancellationToken cancellationToken)
    {
        var user = await _aggregateRepository.LoadAsync <User>(_currentUser.Id);

        user.ChangePassword(_passwordService, request.OldPassword, request.NewPassword);

        await _aggregateRepository.SaveAsync(user);

        return(Unit.Value);
    }
    public async Task <Unit> Handle(CloseMergeRequestCommand request, CancellationToken cancellationToken)
    {
        var mergeRequest = await _aggregateRepository.LoadAsync <MergeRequest>(request.MergeRequestId);

        mergeRequest.Close(_currentUser.Id);

        await _aggregateRepository.SaveAsync(mergeRequest);

        return(Unit.Value);
    }
Ejemplo n.º 20
0
    public async Task <Unit> Handle(MergeNoteCommand request, CancellationToken cancellationToken)
    {
        var note = await _aggregateRepository.LoadAsync <Note>(request.NoteId);

        note.Merge(request.SourceNoteId, request.Title, request.Content);

        await _aggregateRepository.SaveAsync(note);

        return(Unit.Value);
    }
Ejemplo n.º 21
0
    public async Task <Unit> Handle(UpdateNoteTagsCommand request, CancellationToken cancellationToken)
    {
        var note = await _aggregateRepository.LoadAsync <Note>(request.NoteId);

        note.UpdateTags(request.Tags);

        await _aggregateRepository.SaveAsync(note);

        return(Unit.Value);
    }
    public async Task <Guid> Handle(AddMergeRequestCommentCommand request, CancellationToken cancellationToken)
    {
        var mergeRequest = await _aggregateRepository.LoadAsync <MergeRequest>(request.MergeRequestId);

        var comment = mergeRequest.AddComment(_currentUser.Id, request.Text);

        await _aggregateRepository.SaveAsync(comment);

        return(comment.Id);
    }
Ejemplo n.º 23
0
        public async Task SaveAsync <AR>(AR aggregateRoot) where AR : IAggregateRoot
        {
            using (logger.BeginScope(s => s
                                     .AddScope(Log.AggregateName, typeof(AR).Name)
                                     .AddScope(Log.AggregateId, aggregateRoot.State.Id.Value)))
            {
                await realDeal.SaveAsync <AR>(aggregateRoot).ConfigureAwait(false);

                logger.Debug(() => "Aggregate has been saved.");
            }
        }
Ejemplo n.º 24
0
    public async Task <Guid> Handle(CreateMergeRequestCommand request, CancellationToken cancellationToken)
    {
        var note = await _aggregateRepository.LoadAsync <Note>(request.NoteId);

        var mergeRequest = note.CreateMergeRequest(_noteChecker, _mergeRequestChecker, _currentUser.Id, request.Title,
                                                   request.Description);

        await _aggregateRepository.SaveAsync(mergeRequest);

        return(mergeRequest.Id);
    }
Ejemplo n.º 25
0
        public async Task <Unit> Handle(StartToDo request, CancellationToken cancellationToken)
        {
            var toDo = await _aggregateRepository.LoadAsync(request.ToDoId);

            var @event = new ToDoStartedV1(toDo.Id, toDo.State.ToDoListId);

            toDo.When(@event);

            await _aggregateRepository.SaveAsync(toDo);

            return(Unit.Value);
        }
Ejemplo n.º 26
0
    public async Task <Unit> Handle(EditCommentCommand request, CancellationToken cancellationToken)
    {
        var comment = await _aggregateRepository.LoadAsync <Comment>(request.CommentId);

        await _resourceAuthorizationService.CheckAsync(comment, CommonOperations.Update);

        comment.Edit(request.Text);

        await _aggregateRepository.SaveAsync(comment);

        return(Unit.Value);
    }
Ejemplo n.º 27
0
        public async Task <Unit> Handle(AddToDo request, CancellationToken cancellationToken)
        {
            var toDo = await _aggregateRepository.LoadAsync(null);

            var @event = new ToDoAddedV1(toDo.Id, request.ToDoListId, request.Label, request.Description);

            toDo.When(@event);

            await _aggregateRepository.SaveAsync(toDo);

            return(Unit.Value);
        }
Ejemplo n.º 28
0
        public async Task <Unit> Handle(DeleteToDoList request, CancellationToken cancellationToken)
        {
            var toDoList = await _aggregateRepository.LoadAsync(request.ToDoListId);

            var @event = new ToDoListDeletedV1(toDoList.Id);

            toDoList.When(@event);

            await _aggregateRepository.SaveAsync(toDoList);

            return(Unit.Value);
        }
Ejemplo n.º 29
0
    public async Task <Unit> Handle(DeleteSpaceCommand request, CancellationToken cancellationToken)
    {
        var space = await _aggregateRepository.LoadAsync <Space>(request.SpaceId);

        await _resourceAuthorizationService.CheckAsync(space, CommonOperations.Delete);

        space.Delete();

        await _aggregateRepository.SaveAsync(space);

        return(Unit.Value);
    }
Ejemplo n.º 30
0
    public async Task <Guid> Handle(CreateNoteCommand request, CancellationToken cancellationToken)
    {
        var space = await _aggregateRepository.LoadAsync <Space>(request.SpaceId);

        var note = space.CreateNote(
            request.Title,
            request.Content,
            request.Status);

        await _aggregateRepository.SaveAsync(note);

        return(note.Id);
    }