Example #1
0
        public async Task <ActionResult <User> > Post(PostUserRequest userRequest)
        {
            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (string.IsNullOrWhiteSpace(userId))
            {
                return(BadRequest("No userId found."));
            }

            if (string.IsNullOrWhiteSpace(userId))
            {
                return(BadRequest("No userId found."));
            }

            if (string.IsNullOrWhiteSpace(userRequest.Username))
            {
                return(BadRequest("Username cannot be empty."));
            }

            if (userRequest.Username.Length > 30)
            {
                return(BadRequest("Username cannot be longer than 30 characters."));
            }

            var users = await _userManager.SearchUsersAsync(userRequest.Username);

            if (users.Any(u => u.Id != userId))
            {
                return(BadRequest("Username is taken."));
            }

            var user = new User
            {
                ContentSourceEnum = ContentSource.Community,
                ContentTypeEnum   = ContentType.CommunityContent,
                RowKey            = userId,
                PartitionKey      = ContentType.CommunityContent.ToString(),
                Id       = userId,
                Username = userRequest.Username
            };

            await _tableStorage.AddOrUpdateAsync("users", user);

            return(Ok(user));
        }
        public async Task <Character> SaveCharacterAsync(PostCharacterRequest characterRequest, string userId,
                                                         CharacterPermissionLevel permission)
        {
            var blobContainerClient =
                new BlobContainerClient(_configuration["StorageAccountConnectionString"], "characters");
            await blobContainerClient.CreateIfNotExistsAsync();

            if (string.IsNullOrWhiteSpace(characterRequest.Id))
            {
                characterRequest.Id = Guid.NewGuid().ToString();
            }

            var blobClient = blobContainerClient.GetBlobClient($"{userId}/{characterRequest.Id}");

            var content = Encoding.UTF8.GetBytes(characterRequest.JsonData);

            await using var ms = new MemoryStream(content);
            await blobClient.UploadAsync(ms, true);

            var character = new Character
            {
                RowKey            = characterRequest.Id,
                PartitionKey      = ContentType.CommunityContent.ToString(),
                ContentSourceEnum = ContentSource.Community,
                ContentTypeEnum   = ContentType.CommunityContent,
                JsonData          = characterRequest.JsonData,
                Id = characterRequest.Id
            };

            if (permission == CharacterPermissionLevel.Owner)
            {
                character.UserId          = userId;
                character.UserPermissions = characterRequest.UserPermissions;
            }

            await _tableStorage.AddOrUpdateAsync("characters", character);

            return(character);
        }