public IEnumerable <Comment> GetAllComments(string post_id) { var post_comments_collection = dataContext.getConnection().GetCollection <Post_Comment>("Post_Comment"); Post_Comment post_Comments = post_comments_collection.Find(x => x.post_id == ObjectId.Parse(post_id)).SingleAsync().Result; List <Comment> comments = post_Comments.comments; foreach (Comment c in comments) { User u = um.GetUser_Detail(c.owner._id); c.owner.user_name = u.first_name + " " + u.last_name; c.owner.user_picture = u.profile_img; } return(comments); }
// POST: api/Users public IHttpActionResult AddComment(Post_Comment Comment) { if (Comment != null) { //this is for an error that it can serialize the data that we send through db.Configuration.ProxyCreationEnabled = false; try { db.Post_Comment.Add(Comment); db.SaveChanges(); return(Ok(Comment)); } catch (Exception e) { return(Ok()); } } else { return(NotFound()); } }
public async Task <IActionResult> Create([FromForm] CreatePostCommentDto commentDto) { var folderName = DateTime.Now.ToString("yyyy-MM-dd"); var imageUploadPath = Path.Combine(_hostingEnvironment.WebRootPath, "Media/Images/"); var voiceUploadPath = Path.Combine(_hostingEnvironment.WebRootPath, "Media/Voice/"); switch (commentDto.Type) { case CommentType.Image: if (commentDto.File != null) { if (!Directory.Exists(Path.Combine(imageUploadPath, folderName))) { Directory.CreateDirectory(Path.Combine(imageUploadPath, folderName)); } if (commentDto.File.Length > 0) { string extension = Path.GetExtension(commentDto.File.FileName); var filePath = FileDetail(imageUploadPath, Path.Combine(folderName, DateTime.Now.ToFileTime() + "_" + UserId + extension)); using (var fileStream = new FileStream(filePath.FilePath, FileMode.Create)) { await commentDto.File.CopyToAsync(fileStream); commentDto.Comment = filePath.FileName; } } } break; case CommentType.Voice: if (commentDto.File != null) { if (!Directory.Exists(Path.Combine(voiceUploadPath, folderName))) { Directory.CreateDirectory(Path.Combine(voiceUploadPath, folderName)); } var voicePath = FileDetail(voiceUploadPath, Path.Combine(folderName, DateTime.Now.ToFileTime() + "_" + UserId + "_voice.m4a")); using (var fileStream = new FileStream(voicePath.FilePath, FileMode.Create)) { await commentDto.File.CopyToAsync(fileStream); commentDto.Comment = voicePath.FileName; } } break; } var postMaster = await _db.Post_Masters .AsNoTracking() .Include(x => x.PostImages) .SingleOrDefaultAsync(x => x.Id == commentDto.MasterId); var newComment = new Post_Comment { Comment = commentDto.Comment, PostId = commentDto.MasterId, MemberId = UserId, Type = commentDto.Type, Date = DateTime.UtcNow, Views = new List <Post_CommentView>() }; var commentView = new Post_CommentView { CDate = DateTime.UtcNow, IsRead = false, MemberId = postMaster.OpnedId }; newComment.Views.Add(commentView); await _db.Post_Comments.AddAsync(newComment); try { await _db.SaveChangesAsync(); var main = await _db.Post_Comments .Include(x => x.Createdby) .ThenInclude(x => x.Photos) .SingleOrDefaultAsync(x => x.Id == newComment.Id); var toDto = _mapper.Map <DataCommentDto>(main, n => n.Items.Add("UserId", UserId)); return(Ok(toDto)); } catch (Exception e) { throw e; } }