public async Task <IActionResult> GetPost(string Slug) { var post = await _context.Posts.FirstOrDefaultAsync(w => w.Slug == Slug); if (post == null) { return(NotFound()); } PostOut postOut = new PostOut { BlogPost = new PostOut.Post { Slug = post.Slug, Title = post.Title, Description = post.Description, Body = post.Body, CreatedAt = post.CreatedAt.ToString("yyyy-MM-ddThh:mm:ss.mmmZ"), UpdatedAt = post.UpdatedAt != null?post.UpdatedAt.Value.ToString("yyyy-MM-ddThh:mm:ss.mmmZ") : "n/a", Tags = await _context.PostTags.Where(w => w.PostId == post.Slug).Select(x => x.Tag.Name).ToListAsync() } }; return(Ok(postOut)); }
public async Task <IActionResult> Put(string slug, PostToDb update) { var post = await _context.Posts.Where(w => w.Slug == slug).FirstOrDefaultAsync(); if (post == null) { return(NotFound()); } if (!string.IsNullOrEmpty(update.blogPost.Title)) { var updateSlug = update.blogPost.Title; RegexOptions options = RegexOptions.None; Regex regex = new Regex("[ ]{2,}", options); updateSlug = regex.Replace(updateSlug, " "); updateSlug = updateSlug.Replace(" ", "_").ToLower(); //post.Slug = updateSlug; -- important rule of EF said that we can't change ID, so I will go in another way Post p = new Post // create new post with new slug and title { Slug = updateSlug, Body = post.Body, // update.blogPost.Body = maybe is empty so I will update body in next if statement CreatedAt = post.CreatedAt, Title = update.blogPost.Title, Description = post.Description }; await _context.Posts.AddAsync(p); var postsTags = _context.PostTags.Include(x => x.Tag).Where(w => w.PostId == slug).ToList(); // all tags related to choosen post foreach (var postTags in postsTags) // After I created new post, adding tags with new post slug { PostTag pT = new PostTag { PostId = p.Slug, TagId = postTags.TagId }; await _context.PostTags.AddAsync(pT); } //then delete post and related tags var deletePostTags = _context.PostTags.Where(w => w.PostId == slug).ToList(); // find all rows in table PostTags _context.PostTags.RemoveRange(deletePostTags); // delete all rows with wanted slug _context.Posts.Remove(post); // last but not least, removing post post = p; // post is global variable so I can use it further await _context.SaveChangesAsync(); } if (!string.IsNullOrEmpty(update.blogPost.Description)) { post.Description = update.blogPost.Description; } if (!string.IsNullOrEmpty(update.blogPost.Body)) { post.Body = update.blogPost.Body; } post.UpdatedAt = DateTime.Now; _context.Attach(post); //new post is already in database _context.Update(post); await _context.SaveChangesAsync(); // update Description and Body if its changed PostOut postOut = new PostOut { BlogPost = new PostOut.Post { Slug = post.Slug, Title = post.Title, Description = post.Description, Body = post.Body, CreatedAt = post.CreatedAt.ToString("yyyy-MM-ddThh:mm:ss.mmmZ"), UpdatedAt = post.UpdatedAt != null?post.UpdatedAt.Value.ToString("yyyy-MM-ddThh:mm:ss.mmmZ") : "n/a", Tags = await _context.PostTags.Where(w => w.PostId == post.Slug).Select(x => x.Tag.Name).ToListAsync() } }; return(Ok(postOut)); }