public void Add() { PostsRepo postGet = new PostsRepo(); List <Post> junkPost = new List <Post>(); junkPost.Add(postGet.GetAll().Posts.FirstOrDefault()); Tag toAdd = new Tag { TagName = "#Testerized", Posts = junkPost }; TagsResponse response = repo.Add(toAdd); 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); }
public TagsResponse Add(Tag tag) { var context = new PersonalBlogEntities(); if (context.Tags.Contains(tag)) { var response = new TagsResponse(); response.Success = false; response.Message = $"The tag {tag.TagName} already exists in the database."; response.Tags.Add(tag); return(response); } else { return(repo.Add(tag)); } }