Exemple #1
0
        public async Task <Organization> Execute(CreateOrganization command, CancellationToken cancellationToken = new CancellationToken())
        {
            CloudTable organizationTable = _tableProvider.GetTable(_settings.OrganizationTableName);

            var entity = new OrganizationEntity(command.UserId, command.OrganizationName, command.Timestamp);

            await organizationTable.ExecuteAsync(TableOperation.Insert(entity), cancellationToken);

            var indexEntity = new UserOrganizationIndexEntity(command.Timestamp, command.UserId, entity.Id, entity.Name, true);

            CloudTable userOrganizationIndexTable = _tableProvider.GetTable(_settings.UserOrganizationIndexTableName);

            await userOrganizationIndexTable.ExecuteAsync(TableOperation.InsertOrReplace(indexEntity), cancellationToken);

            return(entity);
        }
        public async Task <Organization> Execute(GetOrganization query, CancellationToken cancellationToken = new CancellationToken())
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            if (string.IsNullOrWhiteSpace(query.UserId))
            {
                throw new ArgumentException("UserId is required");
            }

            CloudTable organizationUserTable = _tableProvider.GetTable(_settings.UserOrganizationIndexTableName);

            TableQuery <UserOrganizationIndexEntity> organizationUserQuery = new TableQuery <UserOrganizationIndexEntity>()
                                                                             .Where(TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, query.UserId),
                                                                                                              TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, query.OrganizationId))).Take(1);

            IEnumerable <UserOrganizationIndexEntity> organizationUsers =
                await organizationUserTable.ExecuteQueryAsync(organizationUserQuery, cancellationToken);

            UserOrganizationIndexEntity organizationUser = organizationUsers.SingleOrDefault();

            if (organizationUser == null)
            {
                throw new OrganizationNotFoundException(query.UserId, query.OrganizationId);
            }

            CloudTable organizationTable = _tableProvider.GetTable(_settings.OrganizationTableName);

            TableQuery <OrganizationEntity> organizationQuery = new TableQuery <OrganizationEntity>()
                                                                .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
                                                                                                          query.OrganizationId)).Take(1);

            IEnumerable <OrganizationEntity> organizations = await organizationTable.ExecuteQueryAsync(organizationQuery, cancellationToken);

            OrganizationEntity organization = organizations.SingleOrDefault();

            if (organization == null)
            {
                throw new OrganizationNotFoundException(query.UserId, query.OrganizationId);
            }

            return(organization);
        }