public async Task DeletesOrderFromDb() { //Arrange - create an object to configure your inmemory DB. var options = new DbContextOptionsBuilder <MSDbContext>() .UseInMemoryDatabase(databaseName: "DeletesOrderFromDb") .Options; //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext using (var db = new MSDbContext(options)) { Order order = new Order { Id = 43, StoreId = 4 }; db.Add(order); await db.SaveChangesAsync(); db.Remove(order); await db.SaveChangesAsync(); } //Assert using (var context = new MSDbContext(options)) { Assert.Equal(0, context.Orders.Count()); } }
public async Task DeletesCustomerFromDb() { //Arrange - create an object to configure your inmemory DB. var options = new DbContextOptionsBuilder <MSDbContext>() .UseInMemoryDatabase(databaseName: "DeletesCustomerToDb") .Options; //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext using (var db = new MSDbContext(options)) { CustomerAddress customerAddress = new CustomerAddress { Id = 1, Street = "8286 Clay Ave.", City = "Spokane", State = "WA", Zip = "11111" }; Customer customer = new Customer { Id = 6, FirstName = "Maribeth", LastName = "Fontenot", Email = "*****@*****.**", PhoneNo = "1234112233", Password = "******", UserTypeId = 2, CustomerAddress = customerAddress }; db.Add(customer); await db.SaveChangesAsync(); db.Remove(customer); await db.SaveChangesAsync(); } //Assert using (var context = new MSDbContext(options)) { Assert.Equal(0, context.Customers.Count()); } }
public async Task DeletesProductFromDb() { //Arrange - create an object to configure your inmemory DB. var options = new DbContextOptionsBuilder <MSDbContext>() .UseInMemoryDatabase(databaseName: "DeletesProductFromDb") .Options; //Act - send in the configure object to the DbContext constructor to be used in configuring the DbContext using (var db = new MSDbContext(options)) { Product product = new Product { Id = 26, Name = "drum", }; db.Add(product); await db.SaveChangesAsync(); db.Remove(product); await db.SaveChangesAsync(); } //Assert using (var context = new MSDbContext(options)) { Assert.Equal(0, context.Products.Count()); } }
public async Task <IActionResult> Create([Bind("Id,Name,CountryId,Status,Remarks")] District mSDistrict) { if (ModelState.IsValid) { _context.Add(mSDistrict); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(mSDistrict)); }
/// <summary> /// Add Entity of type T to the database. /// </summary> /// <param name="entity"></param> /// <returns></returns> public async Task AddAsync(T entity) { try { _context.Set <T>().Add(entity); await _context.SaveChangesAsync(); } catch (Exception) { throw; } }
public async Task <IActionResult> Create([Bind("Id,Name,ProductCode,Price")] Product product) { if (ModelState.IsValid) { ViewBag.UserName = User.FindFirstValue(ClaimTypes.Name); ViewBag.StoreId = User.FindFirstValue(ClaimTypes.NameIdentifier); _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task GetsAllLocations() { //Arrange var options = InMemoryDb("GetsAllLocations"); //Act using (var context = new MSDbContext(options)) { var store = new Store { Name = "Florida" }; context.Add(store); store = new Store { Name = "Texas" }; context.Add(store); store = new Store { Name = "Washington" }; context.Add(store); await context.SaveChangesAsync(); } //Assert using (var context = new MSDbContext(options)) { var stores = context.Stores.Select(x => x); //var stores = await storeRepo.(); Assert.Equal(4, stores.Count()); } }
/// <summary> /// Adds inventory to the database. /// </summary> /// <param name="inventoryItemToUpdate"></param> /// <returns></returns> public async Task AddInventoryItems(InventoryItem inventoryItemToUpdate) { try { _context.Inventory.Update(inventoryItemToUpdate); await _context.SaveChangesAsync(); } catch (Exception) { throw; } }
/// <summary> /// Creates order /// </summary> /// <param name="order"></param> /// <returns></returns> public async Task CreateOrder(Order order) { try { order.OrderDate = DateTime.Now; _context.Orders.Add(order); var shoppinCartItems = _shoppingCart.ShoppingCartItems; foreach (var item in shoppinCartItems) { var orderLineItem = new OrderLineItem { Order = order, Quantity = item.Quantity, InventoryItemId = item.Product.Id * order.StoreId, Price = item.Product.Price }; var inventoryItems = _context.Inventory .Include(i => i.Product) .Include(i => i.Store) .AsNoTracking() .Where(i => i.StoreId == order.StoreId && i.ProductId == item.Product.Id).FirstOrDefault(); var newQuantity = inventoryItems.Quantity - item.Quantity; var inventoryToUpdate = new InventoryItem { Id = inventoryItems.Id, ProductId = item.Product.Id, StoreId = order.StoreId, Quantity = newQuantity, ChangedDate = DateTimeOffset.Now, LoggedUserId = order.CustomerId }; _context.OrderLineItems.Add(orderLineItem); _context.Update(inventoryToUpdate); } await _context.SaveChangesAsync(); } catch (Exception) { throw; } }