public Article Get(long?userId, long id) { var article = HashgardContext.Articles.Include(a => a.User).ForId(id); HashgardContext.SetReactionTypes(article, userId, rt => rt.ArticleId); return(article); }
public async Task <Article> EditAsync(long userId, long id, string newTitle, string newText) { using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var existing = HashgardContext.Articles .Include(a => a.Comments) .Include(a => a.ReactionTypes) .FirstOrDefault(a => a.Id == id); if (existing == null) { throw new Exception("Article does not exist."); } if (existing.UserId != userId) { throw new Exception("Unauthorized."); } var article = new Article() { UserId = userId, CategoryId = existing.CategoryId, CorrelationUid = existing.CorrelationUid, Title = newTitle, Text = newText, VersionDate = DateTime.Now }; HashgardContext.Articles.Add(article); await HashgardContext.SaveChangesAsync(); foreach (var comment in existing.Comments) { comment.ArticleId = article.Id; HashgardContext.Comments.Update(comment); } foreach (var reactionType in existing.ReactionTypes) { reactionType.ArticleId = article.Id; HashgardContext.ReactionTypes.Update(reactionType); } await HashgardContext.SaveChangesAsync(); HashgardContext.SetReactionTypes(article, userId, rt => rt.ArticleId); transaction.Complete(); return(article); } }
public async Task <Comment> EditAsync(long userId, long id, string text) { using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var oldComment = HashgardContext.Comments .Include(c => c.Children) .Include(c => c.ReactionTypes) .ForId(id); if (oldComment == null) { throw new Exception("Comment does not exist."); } if (oldComment.UserId != userId) { throw new Exception("Unauthorized."); } var newComment = new Comment { UserId = oldComment.UserId, ArticleId = oldComment.ArticleId, ParentId = oldComment.ParentId, Text = text, CorrelationUid = oldComment.CorrelationUid, VersionDate = DateTime.Now }; HashgardContext.Comments.Add(newComment); await HashgardContext.SaveChangesAsync(); foreach (var child in oldComment.Children) { child.ParentId = newComment.Id; HashgardContext.Comments.Update(child); } foreach (var reactionType in oldComment.ReactionTypes) { reactionType.CommentId = newComment.Id; HashgardContext.ReactionTypes.Update(reactionType); } await HashgardContext.SaveChangesAsync(); HashgardContext.SetReactionTypes(newComment, userId, rt => rt.CommentId); transaction.Complete(); return(newComment); } }
private Page <Comment> GetList(long?userId, long?articleId, long?commentId, int index, int count) { var page = HashgardContext.Comments .Where(c => c.ArticleId == articleId && c.ParentId == commentId) .Include(c => c.User) .GetLastVersions() .ToPage(index, count, c => c.VersionDate, OrderBy.Desc); HashgardContext.SetReactionTypes <Comment>(page.Items, userId, rt => rt.CommentId); return(page); }
public Page <Article> GetList(long?userId, long categoryId, int index, int count) { var page = HashgardContext.Articles .Where(a => a.CategoryId == categoryId) .Include(a => a.User) .GetLastVersions() .ToPage(index, count, a => a.VersionDate, OrderBy.Desc); var totalCount = HashgardContext.Articles.Count(a => a.CategoryId == categoryId); HashgardContext.SetReactionTypes <Article>(page.Items, userId, rt => rt.ArticleId); return(page); }