public async Task<ArticleDto> GetArticleAsync(int articleId)
        {
            if (articleId == 0)
            {
                throw new ArgumentException("articleId");
            }

            using (var context = new PressfordContext())
            {
                var articlesQuery = from article in context.Articles
                                    where article.ArticleId == articleId
                                    select new ArticleDto
                                    {
                                        ArticleId = article.ArticleId,
                                        Title = article.Title,
                                        Body = article.Body,
                                        PublishDate = article.PublishDate,
                                        Author = article.Author,
                                        Comments = article.Comments.Select(comment => new CommentDto { AuthorName = comment.Author.Name, Content = comment.Content }).ToList(),
                                        LikeCounter = article.Likes.Count,
                                        IsUserPublisher = System.Threading.Thread.CurrentPrincipal.Identity.Name == article.Author.Name,
                                        IsAlreadyLiked = article.Likes.Any(like => like.User.Name == System.Threading.Thread.CurrentPrincipal.Identity.Name)
                                    };
                return await articlesQuery.SingleOrDefaultAsync();
            }
        }
        public async Task <ArticleDto> GetArticleAsync(int articleId)
        {
            if (articleId == 0)
            {
                throw new ArgumentException("articleId");
            }

            using (var context = new PressfordContext())
            {
                var articlesQuery = from article in context.Articles
                                    where article.ArticleId == articleId
                                    select new ArticleDto
                {
                    ArticleId   = article.ArticleId,
                    Title       = article.Title,
                    Body        = article.Body,
                    PublishDate = article.PublishDate,
                    Author      = article.Author,
                    Comments    = article.Comments.Select(comment => new CommentDto {
                        AuthorName = comment.Author.Name, Content = comment.Content
                    }).ToList(),
                    LikeCounter     = article.Likes.Count,
                    IsUserPublisher = System.Threading.Thread.CurrentPrincipal.Identity.Name == article.Author.Name,
                    IsAlreadyLiked  = article.Likes.Any(like => like.User.Name == System.Threading.Thread.CurrentPrincipal.Identity.Name)
                };
                return(await articlesQuery.SingleOrDefaultAsync());
            }
        }
Example #3
0
 public async Task <int> GetNumberOfLikesAsync(int userId)
 {
     using (var context = new PressfordContext())
     {
         var likesQuery = from like in context.Likes
                          where like.UserId == userId
                          select like;
         return(await likesQuery.CountAsync());
     }
 }
Example #4
0
 public async Task<int> GetNumberOfLikesAsync(int userId)
 {
     using (var context = new PressfordContext())
     {
         var likesQuery = from like in context.Likes
                      where like.UserId == userId
                      select like;
         return await likesQuery.CountAsync();
     }
 }
        public async Task UpdateArticleAsync(ArticleDto article)
        {
            using (var context = new PressfordContext())
            {
                var articleToUpdate = await context.Articles.SingleAsync(art => art.ArticleId == article.ArticleId);

                articleToUpdate.Title = article.Title;
                articleToUpdate.Body  = article.Body;
                await context.SaveChangesAsync();
            }
        }
 public async Task DeleteArticleAsync(int articleId)
 {
     using (var context = new PressfordContext())
     {
         var article = context.Articles.Attach(new Article {
             ArticleId = articleId
         });
         context.Articles.Remove(article);
         await context.SaveChangesAsync();
     }
 }
 public async Task LikeArticleAsync(int articleId, int userId)
 {
     using (var context = new PressfordContext())
     {
         context.Likes.Add(new Like
         {
             ArticleId = articleId,
             UserId    = userId
         });
         await context.SaveChangesAsync();
     }
 }
 public async Task LikeArticleAsync(int articleId, int userId)
 {
     using (var context = new PressfordContext())
     {
         context.Likes.Add(new Like
         {
             ArticleId = articleId,
             UserId = userId
         });
         await context.SaveChangesAsync();
     }
 }
Example #9
0
        public void AddUser(UserLogin userLogin)
        {
            if (userLogin == null)
            {
                throw new ArgumentException("userLogin");
            }

            using (var context = new PressfordContext())
            {
                context.UserLogins.Add(userLogin);
                context.SaveChanges();
            }
        }
 public async Task <IEnumerable <ArticleLikesDto> > GetArticleWithMostLikesAsync(int limit = 100)
 {
     using (var context = new PressfordContext())
     {
         var articlesQuery = from article in context.Articles
                             orderby article.Likes.Count descending
                             select new ArticleLikesDto
         {
             ArticleTitle = article.Title,
             LikeCounter  = article.Likes.Count
         };
         return(await articlesQuery.Take(limit).ToListAsync());
     }
 }
Example #11
0
        public User GetUser(int userId)
        {
            if (userId == 0)
            {
                throw new ArgumentException("userId");
            }

            using (var context = new PressfordContext())
            {
                var usersQuery = from user in context.Users
                                 where user.UserId == userId
                                 select user;
                return(usersQuery.SingleOrDefault());
            }
        }
Example #12
0
        public User GetUser(int userId)
        {
            if (userId == 0)
            {
                throw new ArgumentException("userId");
            }

            using (var context = new PressfordContext())
            {
                var usersQuery = from user in context.Users
                                    where user.UserId == userId
                                    select user;
                return  usersQuery.SingleOrDefault();
            }
        }
Example #13
0
        public async Task <User> GetUserAsync(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("userName");
            }

            using (var context = new PressfordContext())
            {
                var usersQuery = from user in context.Users
                                 where user.Name == userName
                                 select user;
                return(await usersQuery.SingleOrDefaultAsync());
            }
        }
Example #14
0
        public async Task<User> GetUserAsync(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("userName");
            }

            using (var context = new PressfordContext())
            {
                var usersQuery = from user in context.Users
                                 where user.Name == userName
                                 select user;
                return await usersQuery.SingleOrDefaultAsync();
            }
        }
 public async Task AddCommentToArticleAsync(int articleId, int userId, string comment)
 {
     using (var context = new PressfordContext())
     {
         var article = context.Articles.Attach(new Article { ArticleId = articleId });
         var user = context.Users.Attach(new User { UserId = userId });
         context.Comments.Add(new Comment
         {
             Article = article,
             Author = user,
             PublishDate = DateTime.Now,
             Content = comment
         });
         await context.SaveChangesAsync();
     }
 }
 public async Task AddArticleAsync(ArticleDto article)
 {
     using (var context = new PressfordContext())
     {
         context.Users.Attach(article.Author);
         context.Articles.Add(new Article
         {
             Author      = article.Author,
             Title       = article.Title,
             Body        = article.Body,
             PublishDate = DateTime.Now
         }
                              );
         await context.SaveChangesAsync();
     }
 }
 public async Task<IEnumerable<ArticleDto>> GetArticlesAsync()
 {
     using (var context = new PressfordContext())
     {
         var articlesQuery = from article in context.Articles
                             select new ArticleDto
                             {
                                 ArticleId = article.ArticleId,
                                 Title = article.Title,
                                 Body = article.Body,
                                 PublishDate = article.PublishDate,
                                 Author = article.Author,
                                 Comments = article.Comments.Select(comment => new CommentDto { AuthorName = comment.Author.Name, Content = comment.Content }).ToList(),
                                 LikeCounter = article.Likes.Count,
                                 IsUserPublisher = System.Threading.Thread.CurrentPrincipal.Identity.Name == article.Author.Name,
                                 IsAlreadyLiked = article.Likes.Any(like => like.User.Name == System.Threading.Thread.CurrentPrincipal.Identity.Name)
                             };
         return await articlesQuery.ToListAsync();
     }
 }
 public async Task AddCommentToArticleAsync(int articleId, int userId, string comment)
 {
     using (var context = new PressfordContext())
     {
         var article = context.Articles.Attach(new Article {
             ArticleId = articleId
         });
         var user = context.Users.Attach(new User {
             UserId = userId
         });
         context.Comments.Add(new Comment
         {
             Article     = article,
             Author      = user,
             PublishDate = DateTime.Now,
             Content     = comment
         });
         await context.SaveChangesAsync();
     }
 }
Example #19
0
        public User GetUser(string userName, string hashedPassword)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("userName");
            }

            if (string.IsNullOrWhiteSpace(hashedPassword))
            {
                throw new ArgumentException("hashedPassword");
            }

            using (var context = new PressfordContext())
            {
                var usersQuery = from userLogin in context.UserLogins
                                 where userLogin.User.Name == userName
                                 && userLogin.Password == hashedPassword
                                 select userLogin.User;
                return usersQuery.SingleOrDefault();
            }
        }
Example #20
0
        public User GetUser(string userName, string hashedPassword)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("userName");
            }

            if (string.IsNullOrWhiteSpace(hashedPassword))
            {
                throw new ArgumentException("hashedPassword");
            }

            using (var context = new PressfordContext())
            {
                var usersQuery = from userLogin in context.UserLogins
                                 where userLogin.User.Name == userName &&
                                 userLogin.Password == hashedPassword
                                 select userLogin.User;
                return(usersQuery.SingleOrDefault());
            }
        }
 public async Task <IEnumerable <ArticleDto> > GetArticlesAsync()
 {
     using (var context = new PressfordContext())
     {
         var articlesQuery = from article in context.Articles
                             select new ArticleDto
         {
             ArticleId   = article.ArticleId,
             Title       = article.Title,
             Body        = article.Body,
             PublishDate = article.PublishDate,
             Author      = article.Author,
             Comments    = article.Comments.Select(comment => new CommentDto {
                 AuthorName = comment.Author.Name, Content = comment.Content
             }).ToList(),
             LikeCounter     = article.Likes.Count,
             IsUserPublisher = System.Threading.Thread.CurrentPrincipal.Identity.Name == article.Author.Name,
             IsAlreadyLiked  = article.Likes.Any(like => like.User.Name == System.Threading.Thread.CurrentPrincipal.Identity.Name)
         };
         return(await articlesQuery.ToListAsync());
     }
 }
 public async Task UpdateArticleAsync(ArticleDto article)
 {
     using (var context = new PressfordContext())
     {
         var articleToUpdate = await context.Articles.SingleAsync(art => art.ArticleId == article.ArticleId);
         articleToUpdate.Title = article.Title;
         articleToUpdate.Body = article.Body;
         await context.SaveChangesAsync();
     }
 }
Example #23
0
        public void AddUser(UserLogin userLogin)
        {
            if (userLogin == null)
            {
                throw new ArgumentException("userLogin");
            }

            using (var context = new PressfordContext())
            {
                context.UserLogins.Add(userLogin);
                context.SaveChanges();
            }
        }
 public async Task<IEnumerable<ArticleLikesDto>> GetArticleWithMostLikesAsync(int limit = 100)
 {
     using (var context = new PressfordContext())
     {
         var articlesQuery = from article in context.Articles
                             orderby article.Likes.Count descending
                             select new ArticleLikesDto
                             {
                                 ArticleTitle = article.Title,
                                 LikeCounter = article.Likes.Count
                             };
         return await articlesQuery.Take(limit).ToListAsync();
     }
 }
 public async Task AddArticleAsync(ArticleDto article)
 {
     using (var context = new PressfordContext())
     {
         context.Users.Attach(article.Author);
         context.Articles.Add(new Article
         {
             Author = article.Author,
             Title = article.Title,
             Body = article.Body,
             PublishDate = DateTime.Now
         }
             );
         await context.SaveChangesAsync();
     }
 }
 public async Task DeleteArticleAsync(int articleId)
 {
     using (var context = new PressfordContext())
     {
         var article = context.Articles.Attach(new Article { ArticleId = articleId });
         context.Articles.Remove(article);
         await context.SaveChangesAsync();
     }
 }