private Boolean ArePostAttributesEmpty(PostWithTags post) { if (post.Title == null && post.Description == null && post.Body == null) { return(true); } return(false); }
private Boolean ArePostAttributesFilledCorectly(PostWithTags post) { if (post.Title == null || post.Title == String.Empty || post.Description == null || post.Description == String.Empty || post.Body == null || post.Body == String.Empty) { return(false); } return(true); }
public async Task <ActionResult <Post> > Post([FromForm] PostWithTags pwt) { pwt.Post.Id = Guid.NewGuid(); IdentityUser currentUser = await _context.Users.FirstAsync(u => u.Email == User.Identity.Name); pwt.Post.IdentityUser = currentUser; pwt.Post.CreatedAt = DateTime.Now; pwt.Post.UpdatedAt = DateTime.Now; pwt.Post.Category = await _context.Categories.FirstAsync(p => p.Id == pwt.Post.CategoryId); var fileName = Guid.NewGuid().ToString() + ".html"; var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Posts", fileName); using (var stream = new StreamWriter(filePath)) { await stream.WriteAsync(pwt.Post.Text); } pwt.Post.Text = fileName; await _context.Posts.AddAsync(pwt.Post); char[] separators = { ' ', ',', ';' }; string[] tags = pwt.Tags.Split(separators); foreach (var t in tags) { PostsTags pt; Tag tag; if (_context.Tags.Where(tg => tg.Name == t).Count() <= 0) { tag = new Tag { Id = Guid.NewGuid(), Name = t }; await _context.Tags.AddAsync(tag); } else { tag = await _context.Tags.Where(tg => tg.Name == t).FirstAsync(); } await _context.PostsTags.AddAsync(new PostsTags { Id = Guid.NewGuid(), Tag = tag, Post = pwt.Post }); } await _context.SaveChangesAsync(); return(Redirect("/Posts/List")); }
private PostWithTags AddTagsToPost(Post oldPost) { PostWithTags newPost = new PostWithTags(); var listOfTags = FindTagsOfPost(oldPost); String[] arrayOfTags = {}; List <string> addTagsToPost = new List <string>(); foreach (var tag in listOfTags) { addTagsToPost.Add(tag); } String[] addedTags = addTagsToPost.Distinct().ToArray(); newPost.Slug = oldPost.Slug; newPost.Title = oldPost.Title; newPost.Description = oldPost.Description; newPost.Body = oldPost.Body; newPost.TagList = addedTags; newPost.CreatedAt = oldPost.CreatedAt; newPost.UpdatedAt = oldPost.UpdatedAt; return(newPost); }