public void product_add_product_httpPost_modelstate_is_valid()
        {
            // Arrange
            TestControllerBuilder builder = new TestControllerBuilder();
            var controller = new ProductController(new ProductBLL(new ProductDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };
            ProductDetail p = new ProductDetail()
            {
                name = "Pilsner",
                description = "gul",
                longDescription = "gulere"
            };

            // Act
            var result = (JsonResult)controller.addProduct(p);
            var success = (bool)(new PrivateObject(result.Data, "success")).Target;

            // Assert
            Assert.IsTrue(success);
        }
        public void product_update_detail_HTTPPOST_modelState_Is_INValid()
        {
            // Arrange
            TestControllerBuilder builder = new TestControllerBuilder();

            var controller = new ProductController(new ProductBLL(new ProductDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };
            controller.ViewData.ModelState.AddModelError("feil", "dette ble feil gitt");
            ProductDetail expected = new ProductDetail()
            {
                itemnumber = 1,
                name = "Tullball",
                description = "Hei",
                price = 123,
                volum = 50,
                producerid = 2,
                longDescription = "Tullball er et fantastisk godt drikkeprodukt",
                subCategoryid = 3,
                countryid = 1
            };

            // Act
            var result = (JsonResult)controller.ProductDetails(1, expected);
            var success = (bool)(new PrivateObject(result.Data, "success")).Target;

            //Assert
            Assert.IsFalse(success);
        }
        public void product_add_product_httpPost_modelstate_is_invalid()
        {
            // Arrange
            TestControllerBuilder builder = new TestControllerBuilder();
            var controller = new ProductController(new ProductBLL(new ProductDALStub()));
            builder.InitializeController(controller);
            controller.ViewData.ModelState.AddModelError("feil", "dette ble feil gitt");
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };
            ProductDetail p = new ProductDetail()
            {
                name = "",
                description = "",
                longDescription = ""
            };
            // Act
            var result = (JsonResult)controller.addProduct(p);
            var success = (bool)(new PrivateObject(result.Data, "success")).Target;

            // Assert
            Assert.IsFalse(success);
        }
        public void product_detail_view()
        {
            // Arrange
            TestControllerBuilder builder = new TestControllerBuilder();

            var controller = new ProductController(new ProductBLL(new ProductDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };

            ProductDetail expected = new ProductDetail()
            {
                itemnumber = 1,
                name = "Tullball",
                description = "Hei",
                price = 123,
                volum = 50,
                producerid = 2,
                longDescription = "Tullball er et fantastisk godt drikkeprodukt",
                subCategoryid = 3,
                countryid = 1
            };

            // Act
            var action = (ViewResult)controller.ProductDetails(expected.itemnumber);
            var result = (ProductDetail)action.Model;

            // Assert
            Assert.AreEqual(expected.itemnumber, result.itemnumber);
            Assert.AreEqual(expected.name, result.name);
            Assert.AreEqual(expected.description, result.description);
            Assert.AreEqual(expected.price, result.price);
            Assert.AreEqual(expected.volum, result.volum);
            Assert.AreEqual(expected.producerid, result.producerid);
            Assert.AreEqual(expected.longDescription, result.longDescription);
            Assert.AreEqual(expected.countryid, result.countryid);
        }
        public ActionResult addProduct(ProductDetail p)
        {
            if (!isAdmin())
            {
                return RedirectToAction("LogIn", "Main");
            }
            if(ModelState.IsValid)
            {
                Customer admin = (Customer)Session["loggedInUser"];
                var adminid = admin.id;
                var result = _product.addProduct(adminid, new Product()
                {
                    name = p.name,
                    countryid = p.countryid,
                    description = p.description,
                    longDescription = p.longDescription,
                    price = p.price,
                    producerid = p.producerid,
                    subCategoryid = p.subCategoryid,
                    volum = p.volum
                }); 

                        
                p.countryList = _product.getCountries().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name }).ToList();
                p.subCategoryList = _product.getAllSubCategories().Select(s => new GroupedSelectListItem { GroupKey = s.catId.ToString(), GroupName = s.catName, Value = s.ID.ToString(), Text = s.name,Selected=true }).ToList();
                p.producerList = _product.getProducers().Select(r => new SelectListItem { Value = r.id.ToString(), Text = r.name }).ToList();

                if (result != null)
                    return Json(new { success = true, message = result.name + " ble lagt til med varenummer " + result.itemnumber, redirect = "/Product/ListProducts/?sortOrder=item_desc" });
                return Json(new { success = false, message = "Noe gikk galt, prøv igjen senere" });
            }
            return Json(new { success = false, message = "Feil i validering" });
        }
 public ActionResult addProduct()
 {
     if (!isAdmin())
     {
         return RedirectToAction("LogIn", "Main");
     }
     var placeholder = new ProductDetail()
     {
         countryList = _product.getCountries().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name }).ToList(),
         subCategoryList = _product.getAllSubCategories().Select(s => new GroupedSelectListItem { GroupKey = s.catId.ToString(), GroupName = s.catName, Value = s.ID.ToString(), Text = s.name }).ToList(),
         producerList = _product.getProducers().Select(r => new SelectListItem { Value = r.id.ToString(), Text = r.name }).ToList()
     };
     return View(placeholder);
 }
        public ActionResult ProductDetails(int id,ProductDetail p)
        {
            if (!isAdmin())
            {
                return RedirectToAction("LogIn", "Main");
            }
            if (ModelState.IsValid)
            {
                Product updated = new Product()
                {
                    itemnumber = p.itemnumber,
                    name = p.name,
                    description = p.description,
                    longDescription = p.longDescription,
                    price = p.price,
                    countryid = p.countryid,
                    subCategoryid = p.subCategoryid,
                    volum = p.volum,
                    producerid = p.producerid
                };
                Customer admin = (Customer)Session["loggedInUser"];
                var adminid = admin.id;
                bool result = _product.updateProduct(adminid, updated);
                
                List<GroupedSelectListItem> test2 = new List<GroupedSelectListItem>();
                var test1 = _product.getAllSubCategories();
                foreach (var item in test1)
                {
                    if (item.ID == p.subCategoryid)
                    {
                        test2.Add(new GroupedSelectListItem()
                        {
                            GroupKey = item.catId.ToString(),
                            GroupName = item.catName,
                            Text = item.name,
                            Value = item.ID.ToString(),
                            Selected = true
                        });
                    }
                    else
                    {
                        test2.Add(new GroupedSelectListItem()
                        {
                            GroupKey = item.catId.ToString(),
                            GroupName = item.catName,
                            Text = item.name,
                            Value = item.ID.ToString()
                        });
                    }
                }

                IEnumerable<GroupedSelectListItem> test = test2;
                p.countryList = _product.getCountries().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name }).ToList();
                p.subCategoryList = test;
                p.producerList = _product.getProducers().Select(r => new SelectListItem { Value = r.id.ToString(), Text = r.name }).ToList();

                if (result)
                    return Json(new { success = true, message = "Endringene ble lagret", redirect = "/Product/ListProducts/" });
                return Json(new { success = false, message = "Noe gikk galt, prøv igjen" });
            }
            return Json(new { success = false, message = "Feil i validering" });

        }
        public ActionResult ProductDetails(int id)
        {
            if (!isAdmin())
            {
                return RedirectToAction("LogIn", "Main");
            }
            Product productDetails = _product.seeDetails(id);
            
            List<GroupedSelectListItem> dropdownsubcategory2 = new List<GroupedSelectListItem>();
            var dropdownsubcategory1 = _product.getAllSubCategories();
            foreach (var item in dropdownsubcategory1)
            {
                if(item.ID == productDetails.subCategoryid)
                {
                    dropdownsubcategory2.Add(new GroupedSelectListItem()
                    {
                        GroupKey = item.catId.ToString(),
                        GroupName = item.catName,
                        Text = item.name,
                        Value = item.ID.ToString(),
                        Selected = true
                    });
                }
                else
                {
                    dropdownsubcategory2.Add(new GroupedSelectListItem()
                    {
                        GroupKey = item.catId.ToString(),
                        GroupName = item.catName,
                        Text = item.name,
                        Value = item.ID.ToString()
                    });
                }
            }

            IEnumerable<GroupedSelectListItem> dropdownsubcategory = dropdownsubcategory2; 
            ProductDetail prodinfo = new ProductDetail()
            {
                itemnumber = productDetails.itemnumber,
                name = productDetails.name,
                description = productDetails.description,
                longDescription = productDetails.longDescription,
                price = productDetails.price,
                volum = productDetails.volum,
                countryid = productDetails.countryid,
                producerid = productDetails.producerid,
                pricePerLitre = productDetails.pricePerLitre,
                subCategoryList = dropdownsubcategory,
                countryList = _product.getCountries().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name}).ToList(),
                producerList = _product.getProducers().Select(p => new SelectListItem { Value = p.id.ToString(), Text = p.name}).ToList()
            };
            return View(prodinfo);
        }