public ActionResult <string> Get([FromQuery] string characterName, string characterAttribute) { // REFLECTION!!!!! Pass a string as a property name to a PropertyInfo for any type // then get the value of that property for any object of that type. try { using (var context = new CharacterManagementDBContext()) { var selectedCharacter = context.CharacterDetails .FirstOrDefault(name => name.CharacterName == characterName); PropertyInfo selectedAttribute = selectedCharacter.GetType().GetProperty(characterAttribute); return(selectedAttribute.GetValue(selectedCharacter).ToString()); } } catch (DbException) { return("Unable to retrieve attribute value"); } catch (Exception) { return("Unable to retrieve attribute value"); } }
public ActionResult <SelectedItemDetails> Get([FromQuery] string characterName, string itemName) { SelectedItemDetails itemDetails = new SelectedItemDetails(); using (var context = new CharacterManagementDBContext()) { var details = context.Items .FirstOrDefault(item => item.ItemName == itemName); var quantity = context.CharacterInventory .FirstOrDefault(inventory => inventory.CharacterName == characterName && inventory.ItemName == itemName) .ItemQuantity; itemDetails.ItemName = details.ItemName; itemDetails.ItemValue = details.ItemValue; itemDetails.ItemDescription = details.ItemDescription; itemDetails.ItemQuantity = quantity; } return(itemDetails); }
public ActionResult <int> Get([FromQuery] string characterName, string itemName, int changeInQuantity) { try { using (var context = new CharacterManagementDBContext()) { var selectedInventory = context.CharacterInventory .FirstOrDefault(inventory => inventory.CharacterName == characterName && inventory.ItemName == itemName); var currentQuantity = selectedInventory.ItemQuantity; if (!(currentQuantity == 0 && changeInQuantity == -1)) { selectedInventory.ItemQuantity = currentQuantity + changeInQuantity; context.SaveChanges(); } return(selectedInventory.ItemQuantity); } } catch (DbException) { return(-1); } catch (Exception) { return(-1); } }
public ActionResult <string> Post([FromBody] InventoryUpdateInfo inventoryUpdate) { Items updateItem = new Items(); updateItem.ItemName = inventoryUpdate.ItemName; updateItem.ItemDescription = inventoryUpdate.ItemDescription; updateItem.ItemValue = inventoryUpdate.ItemValue; CharacterInventory inventoryToUpdate = new CharacterInventory(); inventoryToUpdate.CharacterName = inventoryUpdate.CharacterName; inventoryToUpdate.ItemName = inventoryUpdate.ItemName; inventoryToUpdate.ItemQuantity = inventoryUpdate.ItemQuantity; try { using (var context = new CharacterManagementDBContext()) { if (!context.Items.Any(item => item.ItemName == updateItem.ItemName)) { context.Items.Add(updateItem); } if (!context.CharacterInventory.Any(inventory => (inventory.CharacterName == inventoryToUpdate.CharacterName && inventory.ItemName == inventoryToUpdate.ItemName))) { context.CharacterInventory.Add(inventoryToUpdate); } else { var characterInventoryToUpdate = context.CharacterInventory .FirstOrDefault(inventory => (inventory.CharacterName == inventoryToUpdate.CharacterName && inventory.ItemName == inventoryToUpdate.ItemName)); var currentQuantity = characterInventoryToUpdate.ItemQuantity; characterInventoryToUpdate.ItemQuantity = currentQuantity + inventoryToUpdate.ItemQuantity; } context.SaveChanges(); } } catch (DbUpdateException) { return("Inventory could not be updated. Please try again."); } catch (Exception) { return("Inventory update failed."); } return($"{inventoryUpdate.CharacterName}'s inventory updated successfully!"); }
public ActionResult <string> Post([FromBody] SpellsUpdateInfo spellsUpdate) { Spells updateSpell = new Spells(); updateSpell.SpellName = spellsUpdate.SpellName; updateSpell.SpellLevel = (short)spellsUpdate.SpellLevel; updateSpell.SchoolOfMagic = spellsUpdate.SchoolOfMagic; updateSpell.SpellCastingTime = spellsUpdate.SpellCastingTime; updateSpell.Ritual = spellsUpdate.Ritual; updateSpell.SpellRange = spellsUpdate.SpellRange; updateSpell.SpellComponents = spellsUpdate.SpellComponents; updateSpell.SpellDuration = spellsUpdate.SpellDuration; updateSpell.SpellDescription = spellsUpdate.SpellDescription; CharacterSpells spellListToUpdate = new CharacterSpells(); spellListToUpdate.CharacterName = spellsUpdate.CharacterName; spellListToUpdate.SpellName = spellsUpdate.SpellName; try { using (var context = new CharacterManagementDBContext()) { if (!context.Spells.Any(spell => spell.SpellName == updateSpell.SpellName)) { context.Spells.Add(updateSpell); } if (!context.CharacterSpells.Any(spellList => (spellList.CharacterName == spellListToUpdate.CharacterName && spellList.SpellName == spellListToUpdate.SpellName))) { context.CharacterSpells.Add(spellListToUpdate); } else { return($"{spellsUpdate.CharacterName} already knows that spell!"); } context.SaveChanges(); } } catch (DbUpdateException) { return("Spells could not be updated. Please try again."); } catch (Exception) { return("Spells update failed."); } return($"{spellsUpdate.CharacterName}'s spell list updated successfully!"); }
public ActionResult <Spells> Get([FromQuery] string spellName) { try { using (var context = new CharacterManagementDBContext()) { var spellDetails = context.Spells .FirstOrDefault(spell => spell.SpellName == spellName); return(spellDetails); } } catch (Exception) { return(new Spells()); } }
public ActionResult <CharacterDetails> Get([FromQuery] string characterName) { try { using (var context = new CharacterManagementDBContext()) { var selectedCharacterInfo = context.CharacterDetails .FirstOrDefault(details => details.CharacterName == characterName); return(selectedCharacterInfo); } } catch (DbException) { return(new CharacterDetails()); //figure out a better way to handle this instead of sending a blank object } }
public ActionResult <IEnumerable <string> > Get([FromQuery] string characterName) { List <string> listOfSpellNames = new List <string>(); using (var context = new CharacterManagementDBContext()) { var characterSpellList = context.CharacterSpells .Where(spells => spells.CharacterName == characterName) .Select(spells => new { spells.SpellName }); foreach (var spell in characterSpellList) { listOfSpellNames.Add(spell.SpellName); } } return(listOfSpellNames); }
public ActionResult <IEnumerable <string> > Get([FromQuery] string characterName) { List <string> listOfCharacterNames = new List <string>(); using (var context = new CharacterManagementDBContext()) { var characterInventory = context.CharacterInventory .Where(items => items.CharacterName == characterName) .Select(items => new { items.ItemName }); foreach (var item in characterInventory) { listOfCharacterNames.Add(item.ItemName); } } return(listOfCharacterNames); }
public ActionResult <IEnumerable <string> > Get() { List <string> listOfCharacterNames = new List <string>(); using (var context = new CharacterManagementDBContext()) { var allCharacterNames = context.CharacterDetails .OrderBy(names => names.CharacterName) .Select(names => new { names.CharacterName }); //??projection?? foreach (var name in allCharacterNames) { listOfCharacterNames.Add(name.CharacterName); } } return(listOfCharacterNames); }
public ActionResult <string> Post([FromBody] CharacterDetails newCharacter) { try { using (var context = new CharacterManagementDBContext()) { context.CharacterDetails.Add(newCharacter); context.SaveChanges(); } } catch (DbUpdateException) { return("Character could not be added to the database. Please try again."); } catch (Exception) { return("Character addition failed."); } return($"{newCharacter.CharacterName} added successfully!"); }
public ActionResult <string> Post([FromBody] CharacterUpdateInfoWrapper updateInfoWrapper) { CharacterUpdateInfoParser updateInfo = new CharacterUpdateInfoParser(updateInfoWrapper); updateInfo.ParseUpdateInfo(); try { using (var context = new CharacterManagementDBContext()) { for (int i = 0; i < updateInfo.UpdateNames.Count; i++) { var characterToUpdate = context.CharacterDetails .FirstOrDefault(details => details.CharacterName == updateInfo.UpdateNames.ElementAt(i)); if (characterToUpdate != null) { PropertyInfo attributeToUpdate = characterToUpdate.GetType().GetProperty(updateInfo.UpdateAttributes.ElementAt(i)); attributeToUpdate.SetValue(characterToUpdate, Convert.ChangeType(updateInfo.UpdateNewValues.ElementAt(i), attributeToUpdate.PropertyType), null); } } context.SaveChanges(); } } catch (DbUpdateException) { return("Unable to connect to database please try again."); } catch (Exception) { return("Character update failed due to unexpected error."); } return("Character information updated successfully!"); // return updateInfoWrapper.DynamicUpdateInfo; }