public Product GetOneProduct(int _productId) { Product _targetProduct = new Product(); using (CoffeeShopContext db = new CoffeeShopContext()) { _targetProduct = db.Products.Find(_productId); } return(_targetProduct); }
//this method will load in my DB data private async Task GetData() { db = new CoffeeShopContext(); //call the items table and pull in the data, to hold in our private field //first called the items table itemList = await GetItems(); //now call the users table userList = db.Users.ToList(); }
public void UpdateCountInProduct(int _productId, int _amountOfBothProduct) { Product _changingProduct = new Product(); using (CoffeeShopContext db = new CoffeeShopContext()) { _changingProduct = db.Products.Find(_productId); _changingProduct.Amount = _changingProduct.Amount - _amountOfBothProduct; db.SaveChanges(); } }
public void RetainChanges() { optionsBuilder.UseInMemoryDatabase("RetainChanges"); using (var context = new CoffeeShopContext(optionsBuilder.Options)) { context.Database.EnsureCreated(); } using (var newContextSameDbName = new CoffeeShopContext(optionsBuilder.Options)) { Assert.AreNotEqual(0, newContextSameDbName.Locations.Count()); } }
public IActionResult Index() { CoffeeShopContext db = new CoffeeShopContext(); //grab user items and add to a list //var userItems = db.UserItems.ToList(); //db.UserItems.Add(new UserItems() { ItemId = 1, UserId = 3}); //db.SaveChanges(); return(View()); }
public void ResetWithNoSeedData() { optionsBuilder.UseInMemoryDatabase("ResetWithSeedData"); using (var context = new CoffeeShopContext(optionsBuilder.Options)) { context.Database.EnsureCreated(); } optionsBuilder.UseInMemoryDatabase("NewInMemory"); using (var newContextNewDbName = new CoffeeShopContext(optionsBuilder.Options)) { Assert.AreEqual(0, newContextNewDbName.Locations.Count()); } }
public Task <IEnumerable <Customer> > LoadCustomers() { IEnumerable <Customer> result; using (var c = new CoffeeShopContext()) { result = c.CoffeeCustomers.Select(x => new Customer { FirstName = x.FirstName, LastName = x.LastName, IsDeveloper = x.IsDeveloper }).ToList(); } return(Task.FromResult(result)); }
public void RetainChangesAsync() { _optionsBuilder.UseInMemoryDatabase("RetainChanges"); using (var context = new CoffeeShopContext(_optionsBuilder.Options)) { context.Database.EnsureCreated(); } using (var newContext = new CoffeeShopContext(_optionsBuilder.Options)) { var list = newContext.Locations.ToList(); Assert.AreNotEqual(0, list.Count); } }
public void SaveCustomers(IEnumerable <Customer> customers) { using (var c = new CoffeeShopContext()) { var r = c.CoffeeCustomers; c.CoffeeCustomers.RemoveRange(r); c.CoffeeCustomers.AddRange(customers.Select(x => new Customer { FirstName = x.FirstName, LastName = x.LastName, IsDeveloper = x.IsDeveloper })); c.SaveChanges(); } }
public IActionResult Welcome(User user, string password2, string password) { CoffeeShopContext db = new CoffeeShopContext(); User ValidUser = new User(); if (password != password2) { return(ModalAction()); } else { db.Add(user); db.SaveChanges(); } return(View()); }
public List <Discount> GetAllDiscounts() { List <Discount> _allDiscounts = new List <Discount>(); using (CoffeeShopContext db = new CoffeeShopContext()) { IQueryable _discounts = db.Discounts; foreach (Discount i in _discounts) { _allDiscounts.Add(i); } } return(_allDiscounts); }
public List <Customer> GetAllCustomers() { List <Customer> _allCustomers = new List <Customer>(); using (CoffeeShopContext db = new CoffeeShopContext()) { IQueryable _customers = db.Customers.Include(i => i.Discount); foreach (Customer i in _customers) { _allCustomers.Add(i); } return(_allCustomers); } }
public List <Product> GetAllProducts() { List <Product> _allProducts = new List <Product>(); using (CoffeeShopContext db = new CoffeeShopContext()) { IQueryable _products = db.Products; foreach (Product i in _products) { _allProducts.Add(i); } return(_allProducts); } }
public Customer GetOneCustomerByDiscountCardNumber(int _numberCard) { Customer _targetCustomer = new Customer(); using (CoffeeShopContext db = new CoffeeShopContext()) { foreach (var i in db.Customers) { if (i.DiscountCardNumber == _numberCard) { _targetCustomer = i; break; } } return(_targetCustomer); } }
public static async Task SeedAsync(CoffeeShopContext ctx) { if (!ctx.Categories.Any()) { var categoriesData = File.ReadAllText("../CofeeShopDAL/SeedData/Assets/categories.json"); var categories = JsonSerializer.Deserialize <List <Category> >(categoriesData); foreach (var item in categories) { ctx.Categories.Add(item); } await ctx.SaveChangesAsync(); } if (!ctx.ProductTypes.Any()) { var typesData = File.ReadAllText("../CofeeShopDAL/SeedData/Assets/types.json"); var types = JsonSerializer.Deserialize <List <ProductType> >(typesData); foreach (var item in types) { ctx.ProductTypes.Add(item); } await ctx.SaveChangesAsync(); } if (!ctx.Products.Any()) { var productsData = File.ReadAllText("../CofeeShopDAL/SeedData/Assets/products.json"); var products = JsonSerializer.Deserialize <List <Product> >(productsData); foreach (var item in products) { ctx.Products.Add(item); } await ctx.SaveChangesAsync(); } }
public IActionResult Purchase(decimal?price, User user) { CoffeeShopContext db = new CoffeeShopContext(); user = JsonSerializer.Deserialize <User>(HttpContext.Session.GetString("User")); if (user.CartFunds >= price) { user.CartFunds = user.CartFunds - price; db.Update(user); db.SaveChanges(); } //else if (foundUser.CartFunds <= price) //{ // return View(); //} return(View("Shop")); }
private static void LazyLoadLocationEmployees() { using (var context = new CoffeeShopContext()) { var locations = context.Locations.ToList(); foreach (var l in locations) { Console.WriteLine($"{l.StreetAddress}, Hours {l.Hours}"); if (l.OpenTime <= TimeSpan.FromHours(7)) { foreach (var employee in l.Employees) { Console.WriteLine($"{employee.Name} {(employee.Barista ? "(Barista)" : "")}"); } } } Console.ReadKey(); } }
public IActionResult Login(string email = null, string password = null) { CoffeeShopContext db = new CoffeeShopContext(); TempData["Registered"] = false; if (email != null && password != null) { bool valid = ValidLogin(email, password); if (valid) { TempData["Registered"] = true; User user = (User)TempData["User"]; return(View("Shop", db)); } } return(View(db)); }
private static void MultiplePropertyGroupByIntoKnownType() { using (var context = new CoffeeShopContext()) { var unitGrouping = context.Units .GroupBy(o => new { o.BrewerTypeId, o.Location.StreetAddress }) .Select(g => new UnitGroup { BrewerTypeId = g.Key.BrewerTypeId, StreetAddress = g.Key.StreetAddress, Cost = g.Sum(u => u.Cost), Count = g.Count() }).ToList(); foreach (UnitGroup item in unitGrouping) { Console.WriteLine($"LocationId: {item.StreetAddress}"); Console.WriteLine($" TypeId: {item.BrewerTypeId}"); Console.WriteLine($" # Units: {item.Count}"); Console.WriteLine($" Total Cost: {item.Cost}"); Console.WriteLine($" Average Cost: {item.Cost / item.Count}"); } } }
public async void SaveServerName() { await Task.Run(() => { using (var context = new CoffeeShopContext()) { context.Database.CreateIfNotExists(); App.Current.Dispatcher.BeginInvoke((Action)(() => { var selectDataContext = CSGlobal.Instance.InitializeWindow.DataContext as InitViewmodel; if (selectDataContext != null) { selectDataContext.Loaded(); } })); } }); CSGlobal.Instance.DisconnectedDatabaseWindow.Hide(); CSGlobal.Instance.InitializeWindow.Show(); }
private static void GroupByScalarPropertyOfRelatedType() { using (var context = new CoffeeShopContext()) { var unitGrouping = context.Units .GroupBy(o => o.Location.StreetAddress) .Select(g => new { LocationAddress = g.Key, Cost = g.Sum(u => u.Cost), Count = g.Count() }).ToList(); foreach (var item in unitGrouping) { Console.WriteLine($"Location: {item.LocationAddress}"); Console.WriteLine($" # Units: {item.Count}"); Console.WriteLine($" Total Cost: {item.Cost}"); Console.WriteLine($" Average Cost: {item.Cost / item.Count}"); } } }
//Evaluated on client private static void SingleNavigationPropertyGroup() { using (var context = new CoffeeShopContext()) { var unitGrouping = context.Units .GroupBy(u => u.BrewerType) .Select(g => new { g.Key.BrewerTypeId, Cost = g.Sum(u => u.Cost), Count = g.Count() }).ToList(); foreach (var item in unitGrouping) { Console.WriteLine($" TypeId: {item.BrewerTypeId}"); Console.WriteLine($" # Units: {item.Count}"); Console.WriteLine($" Total Cost: {item.Cost}"); Console.WriteLine($" Average Cost: {item.Cost / item.Count}"); } } }
//Evaluated on client. Extra queries (one per group) to retrieve navigations private static void SinglePropertyGroupWithNavigationsinSelect() { using (var context = new CoffeeShopContext()) { var unitGrouping = context.Units .GroupBy(o => o.BrewerTypeId) .Select(g => new { BrewerTypeId = g.Key, Brewer = g.Min(u => u.BrewerType.Description), Cost = g.Sum(u => u.Cost), Count = g.Count() }).ToList(); foreach (var item in unitGrouping) { Console.WriteLine($"Brewer: {item.Brewer}"); Console.WriteLine($" # Units: {item.Count}"); Console.WriteLine($" Total Cost: {item.Cost}"); Console.WriteLine($" Average Cost: {item.Cost / item.Count}"); } } }
private static void OrderByAndFilterGrouping() { using (var context = new CoffeeShopContext()) { var unitGrouping = context.Units .GroupBy(o => 2) .OrderBy(o => o.Sum(u => u.Cost)) .Where(o => o.Count() >= 2) .Select(g => new { BrewerTypeId = g.Key, Cost = g.Sum(u => u.Cost), Count = g.Count() }).ToList(); foreach (var item in unitGrouping) { Console.WriteLine($" TypeId: {item.BrewerTypeId}"); Console.WriteLine($" # Units: {item.Count}"); Console.WriteLine($" Total Cost: {item.Cost}"); Console.WriteLine($" Average Cost: {item.Cost / item.Count}"); } } }
//Won't compile private static void SinglePropertyGroupWithNonAggregateProjection() { using (var context = new CoffeeShopContext()) { var unitGrouping = context.Units .OrderBy(u => u.Acquired) .GroupBy(u => u.BrewerTypeId) .Select(g => new { BrewerTypeId = g.Key, Cost = g.Sum(u => u.Cost), //First=g.FirstOrDefault(u=>u.Cost), <-won't compile Count = g.Count() }).ToList(); foreach (var item in unitGrouping) { Console.WriteLine($" TypeId: {item.BrewerTypeId}"); Console.WriteLine($" # Units: {item.Count}"); Console.WriteLine($" Total Cost: {item.Cost}"); Console.WriteLine($" Average Cost: {item.Cost / item.Count}"); } } }
public CartController(CoffeeShopContext _db) { db = _db; }
public UsersController(CoffeeShopContext context, IHttpContextAccessor httpContextAccessor) { _context = context; _session = httpContextAccessor.HttpContext.Session; }
public OrderRepository(CoffeeShopContext context) : base(context) { }
public List <Items> GetItems() { CoffeeShopContext db = new CoffeeShopContext(); return(db.Items.ToList()); }
public CustomerAdminController(CoffeeShopContext _db) { db = _db; }