/// <summary> /// Method which searches for a quantity of a dog at a given store, /// throws an exception and returns null if item request is invalid. /// </summary> /// <param name="store">store to search for dogs</param> /// <param name="dog">dog customer wishes to purchase</param> /// <param name="quant">number of dogs customer wishes to purchase</param> /// <returns>Item if store has it</returns> public Model.Item FindItem(StoreLocation store, Dog dog, int quant) { try{ string add = FindStore(store.Address, store.Location).Address; string loc = FindStore(store.Address, store.Location).Location; Entity.Dog searchDog = ( from Dog in _context.Dogs where Dog.Breed == dog.Breed && Dog.Gender == dog.Gender.ToString() select Dog ).Single(); Entity.DogStore dS = ( from DogStore in _context.DogStores where DogStore.StoreAddress == store.Address && DogStore.StoreName == store.Location select DogStore ).Single(); Entity.Inventory inv = ( from Inventory in _context.Inventories where Inventory.StoreId == dS.Id && Inventory.DogId == searchDog.ItemId select Inventory ).Single(); if (inv.Quantity < quant) { Console.WriteLine("Store doesn't have that many of that dog!"); throw new Exception(); } else { return(new Model.OrderItem(new Dog(searchDog.Breed, searchDog.Gender.ToCharArray()[0], searchDog.Price, searchDog.ItemId), quant)); } } catch (Exception) { Log.Error("Item not found"); return(null); } }
/// <summary> /// Adds order to the database /// </summary> /// <param name="dogOrder">dog order to be added to the database</param> /// <returns>order added to the database</returns> public DogOrder AddOrder(DogOrder dogOrder) { try{ Entity.DogOrder dogOrd = new Entity.DogOrder(); dogOrd.BuyerId = dogOrder.DogBuyer.PhoneNumber; dogOrd.StoreId = dogOrder.StoreLocation.id; dogOrd.DateOrder = dogOrder.OrderDate; dogOrd.Total = dogOrder.Total; _context.DogOrders.Add(dogOrd); _context.SaveChanges(); Entity.OrderItem orderItem; dogOrd = ( from DogOrder in _context.DogOrders where DogOrder.BuyerId == dogOrder.DogBuyer.PhoneNumber && DogOrder.StoreId == dogOrder.StoreLocation.id && DogOrder.DateOrder == dogOrder.OrderDate && DogOrder.Total == dogOrder.Total select DogOrder ).Single(); foreach (Model.Item item in dogOrder.GetItems()) { Entity.Inventory inv = ( from Inventory in _context.Inventories where Inventory.StoreId == dogOrder.StoreLocation.id && Inventory.DogId == item.Dog.id select Inventory ).Single(); inv.Quantity -= item.Quantity; _context.SaveChanges(); orderItem = new Entity.OrderItem(); orderItem.DogId = item.Dog.id; orderItem.OrderId = dogOrd.Id; orderItem.Quantity = item.Quantity; _context.OrderItems.Add(orderItem); _context.SaveChanges(); } return(dogOrder); } catch (Exception e) { Console.WriteLine("Something went wrong :("); Log.Error(e.Message); return(null); } }
/// <summary> /// Adds an item to a stores inventory, creates dog if not found then adds it to the inventory /// </summary> /// <param name="store">Store to add inventory to</param> /// <param name="dog">Dog to add</param> /// <param name="quant">Quantity of Dog to add</param> /// <returns>Added item</returns> public Model.Item AddItem(StoreLocation store, Dog dog, int quant) { Item newItem = new Model.Inventory(dog, quant); try{ Entity.Dog searchDog = ( from Dog in _context.Dogs where Dog.Breed == dog.Breed && Dog.Gender == dog.Gender.ToString() select Dog ).Single(); Entity.DogStore dS = ( from DogStore in _context.DogStores where DogStore.StoreAddress == store.Address && DogStore.StoreName == store.Location select DogStore ).Single(); try{ Entity.Inventory inv = ( from Inventory in _context.Inventories where Inventory.StoreId == dS.Id && Inventory.DogId == searchDog.ItemId select Inventory ).Single(); inv.Quantity += quant; _context.SaveChanges(); Log.Information("Item found, quanty incremented: " + quant.ToString()); return(newItem); } catch (Exception e) { Log.Information(e.Message + "dog found but not inventory, adding dog to store's inventory"); Entity.Inventory inventory = new Entity.Inventory(); inventory.DogId = searchDog.ItemId; inventory.Quantity = quant; inventory.StoreId = dS.Id; _context.Inventories.Add(inventory); _context.SaveChanges(); return(newItem); } } catch (Exception e) { Log.Information(e.Message + "Dog not found, creating new dog"); Entity.Dog newDog = new Entity.Dog(); newDog.ItemId = new Random().Next(); newDog.Breed = dog.Breed; newDog.Gender = dog.Gender.ToString(); newDog.Price = dog.Price; _context.Dogs.Add(newDog); _context.SaveChanges(); Entity.Dog searchDog = newDog; Entity.DogStore dS = ( from DogStore in _context.DogStores where DogStore.StoreAddress == store.Address && DogStore.StoreName == store.Location select DogStore ).Single(); Entity.Inventory inventory = new Entity.Inventory(); inventory.DogId = searchDog.ItemId; inventory.Quantity = quant; inventory.StoreId = dS.Id; _context.Inventories.Add(inventory); _context.SaveChanges(); return(newItem); } }