aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Data/Repositories/FeedRepository.cs
blob: 8d3e5e17841718b3b405b0630f47c8a663ea722a (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
		}

		/// <summary>
        /// This returns a given amount of posts of all given friends, created before "firstRequestIssued",
		/// ordered from latest to oldest (time created).
		/// PageSize specifies how many posts to get, and pageNumber specifices how many posts to skip (pageNumber * pageSize).
		///
		/// This method is used in the feed page.
		/// Posts from friends are meant to be gotten in chunks, meaning you get X posts, and then get another amount of posts,
		/// that are after the first X posts.
        /// </summary>
		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))
				.ToListAsync();

			// Ordering by descending can't happen in query, because it doesn't order it
			// completely correctly (example: in query these two times are ordered
			// like this: 2021-01-30T11:49:45, 2021-01-28T21:37:40.701244)
			posts = posts
				.OrderByDescending(x => x.TimeCreated.ToFileTime())
				.Skip((pageNumber - 1) * pageSize)
				.Take(pageSize)
				.ToList();

			return posts;
		}

		/// <summary>
        /// This returns a given amount of posts, that a user has made, created before "firstRequestIssued",
		/// ordered from latest to oldest (time created).
		/// PageSize specifies how many posts to get, and pageNumber specifices how many posts to skip (pageNumber * pageSize).
		///
		/// This method is used in the profile page.
		/// Posts from friends are meant to be gotten in chunks, meaning you get X posts, and then get another amount of posts,
		/// that are after the first X posts.
        /// </summary>
		public async Task<List<Post>> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize)
		{
			List<Post> posts = await this._context.Posts
				.Where(post => post.TimeCreated < firstRequestIssued)
				.Where(p => p.Creator.Id == user.Id)
				.ToListAsync();

			// Look at GetFriendsPosts on why this is done like this
			posts = posts
				.OrderByDescending(x => x.TimeCreated.ToFileTime())
				.Skip((pageNumber - 1) * pageSize)
				.Take(pageSize)
				.ToList();

			return posts;
		}
	}
}