// Puts a character into the DB // ID and character.ID are a little redundant, not sure how I feel about that // I figure we might want a char copy function at some point, so maybe it makes sense to // apply the id to the char as we put them, but I'd probably wanna figure out use cases first public async Task <ErrorInformation> PutCharacter(string id, Character character) { if (String.IsNullOrEmpty(id)) { return(ErrorInformation.InvalidParameterError()); } if (character == null) { return(ErrorInformation.InvalidParameterError()); } if (id == null) { return(ErrorInformation.InvalidParameterError()); } if (!id.Equals(character.Id)) { return(ErrorInformation.InvalidParameterError()); } ErrorInformation result = null; CharacterSchema characterData; if (CharacterExists(id)) { characterData = await Characters.FindAsync(id); if (characterData != null) { characterData.CharacterJsonRepresentation = JsonSerializer.Serialize(character); } } else { characterData = new CharacterSchema() { Id = character.Id, CharacterJsonRepresentation = JsonSerializer.Serialize(character) }; Characters.Add(characterData); } try { await SaveChangesAsync(); } catch (DbUpdateException) { if (CharacterExists(character.Id)) { result = ErrorInformation.DuplicateCharacterError(); } else { throw; } } return(result); }
// Gets a character object from the DB; abstract away the JSON from the controller a bit public async Task <Character> GetCharacter(string id) { CharacterSchema charJson = await Characters.FindAsync(id); if (charJson == null) { return(null); } if (String.IsNullOrEmpty(charJson.CharacterJsonRepresentation)) { return(null); } return(JsonSerializer.Deserialize <Character>(charJson.CharacterJsonRepresentation)); }