public void category_producer_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", "noe gikk galt");
            ProducerInfo pi = new ProducerInfo()
            {
                prodId = 12,
                prodName = "En produsent"
            };

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

            //Assert
            Assert.IsFalse(result); 
        }
        public void category_add_producer_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 producer = new ProducerInfo()
            {
                prodId = 12,
                prodName = "Produsent"
            };
            //Act
            var result = (JsonResult)controller.addProducer(producer);
            var success = (bool)(new PrivateObject(result.Data, "success")).Target;

            //Assert
            Assert.IsTrue(success); 
        }
        public ActionResult ProducerDetails(int id)
        {
            if (!isAdmin())
                return RedirectToAction("LogIn", "Main");
            Producer prodDetails = _categoryBLL.producerDetails(id);

            ProducerInfo prodinfo = new ProducerInfo()
            {
                prodId = prodDetails.id,
                prodName = prodDetails.name
            };

            return View(prodinfo);
        }
        public ActionResult ProducerDetails(int id,ProducerInfo pi)
        {
            if (!isAdmin())
                return RedirectToAction("LogIn", "Main");

            if (ModelState.IsValid)
            {
                Customer c = (Customer)Session["loggedInUser"];
                Producer p = new Producer();
                p.name = pi.prodName;
                var result = _categoryBLL.updateProducer(id , p, c.id);
                if(result)
                    return Json(new { success = true, message = p.name + " ble endret.", redirect = "/Category/ListProducers/" });
                return Json(new { success = false, message = "noe gikk galt, prøv igjen senere." });
            }
            return Json(new { success = false, message = "Feil i validering" });
        }
        public ActionResult addProducer(ProducerInfo producerinfo)
        {
            if (!isAdmin())
                return RedirectToAction("LogIn", "Main");

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