public void Execute(CreatePostCommentDto request) { _validator.ValidateAndThrow(request); _context.Comments.Add(_mapper.Map <Comment>(request)); _context.SaveChanges(_actor.Id); }
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; } }
public async Task <IActionResult> CreateReplay([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; } await _db.Post_Comments.AddAsync(new Post_Comment { Comment = commentDto.Comment, MasterId = commentDto.MasterId, MemberId = UserId, Type = commentDto.Type, Date = DateTime.UtcNow }); try { await _db.SaveChangesAsync(); return(Ok()); } catch (Exception e) { throw e; } }
public IActionResult AddComment(int id, [FromBody] CreatePostCommentDto request, [FromServices] ICreatePostCommentCommand command) { request.PostId = id; _executor.ExecuteCommand(command, request); return(StatusCode(StatusCodes.Status201Created)); }