public void AddNewComment(string userName, CommentNewViewModel comment) { string userId = Context.Users .Where(u => u.UserName == userName) .Select(u => u.Id) .FirstOrDefault(); if (userId == null || comment.EventId == null) { return; } Comment newComment = new Comment { Time = DateTime.Now, Text = comment.Text, Suspended = false, Creator_Id = userId, Event_Id = (int)comment.EventId }; Context.Comments.Add(newComment); Context.SaveChanges(); }
public ActionResult AddComment(int eventId) { CommentNewViewModel newComment = new CommentNewViewModel { EventId = eventId }; return(View(newComment)); }
public ActionResult AddComment(CommentNewViewModel comment, int eventId) { string userName = User.Identity.Name; comment.EventId = eventId; EventService.AddNewComment(userName, comment); return(RedirectToAction("Index", "Event", new { eventId = eventId })); }
public async Task <IActionResult> CreateOrUpdate([FromBody] CommentNewViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _commentService.CreateOrUpdateAsync(model); if (!result.Succeeded) { return(BadRequest(result.Errors)); } return(Ok()); }
public async Task <ProcessResult> CreateOrUpdateAsync(CommentNewViewModel model) { Func <Task> action = async() => { var commentEntity = await GetOrCreateEntityAsync(context.Comments, x => x.Id == model.Id); var comment = commentEntity.result; if (!commentEntity.isCreated && CurrentUser.Id != comment.CreatorId) { throw new InvalidOperationException("You're not owner of requested comment"); } comment.IdeaId = model.IdeaId; comment.Message = model.Message; await context.SaveChangesAsync(); }; return(await Process.RunAsync(action)); }