public async Task <Group> CreateGroup(string newGroupName) { Guid newGroupId = Guid.NewGuid(); var groupTable = _tableClient.GetTableReference(newGroupId.ToTableString()); await groupTable.CreateAsync(); var metadata = new GroupMetadata { Name = newGroupName, CreationTime = DateTime.UtcNow }; TableOperation addMetadataOp = TableOperation.Insert(new TableEntityAdapter <GroupMetadata>(metadata, PartitionNames.Metadata, PartitionNames.Metadata)); TableResult addMetadataResult = await groupTable.ExecuteAsync(addMetadataOp); if (addMetadataResult.Result is TableEntityAdapter <GroupMetadata> groupMetaData) { await AddOrUpdateToGroupsList(groupMetaData.OriginalEntity, newGroupId, 0); _logger.LogInformation($"Group with ID {newGroupId} and name {groupMetaData.OriginalEntity.Name} added."); return(new Group { Count = 0, CreationTime = groupMetaData.OriginalEntity.CreationTime, Id = newGroupId, Name = groupMetaData.OriginalEntity.Name }); } else { await groupTable.DeleteAsync(); _logger.LogError($"Unable to add table named {newGroupName} to database."); return(null); } }
public async Task <ServiceResult> AddUserToGroup(User user, Guid groupId) { var groupTable = _tableClient.GetTableReference(groupId.ToTableString()); if (!await groupTable.ExistsAsync()) { _logger.LogError($"Could not find any group with ID {groupId}."); return(ServiceResult.NotFound); } TableOperation getExistingUser = TableOperation.Retrieve <TableEntityAdapter <UserEntity> >(PartitionNames.User, user.Id.ToIdString()); TableResult existsResult = await groupTable.ExecuteAsync(getExistingUser); if (existsResult.Result is TableEntityAdapter <UserEntity> ) { // User already exists. We're done! return(ServiceResult.Success); } // Verify group isn't currently full. int groupCount = await GetGroupCount(groupId); if (groupCount >= MaxGroupSize) { _logger.LogInformation($"Unable to add user {user.Name}-{user.Id} to group ID {groupId}. Group already has {groupCount} users."); return(ServiceResult.InvalidArguments); } var userEntity = new TableEntityAdapter <UserEntity>( new UserEntity { Name = user.Name, LastSeen = DateTime.UtcNow }, PartitionNames.User, user.Id.ToIdString()); TableOperation addUser = TableOperation.Insert(userEntity); TableResult addResult = await groupTable.ExecuteAsync(addUser); if (addResult.HttpStatusCode != StatusCodes.Status204NoContent) { _logger.LogError($"Unable to add user {user.Name}-{user.Id} to group {groupId}. Result code: {addResult.HttpStatusCode}"); return(ServiceResult.ServerError); } // User added. Do bookkeeping GroupMetadata metadata = await GetGroupMetadata(groupId); await AddOrUpdateToGroupsList(metadata, groupId, groupCount + 1); await AddToUserGroups(user.Id, groupId, DateTime.UtcNow); return(ServiceResult.Success); }
private async Task AddOrUpdateToGroupsList(GroupMetadata group, Guid groupId, int count) { var allGroupsTable = _tableClient.GetTableReference(TableNames.AllGroups); await allGroupsTable.CreateIfNotExistsAsync(); var groupEntry = new AllGroupsGroupEntry { Name = group.Name, Count = count, CreationTime = group.CreationTime }; var addOrUpdateOperation = TableOperation.InsertOrReplace( new TableEntityAdapter <AllGroupsGroupEntry>(groupEntry, PartitionNames.Group, groupId.ToIdString())); TableResult result = await allGroupsTable.ExecuteAsync(addOrUpdateOperation); if (!(result.Result is TableEntityAdapter <AllGroupsGroupEntry>)) { // This isn't fatal. We'll catch it next time we do a group update. _logger.LogWarning($"Failed to update group list metadata table with new group: ID - {groupId}, Name - {group.Name}. Status code: {result.HttpStatusCode}"); } }