コード例 #1
0
        public void HandleCommand(CreateProjectCommand command)
        {
            if (string.IsNullOrEmpty(command.ProjectName))
            {
                throw new Exception("Project is now alloed to have empty name");
            }
            if (command.UserId == Guid.Empty)
            {
                throw new Exception("User is not allowed to have empty Key");
            }

            var userKey          = new SimpleKey(command.UserId.ToString());
            var userBeforeUpdate = _snapshotStore.Load <User>(userKey);
            var user             = _snapshotStore.Load <User>(userKey);

            if (user.ProjectCount >= 5)
            {
                throw new Exception("User is not allowed to have more than 5 projects");
            }

            var projectKey = new SimpleKey(command.ProjectId.ToString());

            try {
                var project = _snapshotStore.Create(projectKey, new Project(command.ProjectName, command.UserId));
                user.AddProject();
            }
            catch (Exception) {
                _snapshotStore.Update(userKey, userBeforeUpdate);
                _snapshotStore.Delete <Project>(projectKey);

                throw new Exception($"Failed to apply {nameof(CreateProjectCommand)}");
            }
        }
コード例 #2
0
        public User HandleQuery(GetByIdQuery query)
        {
            var userKey = new SimpleKey(query.Id.ToString());

            return(_snapshotStore.Load <User>(userKey));
        }
コード例 #3
0
        public Project HandleQuery(GetByIdQuery query)
        {
            var projectKey = new SimpleKey(query.Id.ToString());

            return(_snapshotStore.Load <Project>(projectKey));
        }
コード例 #4
0
        public void HandleCommand(CreateUserCommand command)
        {
            var userKey = new SimpleKey(command.UserId.ToString());

            _snapshotStore.Create(userKey, new User(command.UserName, command.UserId));
        }