public async Task<IServiceResult> CreateComment(CommentEditViewModel model) { var result = new ServiceResult<CommentEditViewModel>(); try { var issue = await context.Issues.FindAsync(model.IssueId); if (issue == null) { result.AddError(m => m.IssueId, "Ez a feladat nem létezik"); } var user = await userManager.FindByIdAsync(model.UserId); if (user == null) { result.AddError(m => m.UserId, "Ez a felhasználó nem létezik"); } if (result.Succeeded) { var comment = mapper.Map<Comment>(model); comment.User = user; comment.Issue = issue; comment.Created = DateTime.Now; context.Comments.Add(comment); await context.SaveChangesAsync(); result.Data = mapper.Map<Comment, CommentListViewModel>(comment); } } catch (Exception e) { result.AddError("", e.Message); } return result; }
public async Task<IServiceResult> UpdateComment(CommentEditViewModel model) { var result = new ServiceResult<CommentEditViewModel>(); try { var comment = model.Id.HasValue ? await context.Comments.Include(c => c.User).FirstOrDefaultAsync(c => c.Id == model.Id.Value) : null; if(comment == null) { result.AddError("", "Ez a komment nem létezik"); return result; } if(comment.User.Id != model.UserId) { result.AddError(m => m.UserId, "Csak az szerkesztheti a kommentet aki létrehozta"); } if (result.Succeeded) { comment.Body = model.Body; comment.Title = model.Title; await context.SaveChangesAsync(); } } catch (Exception e) { result.AddError("", e.Message); } return result; }
public async Task<ActionResult> Create(CommentEditViewModel model) { model.UserId = HttpContext.User.Identity.GetUserId(); return Json(await CommentService.CreateComment(model)); }