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);
            }
        }
예제 #2
0
        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!");
        }
예제 #3
0
        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 <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!");
        }
예제 #5
0
        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;
        }