//[TeacherStudentFilter] public IActionResult Create(string id, CommentModel cmt) { string userId = _userManager.GetUserId(User); string userEmail = _userManager.GetUserName(User); string fileId = id; try { //_taskServices.Add(task, Guid.Parse(userId)); if (ModelState.IsValid) { //_db.Tasks.Add(task); //task.UserId = Guid.Parse(userId); //_db.SaveChanges(); _commentServices.AddComment(cmt, userId, userEmail, fileId); Message = "User: "******"\nleft a comment at {DateTime.UtcNow.ToLongTimeString()}"; _logger.LogInformation(Message); return(RedirectToAction("Index", "Task")); } return(View(cmt)); } catch (Exception ex) { return(View("Error", new ErrorViewModel() { Message = "Error while posting comment." })); } }
public IActionResult AddComment([FromBody] AddCommentRequest request) { try { var post = _postRepository.FindById(request.PostId); if (post == null) { return(BadRequest(new ErrorViewModel { ErrorCode = "400", ErrorMessage = "Post not found" })); } var user = _userRepository.FindById(request.UserId); if (post == null) { return(BadRequest(new ErrorViewModel { ErrorCode = "400", ErrorMessage = "User not found" })); } var comment = new Comment { Context = request.Context, UserId = request.UserId, PostId = request.PostId, TimeStamp = DateTime.UtcNow }; var response = _commentServices.AddComment(comment); if (response != "OK") { return(BadRequest(new ErrorViewModel { ErrorCode = "400", ErrorMessage = "Can not execute. Plz contact admin" })); } return(Ok()); } catch (Exception e) { return(BadRequest(new ErrorViewModel { ErrorCode = "400", ErrorMessage = $"Server Error: {e.Message}" })); } }