コード例 #1
0
        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();
            }
        }
コード例 #2
0
        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());
            }
        }
コード例 #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());
     }
 }
コード例 #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();
     }
 }
コード例 #5
0
        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();
            }
        }
コード例 #6
0
 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();
     }
 }
コード例 #7
0
 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();
     }
 }
コード例 #8
0
 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();
     }
 }
コード例 #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();
            }
        }
コード例 #10
0
 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());
     }
 }
コード例 #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());
            }
        }
コード例 #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();
            }
        }
コード例 #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());
            }
        }
コード例 #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();
            }
        }
コード例 #15
0
 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();
     }
 }
コード例 #16
0
 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();
     }
 }
コード例 #17
0
 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();
     }
 }
コード例 #18
0
 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();
     }
 }
コード例 #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();
            }
        }
コード例 #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());
            }
        }
コード例 #21
0
 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());
     }
 }
コード例 #22
0
 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();
     }
 }
コード例 #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();
            }
        }
コード例 #24
0
 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();
     }
 }
コード例 #25
0
 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();
     }
 }
コード例 #26
0
 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();
     }
 }