public async Task <Comment> UpdateCommentOnArticle(int id, string token, SingleCommentModel newComment) { var user = ServiceProvider.GetRequiredService <UserService>().GetCurrentUser(token); if (user == null) { throw new ApplicationException("Not logged in."); } var comment = Context.Comments.FirstOrDefault(c => c.AuthorUserUid == user.UserUid && c.Id == id); if (comment == null) { throw new ApplicationException("Not found."); } var newDate = DateTime.UtcNow; comment.Body = newComment.Body; comment.UpdatedAt = newDate; Context.Update(comment); var count = await Context.SaveChangesAsync(); return(count > 0 ? comment : null); }
public async Task <Comment> AddCommentToArticle(string slug, string token, SingleCommentModel newComment) { var user = ServiceProvider.GetRequiredService <UserService>().GetCurrentUser(token); if (user == null) { throw new ApplicationException("Not logged in."); } var articleService = ServiceProvider.GetRequiredService <ArticleService>(); var article = articleService.GetArticle(slug); if (article == null) { throw new ApplicationException("Not found."); } if (article.Comments.Any(c => c.AuthorUserUid == user.UserUid && c.Body == newComment.Body)) { throw new ApplicationException("Duplicate comment."); } var newDate = DateTime.UtcNow; var comment = new Comment { Id = Context.Comments.Any() ? Context.Comments.Max(c => c.Id) + 1 : 0, ArticleUid = article.ArticleUid, AuthorUserUid = user.UserUid, Body = newComment.Body, CreatedAt = newDate, UpdatedAt = newDate }; article.Comments.Add(comment); var count = await Context.SaveChangesAsync(); return(count > 0 ? comment : null); }