Example #1
0
        public ActionResult ProductList(Guid? id)
        {
            List<ProductInfo> prods;

            using (var db = new qStoreDBEntities())
            {
                Category category = null;

                if (id.HasValue)
                {
                    category = db.Categories.SingleOrDefault(x => x.Id == id);
                }

                if (category == null)
                {
                    category = db.Categories.First(x => !x.IsDisabled);
                }

                prods = category.Products
                    .Select(x => new ProductInfo
                            {
                                Description = x.Description,
                                Id = x.Id,
                                ImageBase64String = x.ImageBase64String,
                                ImageContentType = x.ImageBase64String,
                                Price = x.Price,
                                Title = x.Title
                            }).ToList();
            }

            return PartialView(prods);
        }
Example #2
0
        public ActionResult CategoryList()
        {
            List<Category> categories;

            using (var db = new qStoreDBEntities())
            {
                categories = db.Categories.ToList();
            }

            return PartialView(categories);
        }
Example #3
0
        public ActionResult DeleteProduct(Guid id)
        {
            using (var db = new qStoreDBEntities())
            {
                var prod = db.Products.SingleOrDefault(x => x.Id == id);

                if (prod != null)
                {
                    db.Products.Remove(prod);
                    db.SaveChanges();
                }
                else
                {
                    throw new InvalidOperationException("Product not found");
                }
            }

            return RedirectToAction("GetProducts");
        }
Example #4
0
        public ActionResult DeleteCategory(Guid id)
        {
            using (var db = new qStoreDBEntities())
            {
                var cat = db.Categories.SingleOrDefault(x => x.Id == id);

                if (cat != null)
                {
                    db.Categories.Remove(cat);
                }
                else
                {
                    throw new InvalidOperationException("Category not found");
                }

                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
Example #5
0
        public ActionResult EditCategory(Guid? id)
        {
            Category cat = null;

            if (id.HasValue)
            {
                using (var db = new qStoreDBEntities())
                {
                    cat = db.Categories.SingleOrDefault(x => x.Id == id);
                }
            }

            if (cat == null)
            {
                cat = new Category
                      {
                          Id = Guid.NewGuid()
                      };
            }

            return View(cat);
        }
Example #6
0
        private bool Authorize(LoginModel model)
        {
            //Check if user is in DB and write into cookie
            if (!string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.Password))
            {
                string hash;
                using (MD5 md5Hash = MD5.Create())
                {
                    hash = GetMd5Hash(md5Hash, model.Password);
                }

                //TODO: check if db contains user with same Name and hash
                User user;
                using (qStoreDBEntities db = new qStoreDBEntities())
                {
                    user = db.Users.FirstOrDefault(x => x.Email == model.Name && x.PassHash == hash);
                }
                if (user != null)
                {
                    var cookie = new HttpCookie("credentials", model.Name);
                    cookie.Expires = DateTime.Now.AddDays(2);

                    Response.Cookies.Add(cookie);

                    return true;
                }
            }

            return false;
        }
Example #7
0
        public ActionResult EditProduct(Guid? id)
        {
            Product prod = null;

            if (id.HasValue)
            {
                using (var db = new qStoreDBEntities())
                {
                    prod = db.Products.SingleOrDefault(x => x.Id == id);
                }
            }

            if (prod == null)
            {
                prod = new Product
                {
                    Id = Guid.NewGuid()
                };
            }

            using (var db = new qStoreDBEntities())
            {
                var catList = db.Categories.Where(x => !x.IsDisabled).ToList()
                    .Select(x => new SelectListItem { Text = x.Title, Value = x.Id.ToString() }).ToList();

                ViewBag.Categories = catList;
            }

            return View(prod);
        }
Example #8
0
        public ActionResult SaveProduct(HttpPostedFileBase file, Product model)
        {
            using (var db = new qStoreDBEntities())
            {
                var product = db.Products.SingleOrDefault(x => x.Id == model.Id);
                model.Category = db.Categories.SingleOrDefault(x => x.Id == model.CategoryId);

                if (file != null && file.ContentLength > 0)
                {
                    model.ImageContentType = file.ContentType;
                    model.ImageBase64String = GetByteArrayString(file);
                }

                if (product != null)
                {
                    product.Title = model.Title;
                    product.Description = model.Description;
                    product.Category = model.Category;
                    product.ImageBase64String = model.ImageBase64String;
                    product.ImageContentType = model.ImageContentType;
                    product.Price = model.Price;
                }
                else
                {
                    db.Products.Add(model);
                }

                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
Example #9
0
        public ActionResult SaveCategory(Category model)
        {
            using (var db = new qStoreDBEntities())
            {
                var cat = db.Categories.SingleOrDefault(x => x.Id == model.Id);

                if (cat != null)
                {
                    cat.Title = model.Title;
                    cat.IsDisabled = model.IsDisabled;
                }
                else
                {
                    db.Categories.Add(model);
                }

                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
Example #10
0
        public ActionResult GetProducts()
        {
            List<Product> prods;

            using (var db = new qStoreDBEntities())
            {
                prods = db.Products.ToList();
            }

            return PartialView(prods);
        }
Example #11
0
        public ActionResult GetProductDetails(Guid id)
        {
            Product prod;
            ProductInfo model;
            Mapper.CreateMap<Product, ProductInfo>();
            using (var db = new qStoreDBEntities())
            {
                prod = db.Products.SingleOrDefault(x => x.Id == id);
                if (prod != null)
                {
                    model = Mapper.Map<ProductInfo>(prod);
                }
                else
                {
                    throw new InvalidOperationException("Product not found");
                }
            }

            return PartialView(model);
        }