public virtual void AddTag(Tag tag) { if(Tags == null) Tags = new List<Tag>(); Tag existingTag = (Tags.Where(t => t.TagName == tag.TagName)).FirstOrDefault(); if (existingTag == null) { Tags.Add(tag); tag.Posts.Add(this); } }
public void AddPost_when_adding_a_tag_that_doesnot_exists_should_add_a_new_tag_with_id_equal_to_zero() { //Arrange SetupMocks(); Tag newtag = new Tag { Id = 0, TagName = "Newtag" }; Post post = CreateNewPost(); post.AddTag(newtag); //Act service.AddPost(post); //Assert Assert.IsNotNull(_AddedPost); Assert.AreEqual(1, _AddedPost.Tags.Count); Assert.AreSame(newtag, _AddedPost.Tags[0]); }
public void AddPost_when_adding_a_tag_that_already_exists_should_use_the_same_instance_of_the_tag() { //Arrange SetupMocks(); Tag newTag1 = new Tag { Id = 0, TagName = ".net" }; Tag newTag2 = new Tag { Id = 0, TagName = "misc" }; Post post = new Post(); post.AddTag(newTag1); post.AddTag(newTag2); //Act service.AddPost(post); //Assert Assert.IsNotNull(_AddedPost,"The post was not saved"); Assert.AreEqual(2, _AddedPost.Tags.Count); Tag tag1 = _AddedPost.Tags.Where(t => t.TagName == ".net").FirstOrDefault(); Tag tag2 = _AddedPost.Tags.Where(t => t.TagName == "misc").FirstOrDefault(); Assert.IsNotNull(tag1); Assert.IsNotNull(tag2); Assert.AreEqual(1, tag1.Id); Assert.AreEqual(2, tag2.Id); }
public virtual ActionResult ViewPostsByTag(string tagname, int pageNumber) { int totalPages = 0; //TODO: build pagination IList<Post> posts = postServices.GetPostsByTag(tagname, 25, 1, out totalPages); //TODO: show not found page if there are no records Tag tag = tagService.GetTagByNormalizedName(tagname); // create a dummy tag if the user has used an actual tag name // in the url and we are unable to find it if (tag == null) tag = new Tag { TagName = tagname, NormalizedTagName = tagname }; IList<PostSummary> postSummaryList = Mapper.Map<IList<Post>, PostSummary[]>(posts); PostsByTag pBT = new PostsByTag { Posts = postSummaryList, Tag = tag, TotalPages = totalPages}; return View(pBT); }
private static Post StructToPost(Post p, XmlRpcStruct x, bool publish) { /* * 1. struct { 2. string postid; 3. DateTime dateCreated; 4. string title; 5. string description; 6. string[] categories; 7. bool publish; 8. } */ if (p == null) { p = new Post(); p.Id = x.ContainsKey("postid") ? Convert.ToInt32(x["postid"]) : 0; } p.Approved = publish; p.Body = x["description"].ToString(); p.Title = x["title"].ToString(); if (x.ContainsKey("categories")) { IEnumerable categories = x["categories"] as IEnumerable; if (categories != null) { foreach(object category in categories) { if (category != null) { Tag tag = new Tag { TagName = category.ToString() }; p.AddTag(tag); } } } } return p; }