public void Can_Filter_Blogposts_By_Category()
 {
     // Arrange: Given a series of blogs associated with a categories, a repository that manages
     // the access to those blogposts, and a blog controller
     var category = new Category {CategoryID = 1, Name = "category 1"};
     var category2 = new Category {CategoryID = 2, Name = "category 2"};
     var blogPost = new BlogPost { ContentID = 1 };
     var blogPost2 = new BlogPost() { ContentID = 2 };
     var blogPost3 = new BlogPost() { ContentID = 3 };
     blogPost.Categories.Add(category);
     blogPost2.Categories.Add(category2);
     blogPost3.Categories.Add(category);
     var moqBlogPostRepository = UnitTestHelpers.MockBlogPostRepositoryReturnsMoqObject(blogPost, blogPost2,
                                                                                        blogPost3);
     moqBlogPostRepository.
         Setup(x => x.GetQueryableBlogPostByCategory(category.Name)).        // when a user wants to filter by category
         Returns((new List<BlogPost> {blogPost, blogPost3}).AsQueryable());  // They will obtain the filtered blogposts
     var controller = new BlogController(moqBlogPostRepository.Object);
     // Act: when the user clicks on category
     var result = controller.Category("category-1");
     // Assert: the result will be a view with a list of blogposts filtered by category
     var model = ((BlogPostViewModel) result.ViewData.Model).BlogPostPagedList;
     model.Count.ShouldEqual(2);
     model.Single(x => x.ContentID == 1).ContentID.ShouldEqual(1);
     model.Single(x => x.ContentID == 3).ContentID.ShouldEqual(3);
     Assert.IsNull(model.FirstOrDefault(x => x.ContentID == 2));
 }
 /// <summary>
 /// Adds a category to the context
 /// </summary>
 /// <param name="category"></param>
 public void AddCategory(Category category)
 {
     // format category name
     category.Name = category.Name.Trim();
     // add category to context
     GetUnitOfWork.Categories.AddObject(category);
 }
 public void SaveCategory(Category category)
 {
     if (category.CategoryId == 0)
     {
         _context.Categories.Add(category);
     }
     _context.SaveChanges();
 }
 public Category Add(Category category)
 {
     if (category.Id == 0)
     {
         context.Categories.Add(category);
         context.SaveChanges();
         return category;
     }
     return null;
 }
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                _categoryRepository.Update(category);
                _unitOfWork.Commit();
                return RedirectToAction("Index", "Home");
            }

            return View();
        }
        public ActionResult Create(CreateModelView modelView)
        {
            if (!ModelState.IsValid)
                return View(modelView);

            var category = new Category
            {
                Name = modelView.CategoryName
            };

            _categoryRepository.Create(category);
            _unitOfWork.Commit();

            return RedirectToAction("Index");
        }
 protected void DatabaseInitialization()
 {
     if (_repository.Categories.Any()) return;
     for (var i = 0; i < 15; i++)
     {
         var cat = new Category();
         var random = new Random();
         var randomnext = random.Next(2, 5);
         cat.CategoryName = String.Format("LeftMenu номер {0}", i);
         for (var j = 0; j < randomnext; j++)
         {
             var sub = new Subcategory { SubcategoryName = String.Format("TopMenu номер {0}", j) };
             cat.Subcatigories.Add(sub);
         }
         _repository.SaveCategory(cat);
     }
 }
 public void Can_Get_A_Related_List_Of_BLogPosts()
 {
     // Arrange: Given a series of blogposts that are related by tags and categories, a repository
     // that manages those blogposts and a blog controller
     // categories
     var category = new Category() { CategoryID = 1, Name = "category 1" };
     var category2 = new Category() { CategoryID = 2, Name = "category 2" };
     // blogposts
     var blogPost = new BlogPost() { ContentID = 1 , Title = "blogPost 1", Path="blogpost-1", PublishingDate = DateTime.Now};
     var blogPost2 = new BlogPost() { ContentID = 2 };
     var blogPost3 = new BlogPost() { ContentID = 3 };
     var blogPost4 = new BlogPost()  { ContentID = 4 };
     // add categories to blogposts
     blogPost.Categories.Add(category);
     blogPost2.Categories.Add(category2);
     blogPost3.Categories.Add(category);
     // create mock repository
     var moqBlogPostRepository = UnitTestHelpers.MockBlogPostRepositoryReturnsMoqObject(blogPost, blogPost2,
                                                                                        blogPost3);
     const int numberOfRelatedPosts = 5;
     //moq get by id
     moqBlogPostRepository.Setup(x => x.GetByPath(blogPost.PublishingDate.Year,
                                     blogPost.PublishingDate.Month,
                                     blogPost.PublishingDate.Day, blogPost.Path)).Returns(blogPost);
     //moq getQueryableRelatedBlogPosts
     moqBlogPostRepository.
         Setup(x => x.GetListOfRelatedBlogPosts(blogPost, numberOfRelatedPosts)). // when a user wants to get related posts
         Returns((new List<BlogPost> { blogPost3 }));                  // They will obtain the related posts
     var controller = new BlogController(moqBlogPostRepository.Object);
     // Act: when the user goes into the detail screen
     var result = controller.Details(blogPost.PublishingDate.Year,
                                     blogPost.PublishingDate.Month,
                                     blogPost.PublishingDate.Day,
                                     blogPost.Path);
     // Assert: then he can see a series of related blogposts
     var viewResult = ((ViewResult) result);
     var model = ((BlogPostDetailModel) viewResult.ViewData.Model);
     model.HasRelatedBlogPosts.ShouldEqual(true);
     model.RelatedBlogPosts.Count.ShouldEqual(1);
 }
 public void GivenIHaveASeriesOfCategories()
 {
     Category1 = new Category {CategoryID = 1, Name = "Category 1"};
         Category2 = new Category {CategoryID = 2, Name = "Category 2"};
         Category3 = new Category {CategoryID = 3, Name = "Category 3"};
 }
Exemple #10
0
 public static MvcHtmlString CategoryLink(this HtmlHelper helper, Category category)
 {
     return helper.ActionLink(category.Name, "Category", "Post",
         new { category = category.UrlSlug },
         new { title = String.Format("See all posts in {0}", category.Name) });
 }
 public void GivenANewCategory()
 {
     CategoryNew = new Category {CategoryID = 0, Name = "new category"};
 }
 public void DeleteCategory(Category categories)
 {
     _context.Categories.Remove(categories);
     _context.SaveChanges();
 }
 public void Update(Category category)
 {
     _domainContext.Update(category);
 }
 public void Create(Category category)
 {
     _domainContext.Add(category);
 }
 public ActionResult Categories(Category category)
 {
     if (ModelState.IsValid)
     {
         // add to context
         BlogPostRepo.AddCategory(category);
         // submit changes
         // BlogPostRepo.SubmitChanges();
         // redirect to category management index
         TempData[vinCMS.Infraestructure.Constants.VIEW_MESSAGE] = SUCCESS_CATEGORY_ADD;
         return RedirectToAction("categories");
     }
     else
     {
         // return the same view
         var catViewModel = new AdminCategoryViewModel
         {
             NewCategory = category,
             PagedListCategories = new PagedList.StaticPagedList<Category>(
                 BlogPostRepo.GetQueryableOrderedCategories().Skip(0).Take(CATEGORIES_PER_PAGE).ToList(),
                 0,
                 CATEGORIES_PER_PAGE,
                 BlogPostRepo.GetQueryableCategories().Count()
                 )
         };
         // the view will show a series of validation error messages
         return View(catViewModel);
     }
 }
 /// <summary>
 /// Deletes a category that is passed as an argument to the method
 /// </summary>
 /// <param name="category"></param>
 public void DeleteCategory(Category category)
 {
     GetUnitOfWork.Categories.DeleteObject(category);
 }
 public void GivenASeriesOfCategories()
 {
     Category1 = new Category {CategoryID = 1};
     Category2 = new Category {CategoryID = 2};
 }
 public void UpdateCategory(Category category)
 {
     context.Entry(category).State = System.Data.Entity.EntityState.Modified;
     context.SaveChanges();
 }