public BlogPostTagDto AssignTagToBlogPost(CreateBlogPostTag createBlogPostTag) { BlogPost blogPost = BlogDBContext.BlogPosts.Where(s => s.Id == createBlogPostTag.BlogPostId).FirstOrDefault(); if (blogPost == null) { throw new ArgumentNullException(); } Tag tag = BlogDBContext.Tags.Where(t => t.Id == createBlogPostTag.TagId).FirstOrDefault(); if (tag == null) { throw new ArgumentNullException(); } BlogPostTags blogPostTags = new BlogPostTags(); blogPostTags.BlogPostId = createBlogPostTag.BlogPostId; blogPostTags.TagId = createBlogPostTag.TagId; blogPostTags.BlogPost = blogPost; blogPostTags.Tag = tag; BlogPostTagDto blogPostTagDto = new BlogPostTagDto(); blogPostTagDto.BlogPostId = blogPost.Id; blogPostTagDto.TagId = tag.Id; BlogDBContext.BlogPostTags.Add(blogPostTags); BlogDBContext.SaveChanges(); return(blogPostTagDto); }
protected override void Seed(context context) { var tag1 = new Tags { slug = "andorid", tagName = "Andorid", createdAt = DateTime.Now, updatedAt = DateTime.Now }; var tag2 = new Tags { slug = "windows", tagName = "Windows", createdAt = DateTime.Now, updatedAt = DateTime.Now }; var tag3 = new Tags { slug = "code", tagName = "Code", createdAt = DateTime.Now, updatedAt = DateTime.Now }; context.tag.Add(tag1); context.tag.Add(tag2); context.tag.Add(tag3); var post1 = new BlogPost { slug = "heading-1", title = "Heading-1", description = "description 1", body = "body 1", createdAt = DateTime.Now, updatedAt = DateTime.Now, }; var post2 = new BlogPost { slug = "heading-2", title = "Heading-2", description = "description 2", body = "body 2", createdAt = DateTime.Now, updatedAt = DateTime.Now, }; context.blogPost.Add(post1); context.blogPost.Add(post2); var postTag1 = new BlogPostTags { Post = post1, Tag = tag1, createdAt = DateTime.Now, updatedAt = DateTime.Now }; var postTag2 = new BlogPostTags { Post = post1, Tag = tag2, createdAt = DateTime.Now, updatedAt = DateTime.Now }; var postTag3 = new BlogPostTags { Post = post2, Tag = tag2, createdAt = DateTime.Now, updatedAt = DateTime.Now }; var postTag4 = new BlogPostTags { Post = post2, Tag = tag3, createdAt = DateTime.Now, updatedAt = DateTime.Now }; context.blogPostTag.Add(postTag1); context.blogPostTag.Add(postTag2); context.blogPostTag.Add(postTag3); context.blogPostTag.Add(postTag4); base.Seed(context); }
public async Task <BlogPostViewModel> Create(BlogPost post) { post.CreatedAt = DateTime.UtcNow; post.UpdatedAt = post.CreatedAt; _context.BlogPosts.Add(post); if (post.TagList == null || post.TagList.Count == 0) { goto noTags; } post.BlogPostTags = new List <BlogPostTags>(); var dbTags = _context.Tags.Select(x => x.TagId); foreach (var tag in post.TagList) { if (!dbTags.Contains(tag)) { _context.Tags.Add(new Tags() { TagId = tag }); } BlogPostTags postTag = new BlogPostTags() { TagId = tag, Slug = post.Slug }; post.BlogPostTags.Add(postTag); } noTags : await _context.SaveChangesAsync(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <BlogPost, BlogPostViewModel>(); }); IMapper mapper = config.CreateMapper(); var postVM = mapper.Map <BlogPostViewModel>(post); return(postVM); }
public IHttpActionResult PostNewPost(dynamic blogPost) { // deserializing json object into a view model blogPostVM model = JsonConvert.DeserializeObject <blogPostVM>(blogPost.blogPost.ToString()); //function in "blogPostVM" class that checks if everthing is ok if (model.CheckRequired()) { return(BadRequest("Invalid data")); } //helperClass is a static class that contains functions that will manuiplate with data and return things we need string newSlug = helperClasses.createSlug(model.title); if (ctx.blogPost.Where(x => x.slug == newSlug).SingleOrDefault() != null) { return(BadRequest("Post already exists")); } //creating a new list of tags List <Tags> tagList = new List <Tags>(); //adding tags to "Tags" table (if they don't already exist) foreach (string s in blogPost.blogPost.tagList) { Tags tagFromDb = ctx.tag.Where(x => x.tagName == s).SingleOrDefault(); if (tagFromDb == null) { Tags tag = new Tags(); tag.slug = helperClasses.createSlug(s); tag.tagName = s; tag.createdAt = DateTime.Now; tag.updatedAt = DateTime.Now; ctx.tag.Add(tag); ctx.SaveChanges(); tagList.Add(tag); } else { tagList.Add(tagFromDb); } } //creating new post BlogPost newPost = new BlogPost(); newPost.slug = helperClasses.createSlug(model.title); newPost.title = model.title; newPost.description = model.description; newPost.body = model.body; newPost.createdAt = DateTime.Now; newPost.updatedAt = DateTime.Now; ctx.blogPost.Add(newPost); ctx.SaveChanges(); //joining tags to post in "BlogPostTags" table (many to many relationship with composite primary key) foreach (Tags tag in tagList) { BlogPostTags blogPostTag = new BlogPostTags(); blogPostTag.PostId = newPost.Id; blogPostTag.TagId = tag.Id; blogPostTag.createdAt = DateTime.Now; blogPostTag.updatedAt = DateTime.Now; ctx.blogPostTag.Add(blogPostTag); } ctx.SaveChanges(); return(Ok(viewPosts(newPost.slug))); }