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);
            }
        }
Ejemplo n.º 2
0
        /// <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);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Loads <c>User</c> from database by id.
 /// </summary>
 /// <param name="id">Id of the user being loaded.</param>
 /// <returns>User object.</returns>
 public User GetUser(int id)
 {
     using (var db = new CustomerApplicationContext())
     {
         return(db.Users.Find(id));
     }
 }
        /// <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);
            }
        }
        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 Location GetLocation(int id)
 {
     using (var db = new CustomerApplicationContext())
     {
         return(db.Locations.Find(id));
     }
 }
 public Product GetProduct(int id)
 {
     using (var db = new CustomerApplicationContext())
     {
         return(db.Products.Find(id));
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// User login with id and password.
 /// </summary>
 /// <param name="userId">Id of the user logging in.</param>
 /// <param name="password">Password of the user.</param>
 /// <returns>User object if login successful.</returns>
 public User Login(String userId, String password)
 {
     using (var db = new CustomerApplicationContext())
     {
         var userIdInt = Int32.Parse(userId);
         return(db.Users.Include(user => user.UserType)
                .Single(user => user.Id == userIdInt && user.Password == password));
     }
 }
 /// <summary>
 /// Loads all <c>Location</c> from the database.
 /// </summary>
 /// <returns>
 /// Collection of <c>Location</c>.
 /// </returns>
 public ICollection <Location> GetLocations()
 {
     using (var db = new CustomerApplicationContext())
     {
         return(db.Locations
                .AsNoTracking()
                .ToList());
     }
 }
 /// <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);
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Loads users from database that match input first name and last name.
 /// </summary>
 /// <param name="firstName">User first name for search.</param>
 /// <param name="lastName">User last name for search.</param>
 /// <returns>Collection of users matching search query.</param>
 public ICollection <User> SearchByName(string firstName, string lastName)
 {
     using (var db = new CustomerApplicationContext())
     {
         return(db.Users
                .AsNoTracking()
                .Where(user => user.FirstName == firstName && user.LastName == lastName)
                .Include(user => user.UserType)
                .ToList());
     }
 }
 /// <summary>
 /// Loads all <c>Product</c> at <c>Location</c>.
 /// </summary>
 /// <param name="locationId">The id of the location being loaded.</param>
 /// <returns>Collection of products.</returns>
 public ICollection <Product> GetProducts(int locationId)
 {
     using (var db = new CustomerApplicationContext())
     {
         db.Locations.Find(locationId);
         return(db.Products
                .AsNoTracking()
                .Where(product => product.Location.Id == locationId)
                .Include(product => product.Location)
                .ToList());
     }
 }
 /// <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);
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Loads <c>Order</c> from database which references the <c>User</c>
 /// </summary>
 /// <param name="userId">The id of the user whose orders are being loaded.</param>
 /// <returns>Collection of the user's orders.</returns>
 public ICollection <Order> GetUserHistory(int userId)
 {
     using (var db = new CustomerApplicationContext())
     {
         var user = db.Users.Find(userId);
         return(db.Orders
                .AsNoTracking()
                .Where(order => order.User.Id == userId)
                .Include(order => order.Product)
                .ThenInclude(product => product.Location)
                .ToList());
     }
 }
Ejemplo n.º 15
0
 /// <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);
     }
 }
        /// <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);
            }
        }
        public void AddLocationToDatabase()
        {
            var options = new DbContextOptionsBuilder <CustomerApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "AddLocationToDatabase")
                          .Options;

            using (var db = new CustomerApplicationContext(options))
            {
                db.Locations.Add(new Location("Some Place"));
                db.SaveChanges();
            }

            using (var db = new CustomerApplicationContext(options))
            {
                Assert.Single(db.Locations);
            }
        }
        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);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Loads <c>Order</c> from database which references the <c>Lcation</c>
        /// </summary>
        /// <param name="userId">The id of the location which orders are being loaded.</param>
        /// <returns>Collection of the location's orders.</returns>
        public ICollection <Order> GetLocationHistory(int locationId)
        {
            using (var db = new CustomerApplicationContext())
            {
                //lLoads location from database.
                var location = db.Locations.Find(locationId);

                //Loads products from database which reference the location.
                var products = db.Products
                               .AsNoTracking()
                               .Where(product => product.Location.Id == locationId)
                               .ToList();

                //Load orders from database which reference the location's products.
                return(db.Orders
                       .AsNoTracking()
                       .Where(order => order.Product.Location.Id == locationId)
                       .Include(order => order.Product)
                       .ThenInclude(product => product.Location)
                       .ToList());
            }
        }
        /// <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);
            }
        }