public void AddOrderToDatabase() { var options = new DbContextOptionsBuilder <CustomerApplicationContext>() .UseInMemoryDatabase(databaseName: "AddOrderToDatabase") .Options; using (var db = new CustomerApplicationContext(options)) { db.UserTypes.Add(new UserType() { Description = "Something", Name = "Something" }); db.Locations.Add(new Location("Something")); db.SaveChanges(); db.Users.Add(new User("Something", "Something", "Something", db.UserTypes.First())); db.Products.Add(new Product("Something", "1.00", db.Locations.First())); db.SaveChanges(); db.Orders.Add(new Order(db.Users.First(), db.Products.First(), 10)); db.SaveChanges(); } using (var db = new CustomerApplicationContext(options)) { Assert.Single(db.Orders); } }
public void RemoveLocationFromDatabase() { var options = new DbContextOptionsBuilder <CustomerApplicationContext>() .UseInMemoryDatabase(databaseName: "RemoveLocationFromDatabase") .Options; using (var db = new CustomerApplicationContext(options)) { db.UserTypes.Add(new UserType() { Description = "Something", Name = "Something" }); db.Locations.Add(new Location("Something")); db.SaveChanges(); db.Users.Add(new User("Something", "Something", "Something", db.UserTypes.First())); db.Products.Add(new Product("Something", "1.00", db.Locations.First())); db.SaveChanges(); db.Orders.Add(new Order(db.Users.First(), db.Products.First(), 10)); db.SaveChanges(); } using (var db = new CustomerApplicationContext(options)) { db.Locations.Remove(db.Locations.Include(location => location.Products).ThenInclude(product => product.Orders).First()); db.SaveChanges(); } using (var db = new CustomerApplicationContext(options)) { Assert.Empty(db.Locations); Assert.Empty(db.Products); Assert.Empty(db.Orders); } }
/// <summary> /// Increments and decrements product quantities at the location. /// Parses input string for <c>Product</c> <c>Id</c> and the amount that <c>Quantity</c> changes. /// </summary> /// <param name="input">Input string which will be parsed.</param> /// <param name="locationId">Id of current location, used for validation.</param> /// <returns>Collection of products updated.</returns> public ICollection <Product> UpdateInventory(string input, int locationId) { using (var db = new CustomerApplicationContext()) { //Loads current location from database. var location = db.Locations.Find(locationId); List <Product> productsUpdated = new List <Product>(); foreach (var productsElement in input.Split(",")) { //Parses input. var productQuery = productsElement.Trim().Split(" "); var productId = Int32.Parse(productQuery[0]); var quantity = Int32.Parse(productQuery[1]); var product = db.Products.Find(productId); //Verifies all products reference the current location. if (product.Location.Id != locationId || (product.Quantity + quantity) < 0) { throw new Exception(); } product.Quantity += quantity; productsUpdated.Add(product); } db.SaveChanges(); return(productsUpdated); } }
/// <summary> /// Creates new <c>Order</c> instances based on input. /// Parses the input for <c>Product</c> <c>Id</c> and <c>Quantity</c>. /// Decrements product quantities accordingly. /// </summary> /// <param name="input">The input string that will be parsed to create orders.</param> /// <param name="userId">The id of the current user.</param> /// <param name="locationId">The id of the current location.</param> /// <returns>Collection of the new order instances.</returns> public ICollection <Order> PlaceOrders(string input, int userId, int locationId) { using (var db = new CustomerApplicationContext()) { //Loads current User and Location from database. var user = db.Users.Find(userId); var location = db.Locations.Find(locationId); //Stores Order instances to be displayed inserting into database. List <Order> ordersPlaced = new List <Order>(); foreach (var ordersElement in input.Split(",")) { //Parses input. var orderQuery = ordersElement.Trim().Split(" "); var productId = Int32.Parse(orderQuery[0]); var orderQuantity = Int32.Parse(orderQuery[1]); var product = db.Products.Find(productId); //Verifies Product references current Location and sufficient inventory. if (product.Location.Id != locationId || product.Quantity - orderQuantity < 0 || orderQuantity > 50 || orderQuantity <= 0) { throw new Exception(); } product.Quantity -= orderQuantity; //Creates new Order instance and inserts into database. ordersPlaced.Add(db.Orders.Add(new Order(user, product, orderQuantity)).Entity); } db.SaveChanges(); return(ordersPlaced); } }
public void AddProductToDatabase() { var options = new DbContextOptionsBuilder <CustomerApplicationContext>() .UseInMemoryDatabase(databaseName: "AddProductToDatabase") .Options; using (var db = new CustomerApplicationContext(options)) { db.Locations.Add(new Location("Some Place")); db.SaveChanges(); db.Products.Add(new Product("Some Product", "1.00", db.Locations.First())); db.SaveChanges(); } using (var db = new CustomerApplicationContext(options)) { Assert.Single(db.Products); } }
/// <summary> /// Finds and removes a <c>Location</c> from the database. /// Removes by Id. /// </summary> /// <param name="locationId">Id of the location object to be deleted from the database.</param> /// <returns>Returns the Location object removed.</returns> public Location RemoveLocation(int locationId) { using (var db = new CustomerApplicationContext()) { var location = db.Locations.Find(locationId); db.Locations.Remove(location); db.SaveChanges(); return(location); } }
/// <summary> /// Validates console input to instantiate a new product and insert it into the database. /// </summary> /// <param name="locationId">Id of location instance which the new product will reference.</param> /// <returns> /// null if console input fails validation. /// Product object if new location inserted into database. /// </returns> public Product Build(int locationId) { using (var db = new CustomerApplicationContext()) { Console.WriteLine("Creating a New Product\n"); NameInput(); PriceInput(); var location = db.Locations.Find(locationId); var product = db.Products.Add(new Product(Name, Price, location)).Entity; db.SaveChanges(); return(product); } }
/// <summary> /// Validates console input to instantiate a new location and insert it into the database. /// </summary> /// <returns> /// null if console input fails validation. /// Location object if new location inserted into database. /// </returns> public Location Build() { Console.WriteLine("Creating a New Location:\n"); //Console input. NameInput(); using (var db = new CustomerApplicationContext()) { //Creates a new Location instance and inserts into database. Location location = db.Locations.Add(new Location(Name)).Entity; db.SaveChanges(); return(location); } }
/// <summary> /// Validates console input to instantiate a new user and insert it into the database. /// </summary> /// <returns> /// null if console input fails validation. /// User object if new location inserted into database. /// </returns> public User Build(int userTypeId) { using (var db = new CustomerApplicationContext()) { var userType = db.UserTypes.Find(userTypeId); Console.WriteLine($"Creating a New ({userType.Name}) User:\n"); FirstNameInput(); LastNameInput(); PasswordInput(); var user = db.Users.Add(new User(FirstName, LastName, Password, userType)).Entity; db.SaveChanges(); return(user); } }
public void AddUserTypeToDatabase() { var options = new DbContextOptionsBuilder <CustomerApplicationContext>() .UseInMemoryDatabase(databaseName: "AddUserTypeToDatabase") .Options; using (var db = new CustomerApplicationContext(options)) { db.UserTypes.Add(new UserType { Description = "Administrator user. Access to tools to create new users, locations and products. Able to manage inventories.", Name = "Admin" }); db.SaveChanges(); } using (var db = new CustomerApplicationContext(options)) { Assert.Single(db.UserTypes); } }
/// <summary> /// Finds and removes a <c>Product</c> from the database. /// Removes by Id. /// </summary> /// <param name="productId">Id of the product object to be deleted from the database.</param> /// <param name="locationId">Id of the current location, used for validation.</param> /// <returns>Returns the Product object removed.</returns> public Product RemoveProduct(int productId, int locationId) { using (var db = new CustomerApplicationContext()) { //Loads current location from database. var location = db.Locations.Find(locationId); //Load product which match the input id. var product = db.Products.Find(productId); //Checks the product references the current location. if (product.Location.Id != locationId) { throw new Exception(); } //Deletes product from current location. db.Products.Remove(product); db.SaveChanges(); return(product); } }