Example #1
0
		private async Task ReplaceTagsAsync(Post post, string[] tagEnumerable)
		{
			var tags = tagEnumerable.Select(x => HttpUtility.HtmlEncode(x.Trim().ToLower())).Distinct().ToArray();

			for (int i = 0; i < post.Tags.Count; i++)
			{
				if (!tags.Contains(post.Tags[i].Tag.ToLower().Trim()))
				{
					RemoveTagFromPost(post, post.Tags[i]);
					i--;
				}
				else
				{
					tags[Array.IndexOf(tags, post.Tags[i].Tag.ToLower().Trim())] = null;
				}
			}

			foreach (var tag in tags)
			{
				if (tag == null)
					continue;
				await AddTagToPostAsync(post, tag);
			}
		}
			public async Task Show()
			{
				var post = new Post();
				PostServiceMock.Setup(f => f.GetPostById("id"))
					.ReturnsAsync(post);

				var result = await Controller.Show("id") as ViewResult;
				Assert.IsNotNull(result);
				var model = result.Model as ShowModel;
				Assert.IsNotNull(model);
				Assert.AreSame(post, model.Post);
			}
Example #3
0
		/// <summary>
		/// Adds a tag to a post. Note that this method does not check for duplicate tags.
		/// </summary>
		/// <param name="post">Post to add the tag to</param>
		/// <param name="tagName">The name of the tag to be added</param>
		/// <returns></returns>
		private async Task AddTagToPostAsync(Post post, string tagName)
		{
			var tag = await _db.Tags.FirstOrDefaultAsync(x => x.Tag == tagName)
						?? new PostTag
						{
							Posts = new List<Post>(),
							Tag = tagName
						};
			tag.Posts.Add(post);
			post.Tags.Add(tag);
		}
Example #4
0
		private void RemoveTagFromPost(Post post, PostTag tag)
		{
			tag.Posts.Remove(post);
			post.Tags.Remove(tag);
			if (tag.Posts.Count == 0)
				_db.Tags.Remove(tag);
		}
Example #5
0
		private static void SortComments(ref Post post)
		{
			if (post == null)
				throw new ArgumentNullException();

			post.Comments = post.Comments.OrderByDescending(x => x.Posted).ToList();
		}
Example #6
0
		public async Task DeletePost(Post post)
		{
            if (post == null)
                throw new ArgumentNullException();

			_db.Posts.Remove(post);
			await _db.SaveChangesAsync();
		}
Example #7
0
		public async Task<Post> AddComment(string title, string content, IPrincipal user, Post post)
		{
			if (String.IsNullOrWhiteSpace(title))
				title = "Untitled";
            if (String.IsNullOrEmpty(title) | String.IsNullOrEmpty(content) | user == null | post == null)
                throw new ArgumentNullException();

			var appuser = await _userService.GetUserByIPrincipalAsync(user);
			var comment = new PostComment
			{
				Author = appuser,
				Content = content.FilterHtml(),
				Id = Guid.NewGuid(),
				OriginalContent = post,
				Posted = DateTime.Now,
				Title = HttpUtility.HtmlEncode(title)
			};
			post.Comments.Add(comment);

			await _db.SaveChangesAsync();
			SortComments(ref post);
			return post;
		}
Example #8
0
		public async Task<Guid> AddPost(string title, string content, string tags, IPrincipal user)
		{
            if(String.IsNullOrEmpty(title)|String.IsNullOrEmpty(content)|String.IsNullOrEmpty(tags)|user == null)
                throw new ArgumentNullException();

			var appuser = await _userService.GetUserByNameAsync(user.Identity.Name);
			var post = new Post 
 			{ 
 				Author = appuser, 
 				Comments = new List<PostComment>(), 
 				Content = content.ConvertNewlines(), 
 				Id = Guid.NewGuid(), 
 				LastEdited = DateTime.Now, 
 				Posted = DateTime.Now, 
 				Tags = new List<PostTag>(), 
 				Title = HttpUtility.HtmlEncode(title) 
 			}; 
 			_db.Posts.Add(post); 
 
 			foreach (var tag in tags.Split(',').Select(x => HttpUtility.HtmlEncode(x.Trim().ToLower())).Distinct().ToArray()) 
 				await AddTagToPostAsync(post, tag);

			await _db.SaveChangesAsync();
 			return post.Id; 
		}
Example #9
0
		public ShowModel(Post post)
		{
			Post = post;
		}