Beispiel #1
0
        public async Task <List <Post> > GetAllPostsAsync()
        {
            var entity = await _context.PostEntities
                         .Include(e => e.User)
                         .Include(e => e.Comments)
                         .ThenInclude(c => c.User)
                         .Include(e => e.Likes)
                         .ThenInclude(c => c.User)
                         .ToListAsync();

            var posts = entity
                        .Select(e => DbEntityConverter.ToPost(e))
                        .ToList();

            return(posts);
        }
Beispiel #2
0
        public async Task <Post> GetPostByIdAsync(int id)
        {
            var posts = await _context.PostEntities
                        .Include(p => p.User)
                        .Include(p => p.Comments)
                        .ThenInclude(c => c.User)
                        .Include(p => p.Likes)
                        .ToListAsync();

            if (!posts.Any())
            {
                return(null);
            }

            var post = posts.FirstOrDefault(p => p.Id == id);

            return(DbEntityConverter.ToPost(post));
        }
Beispiel #3
0
        public async Task <List <Post> > GetPostsByUserIdAsync(int id)
        {
            var entity = await _context.PostEntities
                         .Where(e => e.UserId == id)
                         .Include(e => e.User)
                         .Include(e => e.Comments)
                         .ThenInclude(c => c.User)
                         .Include(e => e.Likes)
                         .ThenInclude(c => c.User)
                         .ToListAsync();

            try {
                var posts = entity.Select(e => DbEntityConverter.ToPost(e)).Reverse().ToList();
                return(posts);
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                throw;
            }
        }