예제 #1
0
        public void GetOrderDetailTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "GetOrderDetailTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Store location = new Store
                {
                    Location = "Maryland"
                };

                db.Add(location);
                db.SaveChanges();

                Customer customer = new Customer
                {
                    FirstName = "Michael",
                    LastName  = "Hall",
                    UserName  = "******",
                    Password  = "******"
                };

                db.Add(customer);
                db.SaveChanges();

                Product product = new Product
                {
                    StoreId     = 1,
                    ProductName = "bar",
                    Inventory   = 5,
                    Price       = 10
                };

                db.Add(product);
                db.SaveChanges();

                Order order = new Order
                {
                    CustomerId = 1,
                    ProductId  = 1,
                    Quantity   = 3,
                };

                db.Add(order);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                var repo      = new OrderRepo();
                var orderTest = repo.GetOrderDetails(context, 1);

                Assert.Equal(1, orderTest.Id);
            }
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("Id,ProductId,CustomerId,Quantity,Timestamp")] Order order)
        {
            var check    = new OrderLogic();
            var products = new ProductRepo();

            if (ModelState.IsValid && check.IsWithinInventory(products.GetInventory(_context, order.ProductId), order.Quantity))
            {
                try
                {
                    products.UpdateInventory(_context, order.ProductId, order.Quantity);
                    _context.Add(order);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception ex)
                {
                    _logger.LogInformation(ex, "Something in the order wasn't able to be added");
                }
            }
            ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "UserName");

            var repo = new OrderRepo();

            ViewData["ProductInfo"] = repo.ProductList(_context);
            return(View(order));
        }
예제 #3
0
        public void AddsCustomerToDbTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "AddsCustomerToDbTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Customer location = new Customer
                {
                    FirstName = "Michael",
                    LastName  = "Hall",
                    UserName  = "******",
                    Password  = "******"
                };

                db.Add(location);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                Assert.Equal(1, context.Customers.Count());

                var customer1 = context.Customers.Where(c => c.Id == 1).FirstOrDefault();
                Assert.Equal(1, customer1.Id);
                Assert.Equal("Michael", customer1.FirstName);
                Assert.Equal("Hall", customer1.LastName);
                Assert.Equal("mbhall", customer1.UserName);
                Assert.Equal("yes", customer1.Password);
            }
        }
예제 #4
0
        public void GetStoresTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "GetStoresTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Store location = new Store
                {
                    Location = "Maryland"
                };

                db.Add(location);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                var repo     = new StoreRepo();
                var products = repo.GetStores(context);
                Assert.Equal(1, products.Count());

                var store1 = context.Stores.Where(s => s.Id == 1).FirstOrDefault();
                Assert.Equal(1, store1.Id);
                Assert.Equal("Maryland", store1.Location);
            }
        }
예제 #5
0
        public void UpdateInventoryTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "UpdateInventoryTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Product product = new Product
                {
                    StoreId     = 1,
                    ProductName = "bar",
                    Inventory   = 5,
                    Price       = 10
                };

                db.Add(product);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                var repo     = new ProductRepo();
                var product1 = context.Products.Where(p => p.StoreId == 1).FirstOrDefault();

                Assert.Equal(5, product1.Inventory);

                repo.UpdateInventory(context, 1, 3);
                Assert.Equal(2, product1.Inventory);
            }
        }
예제 #6
0
        public void GetInventoryTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "GetInventoryTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Product product = new Product
                {
                    StoreId     = 1,
                    ProductName = "bar",
                    Inventory   = 5,
                    Price       = 10
                };

                db.Add(product);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                var repo      = new ProductRepo();
                int inventory = repo.GetInventory(context, 1);

                Assert.Equal(5, inventory);
            }
        }
예제 #7
0
        public void GetOrdersTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "GetOrdersTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Order location = new Order
                {
                    CustomerId = 5,
                    ProductId  = 10,
                    Quantity   = 3,
                };

                db.Add(location);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                var repo   = new OrderRepo();
                var orders = repo.GetOrders(context);
                Assert.Equal(1, orders.Count());

                var order1 = context.Orders.Where(o => o.Id == 1).FirstOrDefault();
                Assert.Equal(1, order1.Id);
                Assert.Equal(5, order1.CustomerId);
                Assert.Equal(10, order1.ProductId);
                Assert.Equal(3, order1.Quantity);
            }
        }
예제 #8
0
        public void AddsProductToDbTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "AddsProductToDbTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Product bar = new Product
                {
                    StoreId     = 7,
                    ProductName = "bar",
                    Inventory   = 5,
                    Price       = 10
                };

                db.Add(bar);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                Assert.Equal(1, context.Products.Count());

                var product1 = context.Products.Where(p => p.StoreId == 7).FirstOrDefault();
                Assert.Equal(7, product1.StoreId);
                Assert.Equal(1, product1.Id);
            }
        }
예제 #9
0
 public IActionResult CreateOrder([FromBody] Order order)
 {
     if (ModelState.IsValid)
     {
         order.OrderId       = 0;
         order.Shipped       = false;
         order.Payment.Total = GetPrice(order.Products);
         ProcessPayment(order.Payment);
         if (order.Payment.AuthCode != null)
         {
             context.Add(order);
             context.SaveChanges();
             return(Ok(new {
                 orderId = order.OrderId,
                 authCode = order.Payment.AuthCode,
                 amount = order.Payment.Total
             }));
         }
         else
         {
             return(BadRequest("Payment rejected"));
         }
     }
     return(BadRequest(ModelState));
 }
예제 #10
0
        public async Task <IActionResult> Create([Bind("PermissionID,Title,Description")] Permission permission)
        {
            if (ModelState.IsValid)
            {
                _context.Add(permission);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(permission));
        }
예제 #11
0
 public IActionResult CreateSupplier([FromBody] SupplierData sdata)
 {
     if (ModelState.IsValid)
     {
         Supplier s = sdata.Supplier;
         context.Add(s);
         context.SaveChanges();
         return(Ok(s.SupplierId));
     }
     else
     {
         return(BadRequest(ModelState));
     }
 }
예제 #12
0
        public void GetProductDataTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "GetProductDataTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Store location = new Store
                {
                    Location = "Maryland"
                };

                db.Add(location);
                db.SaveChanges();

                Product product = new Product
                {
                    StoreId     = 1,
                    ProductName = "bar",
                    Inventory   = 5,
                    Price       = 10
                };

                db.Add(product);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                var repo     = new ProductRepo();
                var product1 = repo.GetProductData(context);
            }
        }
예제 #13
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,UserName,Password")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Add(customer);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception ex)
                {
                    _logger.LogInformation(ex, "Wasn't able to create the customer.");
                }
            }
            return(View(customer));
        }
예제 #14
0
        public void SearchLastNameTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <StoreAppContext>()
                          .UseInMemoryDatabase(databaseName: "SearchLastNameTest")
                          .Options;

            //Act
            using (var db = new StoreAppContext(options))
            {
                Customer location = new Customer
                {
                    FirstName = "Michael",
                    LastName  = "Hall",
                    UserName  = "******",
                    Password  = "******"
                };

                db.Add(location);
                db.SaveChanges();
            }

            //Assert
            using (var context = new StoreAppContext(options))
            {
                var repo      = new CustomerRepo();
                var customers = repo.GetCustomers(context);
                var filter    = repo.SearchLastName(customers, "Ha");
                Assert.Equal(1, filter.Count());

                var customer1 = filter.Where(c => c.Id == 1).FirstOrDefault();
                Assert.Equal(1, customer1.Id);
                Assert.Equal("Michael", customer1.FirstName);
                Assert.Equal("Hall", customer1.LastName);
                Assert.Equal("mbhall", customer1.UserName);
                Assert.Equal("yes", customer1.Password);
            }
        }
예제 #15
0
        public async Task <IActionResult> Create([Bind("Name,Address,Nationality")] User user, string[] selectedPermissions)
        {
            if (selectedPermissions != null)
            {
                user.UserPermissions = new List <UserPermission>();
                foreach (var perm in selectedPermissions)
                {
                    var permissionToAdd = new UserPermission {
                        UserID = user.ID, PermissionID = int.Parse(perm)
                    };
                    user.UserPermissions.Add(permissionToAdd);
                }
            }
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            PopulateUserPermissionData(user);
            return(View(user));
        }