public async Task <PostVM> CreateComment(CreateCommentRequest request, ApplicationUser applicationUser) { var post = await _context.Posts.Where(x => x.PostID == request.PostID).FirstOrDefaultAsync(); if (post == null) { return(null); } // Khởi tạo và add commnet vào bảng commnet var commnet = new Comment() { Content = request.Content, DateCreated = DateTime.Now, Author = applicationUser.FullName, Parent = 0, Id = applicationUser.Id, PostID = request.PostID }; _context.Comments.Add(commnet); await _context.SaveChangesAsync(); // Xử lý post view model hiển thị về cho user var postVM = _public_post_service.PublicDeatilPost(request.PostID); return(await postVM); }
public async Task <PostVM> CreatePost(CreatePostRequest request, string id, string fullName) { // Tên file hình ảnh có thêm ngày tháng năm tạo, được chuyển vào lưu tại thư mục // /Blog.API1/wwwRoot/Image //string wwwRoot = Path.GetFullPath(Directory.GetParent(Directory.GetCurrentDirectory()) + "\\Blog.API1\\wwwroot\\ImageNash"); //string wwwRoot = Path.GetFullPath(Directory.GetParent(Directory.GetCurrentDirectory()) // + "\\" // + Assembly.GetCallingAssembly().GetName().Name // + "\\wwwroot\\Image"); //string fileName = Path.Combine(wwwRoot, // (Path.GetFileNameWithoutExtension(request.ImageFileName.FileName) // + "_" + DateTime.Now.ToString("yymmss") // + Path.GetExtension(request.ImageFileName.FileName))); string fileName = CreateFileNamePath(request.ImageFileName); try { using (Stream stream = new FileStream(fileName, FileMode.Create)) { await request.ImageFileName.CopyToAsync(stream); } } catch (Exception ex) { throw ex; } var newPost = new Post() { Title = request.Title, Content = request.Content, DateCreated = DateTime.Now, Caption = request.Caption, Id = id, Author = fullName, ImageFileName = fileName }; _context.Add(newPost); int ketQua = await _context.SaveChangesAsync(); var postVM = new PostVM() { PostID = newPost.PostID, Title = newPost.Title, DateCreated = newPost.DateCreated, Caption = newPost.Caption, Author = newPost.Author, ImageFileName = Path.GetFileName(newPost.ImageFileName), Content = newPost.Content, ListComments = new List <CommentVM>() }; if (ketQua == 0) { postVM = null; } return(postVM); }