public void category_update_category_details_httppost_modelstate_invalid()
        {
            //Arrange
            TestControllerBuilder builder = new TestControllerBuilder();
            var controller = new CategoryController(new CategoryBLL(new CategoryDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };
            controller.ViewData.ModelState.AddModelError("name", "Mangler brukernavn");
            CategoryInfo ci = new CategoryInfo()
            {
                name = "kaffe"
            };

            //Act
            var result = (JsonResult)controller.updateCatergoryDetails(ci);
            var success = (bool)(new PrivateObject(result.Data, "success")).Target;
            //Assert
            Assert.IsFalse(success);
        }
        public void category_update_category_details()
        {
            //Arrange
            TestControllerBuilder builder = new TestControllerBuilder();
            var controller = new CategoryController(new CategoryBLL(new CategoryDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };

            CategoryInfo expected = new CategoryInfo()
            {
                id = 2,
                name = "Brennevin"
            };

            //Act
            var action = (ViewResult)controller.updateCatergoryDetails(expected.id);
            var result = (CategoryInfo)action.Model; 

            //Assert
            Assert.AreEqual(expected.id, result.id);
            Assert.AreEqual(expected.name, result.name); 
        }
        public void category_update_category_details_httppost_update_OK()
        {
            //Arrange
            TestControllerBuilder builder = new TestControllerBuilder();
            var controller = new CategoryController(new CategoryBLL(new CategoryDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };

            CategoryInfo ci = new CategoryInfo()
            {
                id = 1,
                name = "Kaffe"
            };

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

            //Assert
            Assert.IsTrue(success);
        }
        public void category_new_category_httppost_modelstate_invalid()
        {
            //Arrange
            TestControllerBuilder builder = new TestControllerBuilder();
            var controller = new CategoryController(new CategoryBLL(new CategoryDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };
            controller.ViewData.ModelState.AddModelError("kategori", "Ikke oppgitt kategori");
            CategoryInfo c = new CategoryInfo()
            {
                name = ""
            };

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

            //Assert
            Assert.IsFalse(success);
            
        }
        public void category_new_category_httppost()
        {
            //Arrange
            TestControllerBuilder builder = new TestControllerBuilder();
            var controller = new CategoryController(new CategoryBLL(new CategoryDALStub()));
            builder.InitializeController(controller);
            builder.HttpContext.Session["loggedInUser"] = new Customer() { id = 1, admin = true };
            CategoryInfo c = new CategoryInfo()
            {
                name="en kategori"
            };

            //Act
            var result = (JsonResult)controller.newCategory(c);
            var success = (bool)(new PrivateObject(result.Data, "success")).Target;
            //Assert

            Assert.IsTrue(success);
        }
        public ActionResult updateCatergoryDetails(CategoryInfo c)
        {
            if (!isAdmin())
                return RedirectToAction("LogIn", "Main");
            if (ModelState.IsValid)
            {
                Customer a = (Customer)Session["loggedInUser"];
                Category cat = new Category();
                cat.ID = c.id;
                cat.name = c.name;
                var b = _categoryBLL.updateCategory(c.id, cat ,a.id);
                if (b)
                    return Json(new { success = true, message = "Kategorien ble endret", redirect = "/Category/ListCategories" });
                return Json(new { success = false, message = "Noe gikk galt, prøv igjen senere" });

            }
            return Json(new { success = false, message = "Feil i validering" });
        }
        public ActionResult updateCatergoryDetails(int id) 
        {
            if (!isAdmin())
                return RedirectToAction("LogIn", "Main");

            List<Category> cat = _categoryBLL.getAll(id);
            CategoryInfo ci = new CategoryInfo();
            foreach(var item in cat){
                ci.id = item.ID;
                ci.name = item.name;
            }
           
            
 
            return View(ci);
        }
        public ActionResult newCategory(CategoryInfo category)
        {
            if (!isAdmin())
                return RedirectToAction("LogIn", "Main");

            if (ModelState.IsValid)
            {
                Customer c = (Customer)Session["loggedInUser"];
                Category cat = new Category();
                cat.ID = category.id;
                cat.name = category.name;
                bool OK = _categoryBLL.Add(cat, c.id);
                if (OK)
                    return Json(new { success = true, message = cat.name + " ble lagt til.", redirect = "/Category/ListCategories?item_desc" });
                return Json(new { success = false, message = "noe gikk galt, prøv igjen senere." });
            }
            return Json(new { success = false, message = "Feil i validering" });
        }