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); } }
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)); }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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)); }
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)); }
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)); } }
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); } }
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)); }
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); } }
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)); }