public async Task <JsonResult> GetCharacterList([FromServices] IReadonlyCharacterRepository characterRepository) { if (characterRepository == null) { throw new ArgumentNullException(nameof(characterRepository)); } //The account id should be in the JWT/claim sent. It's required to load the characters from the database //that are associated with the account. int accountId = HaloLiveUserManager.GetUserIdInt(User); //We don't load additional uneeded information. The Ids are enough for the client to load their names, profiles and appearance if required. int[] characterIds = (await characterRepository.LoadAssociatedCharacterIds(accountId)).ToArray(); characterIds = characterIds ?? Enumerable.Empty <int>().ToArray(); //We don't need to do anything fancy. The ID of the characters is TRULY enough for the client to then request and piece together all the other missing content return(Json(new CharacterListResponse(characterIds))); }
public async Task <JsonResult> CreateCharacter([FromBody] CharacterCreationRequest request, [FromServices] ICharacterRepository characterRepository) { if (characterRepository == null) { throw new ArgumentNullException(nameof(characterRepository)); } //TODO: Return error if (!ModelState.IsValid) { return(Json(new CharacterCreationResponse(CharacterCreationResponseCode.GeneralServerError))); } //The account id should be in the JWT/claim sent. It's required to load the characters from the database //that are associated with the account. int accountId = HaloLiveUserManager.GetUserIdInt(User); //TODO: We should check banned names list //We must check the name first because there could be a duplicate bool nameIsTaken = await characterRepository.DoesNameExist(request.CharacterName); if (nameIsTaken) { return(Json(new CharacterCreationResponse(CharacterCreationResponseCode.NameInvalid))); } //TODO: Enforce a maximum account of characters per account CharacterCreationResult result = await characterRepository.TryCreateNewCharacter(accountId, Request.HttpContext.Connection.RemoteIpAddress.ToString(), new CharacterCreationInformation(request.CharacterName, request.CharacterClass)); //This means that the character was created and was assigned a section id if (result.IsSuccessful) { return(Json(new CharacterCreationResponse(result.CharacterSectionId))); } //We don't know what went wrong here, something did. This could be due to a race condition. It should so rarely occur that it is most likely just a major server //issue instead. return(Json(new CharacterCreationResponse(CharacterCreationResponseCode.GeneralServerError))); }