public async Task <Article> CreateAsync(long userId, long categoryId, string title, string text) { using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var existing = HashgardContext.Articles.FirstOrDefault(a => a.CategoryId == categoryId && a.Title == title); if (existing != null) { throw new Exception("Article title already exists."); } var article = new Article { UserId = userId, CategoryId = categoryId, Title = title, Text = text, CorrelationUid = Guid.NewGuid(), VersionDate = DateTime.Now }; HashgardContext.Articles.Add(article); await HashgardContext.SaveChangesAsync(); transaction.Complete(); return(article); } }
public async Task <ReactionType> RemoveAsync(long userId, long reactionTypeId) { using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var reactionType = HashgardContext.ReactionTypes .Include(rt => rt.Reactions) .ForId(reactionTypeId); if (reactionType == null) { throw new Exception("Reaction type does not exist."); } var reaction = reactionType.Reactions .FirstOrDefault(r => r.UserId == userId); if (reaction != null) { reactionType.Reactions.Remove(reaction); HashgardContext.ReactionTypes.Update(reactionType); await HashgardContext.SaveChangesAsync(); transaction.Complete(); RunNotify(reactionType); } return(reactionType); } }
public async Task DeleteAsync(long userId, long id) { using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var existing = HashgardContext.Articles .Include(a => a.Comments).ThenInclude(c => c.Children) .ForId(id); if (existing == null) { throw new Exception("Article does not exist."); } if (existing.UserId != userId) { throw new Exception("Unauthorized."); } var children = existing.Comments .Union(existing.Comments.SelectMany(c => c.Children)); HashgardContext.Comments.RemoveRange(children); var allVersions = HashgardContext.Articles.Where(a => a.CorrelationUid == existing.CorrelationUid); HashgardContext.Articles.RemoveRange(allVersions); await HashgardContext.SaveChangesAsync(); transaction.Complete(); } }
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); } }
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 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); } }
public Category Create(long userId, string name) { var category = new Category() { Name = name, UserId = userId }; HashgardContext.Categories.Add(category); HashgardContext.SaveChanges(); return(category); }
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); }
public LogInResponse LogIn(string name, string password, TimeSpan sessionLifetime) { var user = HashgardContext.Users.FirstOrDefault(u => u.Name == name && u.Password == password); if (user == null) { throw new Exception("Authentication failed."); } user.SessionLifetime = sessionLifetime; user.SetLoggedIn(); HashgardContext.Users.Update(user); HashgardContext.SaveChanges(); return(new LogInResponse(user)); }
public LogInResponse SignUp(string name, string password, TimeSpan sessionLifetime) { if (HashgardContext.Users.Any(u => u.Name == name)) { throw new Exception("Username already exists."); } var user = new User { Name = name, Password = password, SessionLifetime = sessionLifetime, SignUpDate = DateTime.Now, LogInDate = DateTime.Now, Token = Guid.NewGuid() }; HashgardContext.Users.Add(user); HashgardContext.SaveChanges(); return(new LogInResponse(user)); }
private async Task <ReactionType> CreateAsync(long userId, long?commentId, long?articleId, string reactionTypeName) { ReactionType reactionType = new ReactionType() { ArticleId = articleId, CommentId = commentId, Name = reactionTypeName, Reactions = new [] { new Reaction { UserId = userId } } }; HashgardContext.ReactionTypes.Add(reactionType); await HashgardContext.SaveChangesAsync(); RunNotify(reactionType); return(reactionType); }
private async Task <Comment> AddAsync(long userId, long?articleId, long?commentId, string text) { using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var comment = new Comment { UserId = userId, ArticleId = articleId, ParentId = commentId, Text = text, CorrelationUid = Guid.NewGuid(), VersionDate = DateTime.Now }; HashgardContext.Comments.Add(comment); await HashgardContext.SaveChangesAsync(); transaction.Complete(); return(comment); } }
public ArticleService(HashgardContext context) : base(context) { }
private void SaveLoggedOut(User user) { user.SetLoggedOut(); HashgardContext.Users.Update(user); HashgardContext.SaveChanges(); }
public ReactionService(HashgardContext context, IHubContext <ReactionHub, IReactionHubClient> reactionHubContext) : base(context) { _reactionHubContext = reactionHubContext; }
public CategoryService(HashgardContext context) : base(context) { }
protected BaseService(HashgardContext context) { HashgardContext = context; }
public CommentService(HashgardContext context) : base(context) { }
public UserService(HashgardContext context) : base(context) { }