public void AddPost() { Post test = repo.GetByCategory(1).Posts.First(); test.PostId = 0; test.PostTitle = "HurrMurmuhFur"; PostsResponse response = repo.Add(test); Assert.AreEqual(true, response.Success); }
public PostsResponse Add(Post post) { var context = new PersonalBlogEntities(); var response = new PostsResponse(); if (string.IsNullOrEmpty(post.PostTitle)) { response.Success = false; response.Message = "The post title cannot be left blank."; } else if (post.CreatedDate < DateTime.Today.AddDays(1)) { response.Success = false; response.Message = "The post cannot have a creation date before the current date."; } //else if (!post.IsApproved) //{ // response.Success = false; // response.Message = "This post has content that violates our blogging policy."; //} else if (string.IsNullOrEmpty(post.PostBody)) { response.Success = false; response.Message = "The post body cannot be left blank."; } else if (context.Categories.FirstOrDefault(c => c.CategoryId == post.CategoryId) == null) { response.Success = false; response.Message = "That category is invalid"; } else { TagsRepo tagsRepo = new TagsRepo(); List <Tag> allTags = tagsRepo.GetAll().Tags.ToList(); List <Tag> tagsToAdd = post.Tags.AsEnumerable().Where(t => post.Tags.Any(postTag => postTag.TagName != t.TagName)).ToList(); foreach (Tag t in tagsToAdd) { tagsRepo.Add(t); } response = repo.Add(post); response.Message = $"The post \"{post.PostTitle}\" has been added to the database."; } return(response); }