public void category_subcategories_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("error", "feilmelding");
            var scd = new SubCategoryDetail()
            {
                ID = 8,
                name = "Cappucino"
            };

            //Act
            var action = (JsonResult)controller.SubCatDetails(scd);
            var success = (bool)(new PrivateObject(action.Data, "success")).Target;

            //Assert
            Assert.IsFalse(success);
        }
        public void category_new_subcategory_view_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("feil", "dette ble feil gitt"); 
            SubCategoryDetail scd = new SubCategoryDetail()
            {
                name = "Preskanne",
                categoryId = 2
            };

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

            //Assert
            Assert.IsFalse(success);
        }
        public void category_subcategories_details_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 };

            var scd = new SubCategoryDetail()
            {
                ID = 8,
                name = "Cappucino"
            };

            //Act
            var action = (JsonResult)controller.SubCatDetails(scd);
            var result = (bool)(new PrivateObject(action.Data, "success")).Target;

            //Assert
            Assert.IsTrue(result);
        }
        public void category_new_subcategory_view_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 };

            SubCategoryDetail scd = new SubCategoryDetail()
            {
                name = "Preskanne",
                categoryId = 2
            };
            
            //Act
            var result = (JsonResult)controller.newSubCategory(scd);
            var success = (bool)(new PrivateObject(result.Data, "success")).Target;

            //Assert
            Assert.IsTrue(success);
        }
        public ActionResult SubCatDetails(SubCategoryDetail sc)
        {
            if (!isAdmin())
            {
                return RedirectToAction("LogIn", "Main");
            }
            if (ModelState.IsValid)
            {
                SubCategory updated = new SubCategory()
                {
                    ID = sc.ID,
                    name = sc.name,
                    catId = sc.categoryId,
                };
                Customer admin = (Customer)Session["loggedInUser"];
                var adminid = admin.id;
                bool result = _categoryBLL.update(adminid, updated);
    
                sc.categoryList = _categoryBLL.getAll(null).Select(c => new SelectListItem { Value = c.ID.ToString(), Text = c.name }).ToList();

                if (result)
                    return Json(new { success = true, message = "Subkategorien ble endret", redirect = "/Category/ListSubCategories" });
                return Json(new { success = false, message = "Noe gikk galt, prøv igjen senenere" });
            }
            return Json(new { success = false, message = "feil i validering" });
        }
        public ActionResult SubCatDetails(int id)
        {
            if (!isAdmin())
            {
                return RedirectToAction("LogIn", "Main");
            }
            SubCategory subcatdetails = _categoryBLL.SubCatDetails(id);

            SubCategoryDetail subDetail = new SubCategoryDetail()
            {
                ID = subcatdetails.ID,
                name = subcatdetails.name,
                categoryId = subcatdetails.catId,
                categoryList = _categoryBLL.getAll(null).Select(c => new SelectListItem { Value = c.ID.ToString(), Text = c.name }).ToList()
            };
            return View(subDetail);
        }
        public ActionResult newSubCategory(SubCategoryDetail sc)
        {
            if (!isAdmin())
            {
                return RedirectToAction("LogIn", "Main");
            }

            if (ModelState.IsValid)
            {
                Customer admin = (Customer)Session["loggedInUser"];
                var adminid = admin.id;
                if (_categoryBLL.AddSub(adminid, new SubCategory()
                {
                    name = sc.name,
                    catId = sc.categoryId
                }))
                    return Json(new { success = true, message = "Ny subkategori ble lagt til", redirect = "/Category/ListSubCategories/" });
                return Json(new { success = false, message = "Dette gikk ikke så bra, prøv igjen en annen gang" });
            }
            return Json(new { success = false, message = "Feil i validering" });
            
        }
 public ActionResult newSubCategory()
 {
     if (!isAdmin())
     {
         return RedirectToAction("LogIn", "Main");
     }
     var placeholder = new SubCategoryDetail()
     {
         categoryList = _categoryBLL.getAll(null).Select(c => new SelectListItem { Value = c.ID.ToString(), Text = c.name }).ToList()
     };
     return View(placeholder);
 }