Esempio n. 1
0
        public void EditCategory_Returns_View_Error_When_Category_With_Same_Name_Exists()
        {
            IntermediateCategory category = new IntermediateCategory();

            category.IntermediateCategoryId = 1;
            category.MainCategoryByCitiesId = 2;
            category.NameOfMainCategory     = "Zdrowie";
            category.Topic = new List <Topic>();



            Mock <IMainCategoryByCitiesRepository> repository = new Mock <IMainCategoryByCitiesRepository>();


            Mock <ITopicRepository> repositoryTopic = new Mock <ITopicRepository>();

            Mock <IIntermediateCategoryRepository> repositoryInter = new Mock <IIntermediateCategoryRepository>();

            repositoryInter.Setup(r => r.ChangeIntermediateCategory(It.IsAny <IntermediateCategory>())).Returns(false);


            AdminController controller = new AdminController(repositoryTopic.Object, repositoryInter.Object, repository.Object);

            ViewResult result = controller.EditCategory(category) as ViewResult;



            Assert.AreEqual("Error", result.ViewName);
            Assert.AreEqual("Category Exists", controller.ViewBag.ErrorMessage);
        }
        public ActionResult EditCategory(IntermediateCategory category)
        {
            if (!ModelState.IsValid)
            {
                return(View("EditCategory", category));
            }

            IntermediateCategory result = repositoryInter.Get(category.IntermediateCategoryId);

            //if(result.NameOfMainCategory=="Error")
            //{
            //    ViewBag.ErrorMessage = "Category with that Id does not exists";
            //    return View("Error");
            //}

            bool check = repositoryInter.ChangeIntermediateCategory(category);

            if (check)
            {
                return(RedirectToAction("ChangeCategories", new { controller = "Admin", action = "ChangeCategories", id = result.MainCategoryByCitiesId }));
            }
            else
            {
                ViewBag.ErrorMessage = "Category Exists";
                return(View("Error"));
            }
        }
Esempio n. 3
0
        public void EditCategory_Redirects_To_ChangeCategories()
        {
            IntermediateCategory category = new IntermediateCategory();

            category.IntermediateCategoryId = 1;
            category.MainCategoryByCitiesId = 2;
            category.NameOfMainCategory     = "Zdrowie";
            category.Topic = new List <Topic>();



            Mock <IMainCategoryByCitiesRepository> repository = new Mock <IMainCategoryByCitiesRepository>();


            Mock <ITopicRepository> repositoryTopic = new Mock <ITopicRepository>();

            Mock <IIntermediateCategoryRepository> repositoryInter = new Mock <IIntermediateCategoryRepository>();

            repositoryInter.Setup(r => r.ChangeIntermediateCategory(It.IsAny <IntermediateCategory>())).Returns(true);
            repositoryInter.Setup(x => x.Get(1)).Returns(category);

            AdminController controller = new AdminController(repositoryTopic.Object, repositoryInter.Object, repository.Object);

            RedirectToRouteResult result = controller.EditCategory(category) as RedirectToRouteResult;



            Assert.AreEqual(result.RouteValues["controller"], "Admin");
            Assert.AreEqual(result.RouteValues["action"], "ChangeCategories");
            Assert.AreEqual(result.RouteValues["id"], category.MainCategoryByCitiesId);
        }
        public ActionResult AddCategory(IntermediateCategory result)
        {
            //if(string.IsNullOrEmpty(result.NameOfMainCategory))
            //{
            //    ModelState.AddModelError("NameOfMainCategory", "Rubryka nie może być pusta");
            //}

            if (!ModelState.IsValid)
            {
                return(View("AddCategory", result));
            }

            bool DoesItExist = repositoryMain.AddIntermediateCategory(result);

            if (DoesItExist == true)
            {
                ViewBag.ErrorMessage = "Category Exists";
                return(View("Error"));
            }
            else
            {
                //return View("SuccesAddingCategory");
                return(RedirectToAction("ChangeCategories", new { controller = "Admin", action = "ChangeCategories", id = result.MainCategoryByCitiesId }));
            }
        }
        public ActionResult AddCategory(int id)
        {
            IntermediateCategory category = new IntermediateCategory();

            category.MainCategoryByCitiesId = id;


            return(View(category));
        }
Esempio n. 6
0
        public void AddCategory_Has_Specified_Id()
        {
            Mock <IMainCategoryByCitiesRepository> repository      = new Mock <IMainCategoryByCitiesRepository>();
            Mock <ITopicRepository> repositoryTopic                = new Mock <ITopicRepository>();
            Mock <IIntermediateCategoryRepository> repositoryInter = new Mock <IIntermediateCategoryRepository>();

            AdminController controller = new AdminController(repositoryTopic.Object, repositoryInter.Object, repository.Object);

            IntermediateCategory category = (IntermediateCategory)((controller.AddCategory(1) as ViewResult).Model);

            Assert.AreEqual(1, category.MainCategoryByCitiesId);
        }
        public ActionResult EditCategory(int id)
        {
            IntermediateCategory result = repositoryInter.Get(id);

            if (result.NameOfMainCategory == "Error")
            {
                ViewBag.ErrorMessage = "Category with that Id does not exists";
                return(View("Error"));
            }


            return(View(result));
        }
Esempio n. 8
0
        public bool ChangeIntermediateCategory(IntermediateCategory category)
        {
            if (context.IntermediateCategories.Where(x => x.NameOfMainCategory.ToLower() == category.NameOfMainCategory.ToLower()).Any())
            {
                return(false);
            }

            IntermediateCategory result = context.IntermediateCategories.Find(category.IntermediateCategoryId);

            result.NameOfMainCategory = category.NameOfMainCategory;
            context.SaveChanges();

            return(true);
        }
        public ActionResult DeleteCategory(int id)
        {
            IntermediateCategory category = repositoryInter.Get(id);
            //ViewBag.CategoryName = category.NameOfMainCategory;

            bool result = repositoryInter.Remove(category);

            if (result == false)
            {
                ViewBag.ErrorMessage = "You have to keep at least one Category";
                return(View("Error"));
            }


            //return View("DeleteCategory");
            return(RedirectToAction("ChangeCategories", new { controller = "Admin", action = "ChangeCategories", id = category.MainCategoryByCitiesId }));
        }
Esempio n. 10
0
        public bool AddIntermediateCategory(IntermediateCategory category)
        {
            bool result = context.IntermediateCategories.Where(n => n.NameOfMainCategory.ToLower() == category.NameOfMainCategory.ToLower()).Any();

            if (result == true)
            {
                return(true);
            }
            else
            {
                MainCategoryByCities main = context.MainCategoryByCities.Find(category.MainCategoryByCitiesId);
                main.IntermediateCategory.Add(category);
                context.SaveChanges();

                return(false);
            }
        }
Esempio n. 11
0
        public void Add_Category_Returns_True()
        {
            Mock <IMainCategoryByCitiesRepository> repository = new Mock <IMainCategoryByCitiesRepository>();

            repository.Setup(r => r.AddIntermediateCategory(It.IsAny <IntermediateCategory>())).Returns(true);
            Mock <ITopicRepository> repositoryTopic = new Mock <ITopicRepository>();
            Mock <IIntermediateCategoryRepository> repositoryInter = new Mock <IIntermediateCategoryRepository>();

            IntermediateCategory category = new IntermediateCategory();

            AdminController controller = new AdminController(repositoryTopic.Object, repositoryInter.Object, repository.Object);

            ViewResult result   = controller.AddCategory(category) as ViewResult;
            string     viewName = result.ViewName;

            Assert.AreEqual("Error", viewName);
        }
Esempio n. 12
0
        public IntermediateCategory Get(int id)
        {
            IntermediateCategory category;

            category = context.IntermediateCategories.Find(id);

            if (category == null)
            {
                category = new IntermediateCategory();
                category.NameOfMainCategory = "Error";
                category.Topic = new List <Topic>();
                return(category);
            }
            else
            {
                return(category);
            }
        }
Esempio n. 13
0
        public void Edit_Category_Dont_Save_InValid_Changes()
        {
            Mock <ITopicRepository> repositoryTopic = new Mock <ITopicRepository>();
            Mock <IIntermediateCategoryRepository> repositoryInter = new Mock <IIntermediateCategoryRepository>();
            Mock <IMainCategoryByCitiesRepository> repositoryMain  = new Mock <IMainCategoryByCitiesRepository>();

            AdminController      controller = new AdminController(repositoryTopic.Object, repositoryInter.Object, repositoryMain.Object);
            IntermediateCategory category   = new IntermediateCategory()
            {
                NameOfMainCategory = null
            };

            controller.ModelState.AddModelError("NameOfMainCategory", "Pole nie może pozostawać puste");

            ActionResult result     = controller.EditCategory(category);
            ViewResult   resultView = (ViewResult)result;

            repositoryInter.Verify(x => x.ChangeIntermediateCategory(It.IsAny <IntermediateCategory>()), Times.Never);
            Assert.AreEqual(resultView.ViewName, "EditCategory");
        }
Esempio n. 14
0
        public ViewResult Show_Topics(int id)
        {
            ViewBag.IntermediateCategory_Id = id;


            //List<Topic> TopicList = repositoryInter.Get(id).Topic.ToList();


            IntermediateCategory category = repositoryInter.Get(id);

            if (category.NameOfMainCategory == "Error")
            {
                return(View("Error"));
            }
            else
            {
                List <Topic> TopicList = category.Topic.ToList();
                return(View("Show_Topics", TopicList));
            }
        }
Esempio n. 15
0
        public void Add_Category_Returns_False()
        {
            Mock <IMainCategoryByCitiesRepository> repository = new Mock <IMainCategoryByCitiesRepository>();

            repository.Setup(r => r.AddIntermediateCategory(It.IsAny <IntermediateCategory>())).Returns(false);
            Mock <ITopicRepository> repositoryTopic = new Mock <ITopicRepository>();
            Mock <IIntermediateCategoryRepository> repositoryInter = new Mock <IIntermediateCategoryRepository>();

            IntermediateCategory category = new IntermediateCategory();

            category.MainCategoryByCitiesId = 12;

            AdminController controller = new AdminController(repositoryTopic.Object, repositoryInter.Object, repository.Object);

            RedirectToRouteResult result = controller.AddCategory(category) as RedirectToRouteResult;


            Assert.AreEqual(result.RouteValues["action"], "ChangeCategories");
            Assert.AreEqual(result.RouteValues["controller"], "Admin");
            Assert.AreEqual(result.RouteValues["id"], category.MainCategoryByCitiesId);
        }
Esempio n. 16
0
        public bool Remove(IntermediateCategory entity)
        {
            //MainCategoryByCities main = context.MainCategoryByCities.Find(entity.MainCategoryByCitiesId);
            //context.IntermediateCategories.Remove(entity);
            //main.IntermediateCategory.Remove(entity);

            //context.SaveChanges();

            MainCategoryByCities main = context.MainCategoryByCities.Find(entity.MainCategoryByCitiesId);

            if (main.IntermediateCategory.Count <= 1)
            {
                return(false);
            }
            else
            {
                context.IntermediateCategories.Remove(entity);
                main.IntermediateCategory.Remove(entity);

                context.SaveChanges();
                return(true);
            }
        }
Esempio n. 17
0
        public void EditCategory_Check_If_Category_Exists_Returns_Category()
        {
            Mock <IMainCategoryByCitiesRepository> repository = new Mock <IMainCategoryByCitiesRepository>();


            Mock <ITopicRepository> repositoryTopic = new Mock <ITopicRepository>();

            Mock <IIntermediateCategoryRepository> repositoryInter = new Mock <IIntermediateCategoryRepository>();

            repositoryInter.Setup(r => r.Get(2)).Returns(new IntermediateCategory()
            {
                IntermediateCategoryId = 2, NameOfMainCategory = "Zdrowie", MainCategoryByCitiesId = 1
            });


            AdminController controller = new AdminController(repositoryTopic.Object, repositoryInter.Object, repository.Object);

            IntermediateCategory result = (IntermediateCategory)(controller.EditCategory(2) as ViewResult).Model;



            Assert.AreEqual("Zdrowie", result.NameOfMainCategory);
        }
Esempio n. 18
0
 public void Add(IntermediateCategory entity)
 {
     context.IntermediateCategories.Add(entity);
     context.SaveChanges();
 }