public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } Post = await _postService.GetPostById(id); if (Post == null) { return(NotFound()); } ViewData["PostImage"] = Post.PostImage ?? "no-image.png"; Post.PostTitleInBrowser = TextConvertor.ReplaceLetters(Post.PostTitleInBrowser, '-', ' '); ViewData["GroupId"] = new SelectList(_groupService.GetAllGroup(), "GroupId", "GroupTitle"); //ViewData["Author"] = new SelectList(_userService.GetAllUsers(), "UserId", "UserName"); var tagsPost = ""; foreach (var tag in _postTagService.GetAllTagsByPostId(id)) { tagsPost += tag.Tag.TagTitle + ","; } ViewData["Tags"] = tagsPost.Length == 0 ? null : tagsPost.Substring(0, tagsPost.Length - 1); return(Page()); }
public async Task UpdatePostAsync(Post post, IFormFile postImageUp, string tags, string oldImage) { var postTags = _postTagService.GetAllTagsByPostId(post.PostId); foreach (var tagTag in postTags) { _postTagService.RemoveTagFromPostTagByPostId(post.PostId, tagTag.TagId); } await _context.SaveChangesAsync(); post.PostUpdateDate = DateTime.Now; post.PostTitleInBrowser = TextConvertor.ReplaceLetters(TextConvertor.FixingText(post.PostTitleInBrowser), ' ', '-'); post.PostImage = postImageUp == null ? oldImage : ImageTools.UploadImageNormal(oldImage, postImageUp, "no-image.png", "wwwroot/assets/posts/image", true, "wwwroot/assets/posts/thumb", 240); if (tags != null) { var postTagAsArray = TextConvertor.TextToArray(tags, ","); var initialMiddlePostTagTableList = new List <PostTag>(); foreach (var tag in postTagAsArray) { var currentTag = TextConvertor.FixingText(tag); if (!await _tagService.ExistTag(currentTag)) { var newTagForSaveToTagsTable = new Tag { TagTitle = currentTag }; _tagService.AddTag(newTagForSaveToTagsTable); initialMiddlePostTagTableList.Add(InitialMiddlePostTagTable(post.PostId, newTagForSaveToTagsTable.TagId)); } else { var existTagInTagsTable = _tagService.GetTagByTagTitle(currentTag); initialMiddlePostTagTableList.Add(InitialMiddlePostTagTable(post.PostId, existTagInTagsTable.TagId)); } post.PostTags = initialMiddlePostTagTableList; foreach (var pt in initialMiddlePostTagTableList) { await _context.PostTags.AddAsync(pt); } } } _context.Posts.Attach(post).State = EntityState.Modified; await SaveChangeAsync(); }