Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        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();
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
            }
        }
Exemplo n.º 8
0
        public Category Create(long userId, string name)
        {
            var category = new Category()
            {
                Name   = name,
                UserId = userId
            };

            HashgardContext.Categories.Add(category);
            HashgardContext.SaveChanges();

            return(category);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
        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));
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
            }
        }
Exemplo n.º 14
0
 public ArticleService(HashgardContext context) : base(context)
 {
 }
Exemplo n.º 15
0
 private void SaveLoggedOut(User user)
 {
     user.SetLoggedOut();
     HashgardContext.Users.Update(user);
     HashgardContext.SaveChanges();
 }
Exemplo n.º 16
0
 public ReactionService(HashgardContext context, IHubContext <ReactionHub, IReactionHubClient> reactionHubContext)
     : base(context)
 {
     _reactionHubContext = reactionHubContext;
 }
Exemplo n.º 17
0
 public CategoryService(HashgardContext context) : base(context)
 {
 }
Exemplo n.º 18
0
 protected BaseService(HashgardContext context)
 {
     HashgardContext = context;
 }
Exemplo n.º 19
0
 public CommentService(HashgardContext context) : base(context)
 {
 }
Exemplo n.º 20
0
 public UserService(HashgardContext context) : base(context)
 {
 }