Пример #1
0
		public void Delete(Category category)
		{
			var forums = _forumRepository.GetAll().Where(f => f.CategoryID == category.CategoryID);
			foreach (var forum in forums)
				_forumRepository.UpdateCategoryAssociation(forum.ForumID, null);
			_categoryRepository.Delete(category.CategoryID);
		}
Пример #2
0
		public void Delete()
		{
			var service = GetService();
			var cat = new Category(123);
			service.Delete(cat);
			_mockCategoryRepo.Verify(c => c.Delete(cat.CategoryID), Times.Once());
		}
Пример #3
0
		public Category Get(int categoryID)
		{
			Category category = null;
			_sqlObjectFactory.GetConnection().Using(c => c.Command("SELECT CategoryID, Title, SortOrder FROM pf_Category WHERE CategoryID = @CategoryID")
				.AddParameter("@CategoryID", categoryID)
				.ExecuteReader()
				.ReadOne(r => category = new Category(r.GetInt32(0)) { Title = r.GetString(1), SortOrder = r.GetInt32(2) }));
			return category;
		}
Пример #4
0
		public Category Create(string newTitle, int sortOrder)
		{
			var categoryID = 0;
			_sqlObjectFactory.GetConnection().Using(c => categoryID = Convert.ToInt32(c.Command("INSERT INTO pf_Category (Title, SortOrder) VALUES (@Title, @SortOrder)")
				.AddParameter("@Title", newTitle)
				.AddParameter("@SortOrder", sortOrder)
				.ExecuteAndReturnIdentity()));
			var category = new Category(categoryID) { Title = newTitle, SortOrder = sortOrder };
			return category;
		}
Пример #5
0
		public void Update(Category category)
		{
			var result = 0;
			_sqlObjectFactory.GetConnection().Using(c => result = c.Command("UPDATE pf_Category SET Title = @Title, SortOrder = @SortOrder WHERE CategoryID = @CategoryID")
				.AddParameter("@Title", category.Title)
				.AddParameter("@SortOrder", category.SortOrder)
				.AddParameter("@CategoryID", category.CategoryID)
				.ExecuteNonQuery());
			if (result != 1)
				throw new Exception(String.Format("Can't update category with ID {0} because it does not exist.", category.CategoryID));
		}
Пример #6
0
		private void ChangeCategorySortOrder(Category category, int change)
		{
			var categories = GetAll();
			if (category != null)
				categories.Single(c => c.CategoryID == category.CategoryID).SortOrder += change;
			var sorted = categories.OrderBy(c => c.SortOrder).ToList();
			for (var i = 0; i < sorted.Count; i++)
			{
				sorted[i].SortOrder = i * 2;
				_categoryRepository.Update(sorted[i]);
			}
		}
		public void AllCollectionsPersist()
		{
			var c1 = new Category(1);
			var c2 = new Category(2);
			var f1 = new Forum(1) { CategoryID = null };
			var f2 = new Forum(2) { CategoryID = 1 };
			var f3 = new Forum(3) { CategoryID = 2 };
			var cats = new List<Category> { c1, c2 };
			var forums = new List<Forum> { f1, f2, f3 };
			var container = new CategorizedForumContainer(cats, forums);
			Assert.AreEqual(cats, container.AllCategories);
			Assert.AreEqual(forums, container.AllForums);
		}
Пример #8
0
		public void DeleteResetsForumCatIDsToNull()
		{
			var service = GetService();
			var cat = new Category(123);
			var f1 = new Forum(1) { CategoryID = cat.CategoryID };
			var f2 = new Forum(2) { CategoryID = cat.CategoryID };
			var f3 = new Forum(3) { CategoryID = 456 };
			var forums = new List<Forum> {f1, f2, f3};
			_mockForumRepo.Setup(f => f.GetAll()).Returns(forums);
			service.Delete(cat);
			_mockForumRepo.Verify(f => f.UpdateCategoryAssociation(1, null), Times.Once());
			_mockForumRepo.Verify(f => f.UpdateCategoryAssociation(2, null), Times.Once());
			_mockForumRepo.Verify(f => f.UpdateCategoryAssociation(3, null), Times.Never());
		}
		public void ForumsAppearInCategories()
		{
			var c1 = new Category(1) { Title = "Cat1" };
			var c2 = new Category(2) { Title = "Cat2" };
			var f1 = new Forum(1) { CategoryID = null };
			var f2 = new Forum(2) { CategoryID = 1 };
			var f3 = new Forum(3) { CategoryID = 2 };
			var cats = new List<Category> { c1, c2 };
			var forums = new List<Forum> { f1, f2, f3 };
			var container = new CategorizedForumContainer(cats, forums);
			Assert.True(container.CategoryDictionary[c1].Contains(f2));
			Assert.True(container.CategoryDictionary[c2].Contains(f3));
			Assert.False(container.CategoryDictionary[c1].Contains(f1));
			Assert.False(container.CategoryDictionary[c1].Contains(f3));
		}
		public void CategoriesInCorrectOrder()
		{
			var c1 = new Category(1) { SortOrder = 5 };
			var c2 = new Category(2) { SortOrder = 1 };
			var c3 = new Category(3) { SortOrder = 3 };
			var f1 = new Forum(1) { CategoryID = 1 };
			var f2 = new Forum(2) { CategoryID = 2 };
			var f3 = new Forum(3) { CategoryID = 3 };
			var cats = new List<Category> {c1, c2, c3};
			var forums = new List<Forum> {f1, f2, f3};
			var container = new CategorizedForumContainer(cats, forums);
			Assert.True(container.CategoryDictionary.ToArray()[0].Key == c2);
			Assert.True(container.CategoryDictionary.ToArray()[1].Key == c3);
			Assert.True(container.CategoryDictionary.ToArray()[2].Key == c1);
		}
		public void UncategorizedForumsShowUpOnProperty()
		{
			var c1 = new Category(1);
			var c2 = new Category(2);
			var f1 = new Forum(1) {CategoryID = null};
			var f2 = new Forum(2) {CategoryID = 1};
			var f3 = new Forum(3) {CategoryID = 2};
			var f4 = new Forum(4) {CategoryID = 0};
			var cats = new List<Category> {c1, c2};
			var forums = new List<Forum> {f1, f2, f3, f4};
			var container = new CategorizedForumContainer(cats, forums);
			Assert.True(container.UncategorizedForums.Contains(f1));
			Assert.False(container.UncategorizedForums.Contains(f2));
			Assert.False(container.UncategorizedForums.Contains(f3));
			Assert.True(container.UncategorizedForums.Contains(f4));
		}
Пример #12
0
		public void Create()
		{
			const string newTitle = "new category";
			var cat1 = new Category(123) { SortOrder = 0 };
			var cat2 = new Category(456) { SortOrder = 2 };
			var cat3 = new Category(789) { SortOrder = 4 };
			var cat4 = new Category(1000) { SortOrder = 6 };
			var newCat = new Category(999) {Title = newTitle, SortOrder = -2};
			var cats = new List<Category> { cat1, cat2, cat3, cat4, newCat };
			var service = GetService();
			_mockCategoryRepo.Setup(c => c.GetAll()).Returns(cats);
			_mockCategoryRepo.Setup(c => c.Create(newTitle, -2)).Returns(newCat);
			var result = service.Create(newTitle);
			Assert.AreEqual(0, result.SortOrder);
			Assert.AreEqual(999, result.CategoryID);
			Assert.AreEqual(newTitle, result.Title);
			_mockCategoryRepo.Verify(c => c.Create(newTitle, -2), Times.Once());
			Assert.AreEqual(0, newCat.SortOrder);
			Assert.AreEqual(2, cat1.SortOrder);
			Assert.AreEqual(4, cat2.SortOrder);
			Assert.AreEqual(6, cat3.SortOrder);
			Assert.AreEqual(8, cat4.SortOrder);
		}
Пример #13
0
		public void MoveCategoryDown(Category category)
		{
			const int change = 3;
			ChangeCategorySortOrder(category, change);
		}
Пример #14
0
		public void EditCategoryView()
		{
			var cat = new Category(123) { Title = "the cat", SortOrder = 2 };
			var controller = GetController();
			_catService.Setup(c => c.Get(cat.CategoryID)).Returns(cat);
			var result = controller.EditCategory(cat.CategoryID);
			_catService.Verify(c => c.Get(cat.CategoryID), Times.Once());
			Assert.AreSame(cat, result.ViewData.Model);
		}
		public void CategoryWithNoForumsDoesNotAppear()
		{
			var c1 = new Category(1) { Title = "Cat1" };
			var c2 = new Category(2) { Title = "Cat2" };
			var c3 = new Category(3) { Title = "Cat3" };
			var f1 = new Forum(1) { CategoryID = null };
			var f2 = new Forum(2) { CategoryID = 1 };
			var f3 = new Forum(3) { CategoryID = 2 };
			var cats = new List<Category> { c1, c2, c3 };
			var forums = new List<Forum> { f1, f2, f3 };
			var container = new CategorizedForumContainer(cats, forums);
			Assert.False(container.CategoryDictionary.ContainsKey(c3));
		}
Пример #16
0
		public void EditCategory()
		{
			var cat = new Category(123) { Title = "the cat", SortOrder = 2 };
			var controller = GetController();
			_catService.Setup(c => c.Get(cat.CategoryID)).Returns(cat);
			var result = controller.EditCategory(cat.CategoryID, "blah");
			_catService.Verify(c => c.Get(cat.CategoryID), Times.Once());
			_catService.Verify(c => c.UpdateTitle(cat, "blah"), Times.Once());
			Assert.IsInstanceOf<RedirectToRouteResult>(result);
		}
Пример #17
0
		public void MoveCategoryDown()
		{
			var cat = new Category(123) { Title = "the cat", SortOrder = 2 };
			var controller = GetController();
			_catService.Setup(c => c.Get(cat.CategoryID)).Returns(cat);
			controller.MoveCategoryDown(cat.CategoryID);
			_catService.Verify(c => c.MoveCategoryDown(cat), Times.Once());
		}
Пример #18
0
		public void UpdateTitle()
		{
			var savedCategory = new Category(789);
			var service = GetService();
			var cat = new Category(123) { Title = "old", SortOrder = 456 };
			_mockCategoryRepo.Setup(c => c.Update(cat)).Callback<Category>(category => savedCategory = category);
			service.UpdateTitle(cat, "new");
			_mockCategoryRepo.Verify(c => c.Update(It.IsAny<Category>()), Times.Once());
			Assert.AreEqual("new", savedCategory.Title);
			Assert.AreEqual(123, savedCategory.CategoryID);
			Assert.AreEqual(456, savedCategory.SortOrder);
		}
Пример #19
0
		public void UpdateTitle(Category category, string newTitle)
		{
			category.Title = newTitle;
			_categoryRepository.Update(category);
		}
Пример #20
0
		public void NewUp()
		{
			const int catID = 123;
			var cat = new Category(catID);
			Assert.AreEqual(catID, cat.CategoryID);
		}
Пример #21
0
		public void MoveUp()
		{
			var cat1 = new Category(123) {SortOrder = 0};
			var cat2 = new Category(456) {SortOrder = 2};
			var cat3 = new Category(789) {SortOrder = 4};
			var cat4 = new Category(1000) {SortOrder = 6};
			var cats = new List<Category> { cat1, cat2, cat3, cat4 };
			var service = GetService();
			_mockCategoryRepo.Setup(c => c.GetAll()).Returns(cats);
			service.MoveCategoryUp(cat3);
			_mockCategoryRepo.Verify(c => c.GetAll(), Times.Once());
			_mockCategoryRepo.Verify(c => c.Update(It.IsAny<Category>()), Times.Exactly(4));
			_mockCategoryRepo.Verify(c => c.Update(cat1), Times.Once());
			_mockCategoryRepo.Verify(c => c.Update(cat2), Times.Once());
			_mockCategoryRepo.Verify(c => c.Update(cat3), Times.Once());
			_mockCategoryRepo.Verify(c => c.Update(cat4), Times.Once());
			Assert.AreEqual(0, cat1.SortOrder);
			Assert.AreEqual(2, cat3.SortOrder);
			Assert.AreEqual(4, cat2.SortOrder);
			Assert.AreEqual(6, cat4.SortOrder);
		}
Пример #22
0
		public void DeleteCategory()
		{
			var cat = new Category(123) {Title = "the cat", SortOrder = 2};
			var controller = GetController();
			_catService.Setup(c => c.Get(cat.CategoryID)).Returns(cat);
			var result = controller.DeleteCategory(cat.CategoryID);
			_catService.Verify(c => c.Get(cat.CategoryID), Times.Once());
			_catService.Verify(c => c.Delete(cat), Times.Once());
			Assert.IsInstanceOf<RedirectToRouteResult>(result);
			Assert.AreEqual("Categories", result.RouteValues["Action"]);
		}