aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Data/Repositories/FeedRepository.cs
blob: efcb8e0a0a1c02e0f0d89121a25d7bfdb7622b76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper.Internal;
using DevHive.Data.Interfaces.Repositories;
using DevHive.Data.Models;
using Microsoft.EntityFrameworkCore;

namespace DevHive.Data.Repositories
{
	public class FeedRepository : IFeedRepository
	{
		private readonly DevHiveContext _context;

		public FeedRepository(DevHiveContext context)
		{
			this._context = context;
		}

		public async Task<List<Post>> GetFriendsPosts(List<User> friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize)
		{
			List<Guid> friendsIds = friendsList.Select(f => f.Id).ToList();

			List<Post> posts = await this._context.Posts
				.Where(post => post.TimeCreated < firstRequestIssued)
				.Where(p => friendsIds.Contains(p.Creator.Id))
				.OrderByDescending(x => x.TimeCreated)
				.Skip((pageNumber - 1) * pageSize)
				.Take(pageSize)
				.ToListAsync();

			return posts;
		}
	}
}