Esempio n. 1
0
 public IList <Product> GetProductsByCategory(int id)
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Products.Where(p => p.CategoryID == id).ToList());
     }
 }
Esempio n. 2
0
 public IList <Sale> GetAllSales()
 {
     using (var ctx = new CoronaPageContext())
     {
         return(ctx.Sales.ToList());
     }
 }
Esempio n. 3
0
        public Product UpdateProduct(Product product)
        {
            using (var context = new CoronaPageContext())
            {
                var targetProduct = context.Products.Where(p => p.ProductID == product.ProductID).FirstOrDefault();

                targetProduct.Name        = product.Name;
                targetProduct.Description = product.Description;
                targetProduct.Price       = product.Price;
                targetProduct.CategoryID  = product.CategoryID;

                if (product.Image != null)
                {
                    targetProduct.Image = product.Image;
                }

                try
                {
                    context.Products.Update(targetProduct);
                    context.SaveChanges();
                }
                catch (Exception)
                {
                    return(null);
                }

                return(targetProduct);
            }
        }
Esempio n. 4
0
 public IList <Category> GetAllAndNullCategories()
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Categories.ToList());
     }
 }
Esempio n. 5
0
 public User GetUser(string userName)
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Users.FirstOrDefault(u => u.Username == userName));
     }
 }
Esempio n. 6
0
        public LoginResult AttemptLogin(LoginDetails input)
        {
            if (input == null)
            {
                return(LoginResult.Failed);
            }

            using (var context = new CoronaPageContext())
            {
                var userDetails = context.Users.Where(p => p.Username == input.Username).FirstOrDefault();

                if (userDetails == null || input.Password == null || input.Username == null)
                {
                    return(LoginResult.Failed);
                }

                var encryptedPassword = convertToMd5(input.Password);
                var token             = convertToMd5(DateTime.Now.ToString());

                if (!encryptedPassword.Equals(userDetails.Password))
                {
                    return(LoginResult.Failed);
                }

                return(new LoginResult()
                {
                    LoginSucceeded = true,
                    Token = token
                });
            }
        }
Esempio n. 7
0
        public bool BuyProduct(int UserId, int id)
        {
            using (var context = new CoronaPageContext())
            {
                var targetInventory = context.Inventory.Where(p => p.ProductID == id).FirstOrDefault();
                if (targetInventory is null)
                {
                    return(false);
                }
                targetInventory.Quantity--;

                Sale saleToAdd = new Sale()
                {
                    ProductID = id,
                    UserID    = UserId
                };

                try
                {
                    context.Inventory.Update(targetInventory);
                    context.Sales.Add(saleToAdd);
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 8
0
        public IList <CategoryResult> GetAllCategories()
        {
            using (var context = new CoronaPageContext())
            {
                var categories = context.Categories.ToList();

                var productsCount = context.Products.GroupBy(p => p.CategoryID)
                                    .Select(group => new
                {
                    categoryId = group.FirstOrDefault().CategoryID,
                    count      = group.Count()
                }).ToList();

                return(categories.Join(productsCount,
                                       c => c.CategoryID,
                                       pc => pc.categoryId,
                                       (c, pc) => new CategoryResult()
                {
                    CategoryID = c.CategoryID,
                    Name = c.Name,
                    Count = pc.count
                })
                       .ToList());
            }
        }
Esempio n. 9
0
        public Product AddProduct(Product pProduct)
        {
            using (var context = new CoronaPageContext())
            {
                Product newProd;
                try
                {
                    pProduct.SupplierID = 2;
                    context.Products.Add(pProduct);
                    context.SaveChanges();
                    newProd = context.Products.Where(p => p == pProduct).FirstOrDefault();
                    //Inventory newProdInventory = new Inventory()
                    //{
                    //    ProductID = newProd.ProductID,
                    //    Quantity = 100
                    //};
                    //context.Inventory.Add(newProdInventory);
                    //context.SaveChanges();
                }
                catch (Exception e)
                {
                    return(null);
                }

                return(newProd);
            }
        }
Esempio n. 10
0
 public bool DoesUserExists(string username)
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Users.Count(u => u.Username == username) > 0);
     }
 }
Esempio n. 11
0
 private string GetCategoryName(int key)
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Categories.Where(c => c.CategoryID == key).FirstOrDefault().Name);
     }
 }
Esempio n. 12
0
 public IList <Product> GetAllProductsFromInventory()
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Products.ToList());
     }
 }
Esempio n. 13
0
 private string GetUserName(int key)
 {
     using (var context = new CoronaPageContext())
     {
         var query = context.Users.Where(u => u.UserID == key).FirstOrDefault();
         return(query.FirstName);
     }
 }
Esempio n. 14
0
 public dynamic GetAllSalesByUser()
 {
     using (var ctx = new CoronaPageContext())
     {
         return(ctx.Sales.GroupBy(s => s.UserID,
                                  s => s.ProductID,
                                  (key, g) => new { UserID = key, Products = g.ToList() }).ToList());
     }
 }
Esempio n. 15
0
 public ProductResult GetProduct(int id)
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Products
                .Join(context.Categories, p => p.CategoryID, c => c.CategoryID, (product, category) => new ProductResult(product, category))
                .GroupJoin(context.Inventory, p => p.ProductID, i => i.ProductID, (product, inventory) => new ProductResult(product, inventory))
                .FirstOrDefault(p => p.ProductID == id));
     }
 }
Esempio n. 16
0
        public IList <Location> SearchLocations(string name, int?population)
        {
            using (var context = new CoronaPageContext())
            {
                IQueryable <Location> locations = context.Locations.Where(p => p.City.ToLower().Contains(name.ToLower()));

                if (population != null)
                {
                    locations = locations.Where(p => p.Population <= population);
                }

                return(locations.ToList());
            }
        }
Esempio n. 17
0
        public IList <ProductResult> getUserProducts(int UserId)
        {
            IList <ProductResult> listToReturn = new List <ProductResult>();

            using (var context = new CoronaPageContext())
            {
                var sales = context.Sales.Where(u => u.UserID == UserId);
                foreach (var sale in sales)
                {
                    listToReturn.Add(GetProduct(sale.ProductID));
                }
            }
            return(listToReturn);
        }
Esempio n. 18
0
 public bool UpdateInventory(Inventory inventory)
 {
     using (var context = new CoronaPageContext())
     {
         try
         {
             context.Inventory.Add(inventory);
             context.SaveChanges();
         }
         catch (Exception)
         {
             return(false);
         }
         return(true);
     }
 }
Esempio n. 19
0
 public IList <Product> SearchProducts(string term, int?price)
 {
     using (var context = new CoronaPageContext())
     {
         if (price != null)
         {
             return(context.Products
                    .Where(p => (p.Name.Contains(term) || p.Description.Contains(term)) && p.Price <= price).ToList());
         }
         else
         {
             return(context.Products
                    .Where(p => p.Name.Contains(term) || p.Description.Contains(term)).ToList());
         }
     }
 }
Esempio n. 20
0
 public ProductStockResult[] GetProductsStock()
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Inventory.Join(context.Products, i => i.ProductID, p => p.ProductID, (inventory, product) => new
         {
             Product = product,
             Inventory = inventory
         })
                .GroupBy(x => x.Product.Name)
                .Select(x => new ProductStockResult()
         {
             ProductName = x.Key,
             Count = x.Sum(y => y.Inventory.Quantity)
         })
                .ToArray());
     }
 }
Esempio n. 21
0
 public IList <SalesCategory> SalesPerCategory()
 {
     using (var context = new CoronaPageContext())
     {
         return(context.Sales.Join(context.Products, s => s.ProductID, p => p.ProductID, (sale, product) => new
         {
             Product = product,
             Sale = sale
         })
                .GroupBy(x => x.Product.CategoryID)
                .Select(x => new SalesCategory()
         {
             CategoryName = GetCategoryName(x.Key),
             SalesSum = (int)x.Sum(y => y.Product.Price)
         })
                .ToList());
     }
 }
Esempio n. 22
0
        public object UpdateLocation(Location location)
        {
            using (var context = new CoronaPageContext())
            {
                var targetLocation = context.Locations.Where(c => c.LocationID == location.LocationID).FirstOrDefault();

                try
                {
                    targetLocation.Population = location.Population;
                    context.Locations.Update(targetLocation);
                    context.SaveChanges();
                }
                catch (Exception)
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 23
0
        public bool DeleteProduct(int id)
        {
            using (var context = new CoronaPageContext())
            {
                var targetProduct = context.Products.Where(p => p.ProductID == id).FirstOrDefault();
                context.Products.Remove(targetProduct);

                try
                {
                    context.SaveChanges();
                }
                catch (Exception)
                {
                    return(false);
                }

                return(true);
            }
        }
Esempio n. 24
0
        public IList <Product> SearchProducts(string term, int?price, int categoryId)
        {
            using (var context = new CoronaPageContext())
            {
                var products = context.Products.Where(p => (p.Name.ToLower().Contains(term.ToLower()) || p.Description.ToLower().Contains(term.ToLower())));

                if (price != null)
                {
                    products = products.Where(p => p.Price <= price);
                }

                if (categoryId > 0)
                {
                    products = products.Where(p => p.CategoryID == categoryId);
                }

                return(products.ToList());
            }
        }
Esempio n. 25
0
        public bool DeleteLocation(int id)
        {
            using (var context = new CoronaPageContext())
            {
                var targetLocation = context.Locations.Where(c => c.LocationID == id).FirstOrDefault();

                try
                {
                    context.Locations.Remove(targetLocation);
                    context.SaveChanges();
                }
                catch (Exception)
                {
                    return(false);
                }

                return(true);
            }
        }
Esempio n. 26
0
        public IList <ProductsUser> ProductsByUser()
        {
            using (var context = new CoronaPageContext())
            {
                var prodByUser = context.Sales.Join(context.Products, s => s.ProductID, p => p.ProductID, (sale, product) => new
                {
                    Product = product,
                    Sale    = sale
                })
                                 .GroupBy(x => x.Sale.UserID)
                                 .Select(x => new ProductsUser()
                {
                    UserName    = GetUserName(x.Key),
                    UserExpense = (int)x.Sum(y => y.Product.Price)
                })
                                 .OrderByDescending(p => p.UserExpense).ToList();

                prodByUser.RemoveRange(6, prodByUser.Count - 6);
                return(prodByUser);
            }
        }
Esempio n. 27
0
        public bool Register(RegisterDetails UserInput)
        {
            if (UserInput is null || CheckInputValid(UserInput))
            {
                return(false);
            }
            using (var Context = new CoronaPageContext())
            {
                User newUser = new User()
                {
                    Address   = UserInput.Address,
                    FirstName = UserInput.FirstName,
                    IsAdmin   = 0,
                    LastName  = UserInput.LastName,
                    Password  = convertToMd5(UserInput.Password),
                    Phone     = UserInput.Phone,
                    Username  = UserInput.Username
                };

                Context.Users.Add(newUser);
                Context.SaveChanges();
            }
            return(true);
        }