public void RemoveFromStock(int quantity, GamesImp game, StoreImp store) { var value = _db.Inventory.First(i => i.GameId == game.Id && i.StoreId == store.IDNumber); value.GameRemaining -= quantity; _db.SaveChanges(); }
public void AddInventory(string productName, int locationId, int quantity) { // get the location iventories with all the product details var dbLocation = _context.Locations .Include(l => l.Inventories).ThenInclude(i => i.Product) .FirstOrDefault(l => l.Id == locationId).Inventories.FirstOrDefault(i => i.Product.Name == productName); if (dbLocation == null) { // means the store doesn't have this item in its inventory var location = _context.Locations.First(l => l.Id == locationId); var product = _context.Products.First(p => p.Name == productName); location.Inventories.Add(new Inventory() { ProductId = product.Id, LocationId = locationId, Quantity = 0 }); _context.Update(location); _context.SaveChanges(); dbLocation = _context.Locations .Include(l => l.Inventories).ThenInclude(i => i.Product) .FirstOrDefault(l => l.Id == locationId).Inventories.FirstOrDefault(i => i.Product.Name == productName); } dbLocation.Quantity += quantity; _context.Update(dbLocation); _context.SaveChanges(); }
/************************************** * CUSTOMER FUNCTIONS BELOW * *************************************/ ///<summary> ///takes a context and Customer. Returns a true bool to indicate insertion was successfull /// </summary> public CustomerViewModel AddCustomer(Customer customer) { //check of the context has the customer by name if (!(_dbContext.Customers.Any(c => c.CustomerFirstName == customer.CustomerFirstName && c.CustomerLastName == customer.CustomerLastName))) { //if customer is NOT found,,, add the customer Customers entity = Mapper.MapCustomer(customer); _dbContext.Customers.Add(entity);//maybe the context still has the first wrong Customer? } else { Console.WriteLine("\tThere is already an account with that name.\n\tPlease try again with a different name."); return(null); } try { //save changes _dbContext.SaveChanges(); } catch (DbUpdateException ex) { Console.WriteLine(ex.Message); Console.WriteLine("There was an error Adding your account. Please try again with a different name."); _logger.LogError(ex, "Unable to update DB at {time}", DateTime.UtcNow); return(null); } //no errors till now so send the same name into Read Customer to get the new customer from the DB return(ReadCustomer(customer)); }
public void AddCustomer(CustomerImp _customer) { var value = Mapper.Map(_customer); _db.Add(value); _db.SaveChanges(); _customer.Id = value.CustomerId; }
public void AddOrder(OrderImp order) { var value = Mapper.Map(order); _db.Add(value); _db.SaveChanges(); order.OrderID = value.OrderId; }
public void Create(Library.Customer customer) { var cust = new Customer() { FirstName = customer.FirstName, LastName = customer.LastName }; _context.Add(cust); _context.SaveChanges(); }
/// <summary> /// Add a customer to SQL database /// </summary> public void AddCustomer(ConsoleApp.Library.Customer customer) { var dbCustomer = new Customer() { Name = customer.Name, Id = customer.Id, }; _dbContext.Customers.Add(dbCustomer); _dbContext.SaveChanges(); }
public void Add(Library.Product product) { Product dbProduct = new Product() { Name = product.Name, Description = product.Description, Price = product.Price, OrderLimit = product.OrderLimit }; _context.Add(dbProduct); _context.SaveChanges(); }
/// <summary> /// Adds a given quantity of a product to a locaion /// </summary> /// <param name="location">The location to add too</param> /// <param name="product">The product to add</param> /// <param name="quantity">amount to add</param> /// <returns></returns> public bool AddLocationInventory(Location location, DatabaseModels.Product product, int quantity) { // set up context using var context = new Project0Context(_dbContext); // make the new inventory var inventory = new DatabaseModels.Inventory { LocationId = location.Id, Quantity = quantity, ProductId = product.Id }; context.Inventories.Add(inventory); // ensure that the save works successfully try { context.SaveChanges(); } catch (DbUpdateException) { return(false); } return(true); }
public void Add(CustomerOrder order) { using var context = new Project0Context(mOptions); context.CustomerOrder.Add(order); context.SaveChanges(); }
public CustomerOrderRepositoryTest() { mContext = new Project0Context(new DbContextOptionsBuilder <Project0Context> () .UseInMemoryDatabase(databaseName: "CustomerOrderDatabase") .Options); mContext.Database.EnsureDeleted(); mContext.CustomerOrder.Add(new CustomerOrder { CustomerId = 1, StoreId = 1, Customer = new Customer { Id = 1 }, Store = new Store { Id = 1 }, OrderLine = new List <OrderLine> { new OrderLine { Product = new Product() } } }); mContext.SaveChanges(); }
public Customer AddCustomer() { var cust1 = new Customer(); try { Console.Write("What is your first name: "); cust1.FirstName = Console.ReadLine(); Console.WriteLine(); Console.Write("What is your last name: "); cust1.LastName = Console.ReadLine(); Console.WriteLine(); UserNamePass(); } catch (FormatException) { Console.WriteLine("You didn't the connect type"); AddCustomer(); } ctx.Add(cust1); try { ctx.SaveChanges(); Console.WriteLine("Added to Database."); } catch (Exception) { Console.WriteLine("Save didn't work."); } return(cust1); }
public void GetLocationsWorks() { // arrange var options = new DbContextOptionsBuilder <Project0Context>().UseInMemoryDatabase("get_locations_test").Options; using (var db = new Project0Context(options)) { db.Location.Add(new DataAccess.Location { Name = "a" }); db.Location.Add(new DataAccess.Location { Name = "b" }); db.SaveChanges(); } List <Library.Location> locations = new List <Library.Location>(); using (var db = new Project0Context(options)) { //nothing var repo = new DataRepository(db); locations = repo.GetLocations(); } Assert.Equal("a", locations[0].Name); Assert.Equal("b", locations[1].Name); // assert // (no exception should have been thrown) }
public void GetUsersWorks() { // arrange var options = new DbContextOptionsBuilder <Project0Context>().UseInMemoryDatabase("get_users_test").Options; using (var db = new Project0Context(options)) { db.User.Add(new DataAccess.User { FirstName = "a", LastName = "b" }); db.User.Add(new DataAccess.User { FirstName = "c", LastName = "d" }); db.SaveChanges(); } List <Library.User> users = new List <Library.User>(); using (var db = new Project0Context(options)) { //nothing var repo = new DataRepository(db); users = repo.GetUsers(); } Assert.Equal("a", users[0].FirstName); Assert.Equal("b", users[0].LastName); Assert.Equal("c", users[1].FirstName); Assert.Equal("d", users[1].LastName); }
/// <summary> /// Adds an order to the database /// </summary> /// <param name="customer">Customer that placed order</param> /// <param name="location">Location that was ordered from</param> /// <param name="sales">List of sales that the customer placed</param> public void AddOrder(Customer customer, Location location, ICollection <Sale> sales) { // get the context of the db using var context = new Project0Context(_dbContext); // create list converting from Library.Sale to DatabaseModel.Sale var dbSales = new List <DatabaseModels.Sale>(); decimal orderTotal = 0.0m; foreach (var item in sales) { // need the product details var dbProduct = context.Products.First(p => p.Id == item.ProductId); var dbSale = new DatabaseModels.Sale() { ProductId = item.ProductId, ProductName = dbProduct.Name, PurchasePrice = dbProduct.Price, Quantity = item.SaleQuantity, }; // add to the sum of the order total orderTotal += item.SaleQuantity * dbProduct.Price; // add the sale to the running list dbSales.Add(dbSale); // remove the amount from the inventory of the store var locationInventory = context.Inventories.First(i => i.LocationId == location.Id && i.ProductId == item.ProductId); locationInventory.Quantity -= item.SaleQuantity; context.Inventories.Update(locationInventory); context.SaveChanges(); } // create the classes var order = new DatabaseModels.Order() { CustomerId = customer.Id, LocationId = location.Id, Date = DateTime.Now, Sales = dbSales }; // calculate total order.OrderTotal = orderTotal; context.Orders.Add(order); context.SaveChanges(); }
public void AddGame(GamesImp game) { var value = Mapper.Map(game); _db.Add(value); _db.SaveChanges(); game.Id = value.GameId; }
static void RemoveCustomer(string email) { using var context = new Project0Context(s_dbContextOptions); var customer = GetCustomerByEmail(email); context.Customers.Remove(customer); context.SaveChanges(); }
static void Main(string[] args) { var Screen = new Program().GetScreenSelection(); using var context = new Project0Context(Options); if (Screen == 'e') { var employee = context.Employees.Select(c => new { c.EmployeeId, c.FirstName, c.LastName }).ToList(); var empId = context.Employees.Select(c => c.EmployeeId).ToList(); Console.WriteLine("Please enter your employee id:"); string IdEntered = Console.ReadLine(); if (empId.Contains(Int32.Parse(IdEntered))) { Console.WriteLine($"Welcome " + IdEntered); Console.WriteLine("Please select the function you would like to use:\n" + "1: Add New Customer.\n" + "2: Search For a Customer by (Last) Name\n" + "3: Search For a Customer by Id Number\n" + "4: Place an Order for a Customer\n" + "4: Display Order History for a Store\n" + "5: Display the Company Directory"); int menuChoice = Int32.Parse(Console.ReadLine()); Console.WriteLine(menuChoice); var Customer = new CustomerRepository(); switch (menuChoice) { case 1: Customer.InsertCustomer(); break; case 2: Customer.GetCustomerbyLastName(); break; default: Console.WriteLine("Error"); break; } //var emp = context.Employees // .Select(c => new { c.LastName, c.FirstName, c.EmployeeId }) // .ToList(); //foreach (var x in emp) // Console.WriteLine(x); } context.SaveChanges(); Console.WriteLine("Program Completed."); } }
static void UpdateCustomerName(int id, string first, string last) { using var context = new Project0Context(s_dbContextOptions); var customer = GetCustomerById(id); customer.FirstName = first; customer.LastName = last; context.Customers.Update(customer); context.SaveChanges(); }
public void UpdateStoreId(int customerId, int storeId) { using var context = new Project0Context(mOptions); var exisitingCustomer = context.Customer.Where(c => c.Id == customerId).First(); exisitingCustomer.StoreId = storeId; context.SaveChanges(); }
public void SaveStoreStockQuantities(ICollection <StoreStock> storeStock) { using var context = new Project0Context(mOptions); foreach (var stock in storeStock) { context.StoreStock.Where(s => s.Id == stock.Id).First().ProductQuantity = stock.ProductQuantity; } context.SaveChanges(); }
static void AddCustomer(string firstName, string lastName, string email) { using var context = new Project0Context(s_dbContextOptions); var customer = new Customer() { FirstName = firstName, LastName = lastName, Email = email }; context.Customers.Add(customer); context.SaveChanges(); }
public CustomerRepositoryTest() { mContext = new Project0Context(new DbContextOptionsBuilder <Project0Context> () .UseInMemoryDatabase(databaseName: "CustomerDatabase") .Options); mContext.Database.EnsureDeleted(); mContext.Customer.Add(new Customer { Firstname = "Test", Lastname = "One" }); mContext.SaveChanges(); }
/// <summary> /// Commit transaction to the database /// </summary> public void Save() { try { _dbContext.SaveChanges(); } catch (DbUpdateException) { var transaction = _dbContext.ChangeTracker.Entries() .Where(x => x.State == EntityState.Added || x.State == EntityState.Deleted || x.State == EntityState.Modified).ToList(); foreach (var entry in transaction) { entry.State = EntityState.Detached; } throw; } }
/// <summary> /// Add a location to the list of locations /// </summary> /// <param name="location">The location to be added</param> public void AddCustomer(string firstName, string lastName) { // get the context of the db using var context = new Project0Context(_dbContext); // create a new customer from the DatabaseModel if (firstName.Length > 0 && lastName.Length > 0) { DatabaseModels.Customer cust = new DatabaseModels.Customer() { FirstName = firstName, LastName = lastName }; //add customer to context and save it to DB context.Add(cust); context.SaveChanges(); } }
/// <summary> /// InsertCustomer adds a new customer at the highest index of the table /// </summary> public void InsertCustomer() { Console.Write("\nEnter Customer's:\nFirst Name: "); string FirstName = Console.ReadLine(); Console.Write("\nLast Name: "); string LastName = Console.ReadLine(); Console.Write("\nStreet Address (no city/state/zip): "); string StreetAddress = Console.ReadLine(); Console.Write("\nCity: "); string City = Console.ReadLine(); Console.Write("\nState:"); string State = Console.ReadLine(); Console.Write("\nZip Code: "); int Zip = Int32.Parse(Console.ReadLine()); Console.Write("\nE-mail Address: "); string email = Console.ReadLine(); Console.Write("\nPhone Number: "); string phone = Console.ReadLine(); using (var context = new Project0Context(Options)) { Customers customer = new Customers(); customer.FirstName = FirstName; customer.LastName = LastName; customer.StreetAddress = StreetAddress; customer.City = City; customer.State = State; customer.Zip = Zip; customer.Email = email; customer.Phone = phone; context.Customers.Add(customer); context.SaveChanges(); } }
/// <summary> /// Removes a whole product from inventory /// </summary> /// <remarks> /// Will remove the entire product, currently there is no way to remove a particular quantity yet /// </remarks> /// <param name="location">The location to remove from</param> /// <param name="productId">The product to remove</param> /// <returns>true is sucessful, false otherwise</returns> public bool RemoveLocationInventory(Location location, int productId) { // set up context using var context = new Project0Context(_dbContext); // nest in a try catch block to report error to user try { context.Remove(context.Inventories.First(i => i.LocationId == location.Id && i.ProductId == productId)); context.SaveChanges(); } catch (DbUpdateException) { return(false); } return(true); }
/// <summary> /// Creates a new product and adds it to the database /// </summary> /// <param name="name">Name of product</param> /// <param name="description">Description of product</param> /// <param name="price">Price of product</param> /// <param name="orderLimit">Order Limit of product</param> public void AddDbProduct(string name, string description, decimal price, int orderLimit) { // get the context of the db using var context = new Project0Context(_dbContext); // Create the new product var product = new DatabaseModels.Product() { Name = name, Description = description, Price = price, OrderLimit = orderLimit }; // Add to db context.Products.Add(product); context.SaveChanges(); }
/// <summary> /// Add a location to the list of locations /// </summary> /// <param name="location">The location to be added</param> public void AddLocation(string name) { // get the context of the db using var context = new Project0Context(_dbContext); if (name.Length > 0) { // create the db model to add DatabaseModels.Location location = new DatabaseModels.Location() { Name = name }; //add location to context and save context.Add(location); context.SaveChanges(); } }
public StoreRepositoryTest() { mContext = new Project0Context(new DbContextOptionsBuilder <Project0Context> () .UseInMemoryDatabase(databaseName: "StoreDatabase") .Options); mContext.Database.EnsureDeleted(); mContext.Store.Add(new Store { Name = "Test One", StoreStock = new List <StoreStock> { new StoreStock { Product = new Product { Name = "Test Product" } } } }); mContext.SaveChanges(); }