// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, StoreAppContext storeAppContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); app.UseExceptionHandler(builder => { builder.Run(async context => { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; var error = context.Features.Get <IExceptionHandlerFeature> (); if (error != null) { context.Response.AddApplicationError(error.Error.Message); await context.Response.WriteAsync(error.Error.Message); } }); }); } app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()); //app.UseHttpsRedirection(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(); StoreDbContextExtensions.CreateSeedData(storeAppContext); }
/// <summary> /// Gets store orders by store id from database /// </summary> /// <param name="id">Store id</param> /// <returns>A list of order objects the store has received if any in database or an empty list if none found in database</returns> public List <Order> GetOrdersByStoreId(int id) { using var context = new StoreAppContext(_contextOptions); List <OrderDetail> dbOrders; List <Order> matchingOrders = new List <Order>(); try { dbOrders = context.OrderDetails .Include(o => o.OrderProducts) .Where(o => o.LocationId == id).ToList(); } catch (InvalidOperationException) { return(matchingOrders); } foreach (var order in dbOrders) { Dictionary <int, int> products = new Dictionary <int, int>(); foreach (var product in order.OrderProducts) { products.Add(product.ProductId, product.Quantity); } matchingOrders.Add(new Order(order.Id, order.LocationId, order.CustomerId, order.Date, order.Total, products)); } return(matchingOrders); }
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 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 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 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); } }
[Fact]//the name of the function should tell the user what the function is doing public void CreateCustomerSavesANewPlayerToTheDb() { // arrange // Creating the in-memory Db var options = new DbContextOptionsBuilder <StoreAppContext>() .UseInMemoryDatabase(databaseName: "TestDb").Options; // act // add to the In-memory Db Customer p1 = new Customer(); using (var context = new StoreAppContext(options)) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); StoreAppRepsitoryLayer repo = new StoreAppRepsitoryLayer(context); p1 = repo.CreateCustomer("sparky", "jones"); context.SaveChanges(); } // assert // verify the results was as expected using (var context = new StoreAppContext(options)) { StoreAppRepsitoryLayer repo = new StoreAppRepsitoryLayer(context); Customer result = repo.CreateCustomer("sparky", "jones"); Assert.True(p1.Customer_Id.Equals(result.Customer_Id)); } }
/// <summary> /// Get the inventory for a particular product found /// using the incoming id /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <returns></returns> public int GetInventory(StoreAppContext context, int id) { return(context.Products .Where(p => p.Id == id) .Select(p => p.Inventory) .SingleOrDefault()); }
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); } }
/// <summary> /// Retrives all stores available in databases /// </summary> /// <returns>List of library store objects available in database or empty list if none</returns> public List <Store> GetAllStores() { using var context = new StoreAppContext(_contextOptions); List <Store> matchingStores = new List <Store>(); List <StoreTable> dbStores; try { dbStores = context.StoreTables .Include(s => s.Inventories) .ToList(); } catch (InvalidOperationException) { return(matchingStores); } foreach (var store in dbStores) { Dictionary <int, int> inventory = new Dictionary <int, int>(); foreach (var product in store.Inventories) { inventory.Add(product.ProductId, product.Stock); } matchingStores.Add(new Store(store.Id, store.Name, inventory)); } return(matchingStores); }
public TestDataFixture() { var builder = new DbContextOptionsBuilder <StoreAppContext>() .UseInMemoryDatabase("DemoData"); Context = new StoreAppContext(builder.Options); }
/// <summary> /// Retrieves store by name from database /// </summary> /// <param name="name">Store name</param> /// <returns>Library store object if exists in database or null if non existent</returns> public Store GetStoreByName(string name) { using var context = new StoreAppContext(_contextOptions); StoreTable dbStore; try { dbStore = context.StoreTables .Include(s => s.Inventories) .First(s => s.Name == name); } catch (InvalidOperationException) { return(null); } Dictionary <int, int> inventory = new Dictionary <int, int>(); foreach (var product in dbStore.Inventories) { inventory.Add(product.ProductId, product.Stock); } return(new Store(dbStore.Id, dbStore.Name, inventory)); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, StoreAppContext storeAppContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseDeveloperExceptionPage(); /*app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions * { * HotModuleReplacement = true * });*/ app.UseCors("MyPolicy"); app.UseCookiePolicy(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseAuthentication(); app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); storeAppContext.CreateSeedData(); app.UseIdentity(); // IdentitySeedData.SeedDataBaseAsync(app); }
/// <summary> /// Get all the product/store data from the DB /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task <IEnumerable <Product> > GetProductData(StoreAppContext context) { return(await context.Products .Include(p => p.Store) .OrderBy(p => p.Store.Id) .ToListAsync()); }
/// <summary> /// Get all the order/customer/product data for a given order /// using the incoming id /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <returns></returns> public async Task <Order> GetOrderDetails(StoreAppContext context, int id) { return(await context.Orders .Where(o => o.Id == id) .Include(o => o.Customer) .Include(o => o.Product) .ThenInclude(o => o.Store) .FirstOrDefaultAsync()); }
/// <summary> /// Updates the inventory for a particular product /// based on the quantity that a recent order has purchased /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <param name="quant"></param> public void UpdateInventory(StoreAppContext context, int id, int quant) { var product = context.Products .Where(p => p.Id == id) .FirstOrDefault(); product.Inventory -= quant; context.SaveChanges(); }
/// <summary> /// Gets all the order data including foreign key data /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task <IEnumerable <Order> > GetOrderData(StoreAppContext context) { return(await context.Orders .Include(o => o.Customer) .Include(o => o.Product) .ThenInclude(p => p.Store) .OrderBy(o => o.Timestamp) .ToListAsync()); }
/// <summary> /// Retrieves all customers from database /// </summary> /// <returns>List of library customer objects</returns> public List <Customer> GetAllCustomers() { using var context = new StoreAppContext(_contextOptions); var dbCustomers = context.CustomerTables.ToList(); var customers = dbCustomers.Select(c => new Customer(c.Id, c.FirstName, c.LastName, c.Phone)).ToList(); return(customers); }
/// <summary> /// Get all the order/customer/product/store data needed to display /// the history of the customer matching the incoming id /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <returns></returns> public async Task <IEnumerable <Order> > GetCustomerHistory(StoreAppContext context, int id) { return(await context.Orders .Where(o => o.Customer.Id == id) .Include(o => o.Customer) .Include(o => o.Product) .ThenInclude(p => p.Store) .OrderBy(o => o.Timestamp) .ToListAsync()); }
public HomeMenu(StoreAppContext context, IManagerRepoActions managerRepo, ICustomerRepoActions customerRepo, ILocationRepoActions locationRepo, IBaseballBatRepoActions baseballBatRepo, IOrderRepoActions orderRepo, IInventoryRepoActions inventoryRepo) { this.context = context; this.managerRepo = managerRepo; this.customerRepo = customerRepo; this.locationRepo = locationRepo; this.baseballBatRepo = baseballBatRepo; this.orderRepo = orderRepo; this.inventoryRepo = inventoryRepo; }
public string GetNewCustomerUsername(StoreAppContext context) { string newUsername = null; foreach (Entities.Customer cust in context.Customer) { newUsername = cust.Username; } return(newUsername); }
public bool UsernameParser(string username, StoreAppContext context) { foreach (StoreApp.Library.Entities.Customer cust in context.Customer) { if (cust.Username == username) { return(true); } } return(false); }
public void PlaceOrder(Order LogicOrder, StoreAppContext context) { try { context.Orders.Add(parser.LogicOrderToContextOrder(LogicOrder)); context.SaveChanges(); } catch (Microsoft.EntityFrameworkCore.DbUpdateException e) { Console.WriteLine("Something went wrong inputting order: " + e); } }
public SignupMenu(StoreAppContext context, IManagerRepoActions managerRepoActions, ICustomerRepoActions customerRepoActions, ILocationRepoActions locationRepoActions, IBaseballBatRepoActions baseballBatRepoActions, IOrderRepoActions orderRepoActions, IInventoryRepoActions inventoryRepoActions) { this.context = context; this.customerRepoActions = customerRepoActions; this.managerRepoActions = managerRepoActions; this.locationRepoActions = locationRepoActions; this.inventoryRepoActions = inventoryRepoActions; this.baseballBatRepoActions = baseballBatRepoActions; this.orderRepoActions = orderRepoActions; this.customerActions = new CustomerActions(context, customerRepoActions); }
/// <summary> /// Creates a product list to store in the select input form /// </summary> /// <param name="context"></param> /// <returns></returns> public IEnumerable <SelectListItem> ProductList(StoreAppContext context) { List <SelectListItem> products = new List <SelectListItem>(); foreach (var product in context.Products.Include(p => p.Store)) { products.Add(new SelectListItem { Text = $"{product.Store.Location } | {product.ProductName} | Inventory: {product.Inventory} @ ${product.Price} each", Value = $"{product.Id}" }); } return(products); }
public SysUserController(ISysUserRepository sysUserRpt, ISysRoleUserRepository sysRoleUserRpt, ISysRoleRepository sysRoleRpt, ISysOrgRepository orgRepository, StoreAppContext context, IMapper mapper) { _sysUserRpt = sysUserRpt; _sysRoleUserRpt = sysRoleUserRpt; _sysRoleRpt = sysRoleRpt; _context = context; _mapper = mapper; _orgRepository = orgRepository; }
public ManagerMainMenu(StoreAppContext context, IManagerRepoActions managerRepoActions, ILocationRepoActions locationRepoActions, IInventoryRepoActions inventoryRepoActions, IOrderRepoActions orderRepoActions, IBaseballBatRepoActions batRepoActions, Location location) { this.context = context; this.managerRepoActions = managerRepoActions; this.inventoryRepoActions = inventoryRepoActions; this.orderRepoActions = orderRepoActions; this.batRepoActions = batRepoActions; this.managerActions = new ManagerActions(context, managerRepoActions); this.inventoryActions = new InventoryActions(context, inventoryRepoActions); this.orderActions = new OrderActions(context, orderRepoActions); this.batActions = new BaseballBatActions(context, batRepoActions); this.location = location; }
/// <summary> /// Retrieves customer my first and last name from database /// </summary> /// <param name="first">Customer First Name</param> /// <param name="last">Customer Last Name</param> /// <returns>Library customer object if found null otherwise</returns> public Customer GetCustomerByFirstAndLastName(string first, string last) { using var context = new StoreAppContext(_contextOptions); CustomerTable dbCustomer; try { dbCustomer = context.CustomerTables.First(c => c.FirstName == first && c.LastName == last); } catch (InvalidOperationException) { return(null); } return(new Customer(dbCustomer.Id, dbCustomer.FirstName, dbCustomer.LastName, dbCustomer.Phone)); }