forked from MicrosoftDocs/mslearn-tailspin-spacegame-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalDocumentDBRepository.cs
executable file
·81 lines (74 loc) · 3.2 KB
/
LocalDocumentDBRepository.cs
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
78
79
80
81
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using TailSpin.SpaceGame.Web.Models;
namespace TailSpin.SpaceGame.Web
{
public class LocalDocumentDBRepository<T> : IDocumentDBRepository<T> where T : Model
{
// An in-memory list of all items in the collection.
private readonly List<T> _items;
public LocalDocumentDBRepository(string fileName)
{
// Serialize the items from the provided JSON document.
_items = JsonConvert.DeserializeObject<List<T>>(File.ReadAllText(fileName));
}
/// <summary>
/// Retrieves the item from the store with the given identifier.
/// </summary>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the retrieved item.
/// </returns>
/// <param name="id">The identifier of the item to retrieve.</param>
public Task<T> GetItemAsync(string id)
{
return Task<T>.FromResult(_items.Single(item => item.Id == id));
}
/// <summary>
/// Retrieves items from the store that match the given query predicate.
/// Results are given in descending order by the given ordering predicate.
/// </summary>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the collection of retrieved items.
/// </returns>
/// <param name="queryPredicate">Predicate that specifies which items to select.</param>
/// <param name="orderDescendingPredicate">Predicate that specifies how to sort the results in descending order.</param>
/// <param name="page">The 1-based page of results to return.</param>
/// <param name="pageSize">The number of items on a page.</param>
public Task<IEnumerable<T>> GetItemsAsync(
Expression<Func<T, bool>> queryPredicate,
Expression<Func<T, int>> orderDescendingPredicate,
int page = 1, int pageSize = 10
)
{
var result = _items.AsQueryable()
.Where(queryPredicate) // filter
.OrderByDescending(orderDescendingPredicate) // sort
.Skip(page * pageSize) // find page
.Take(pageSize) // take items
.AsEnumerable(); // make enumeratable
return Task<IEnumerable<T>>.FromResult(result);
}
/// <summary>
/// Retrieves the number of items that match the given query predicate.
/// </summary>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the number of items that match the query predicate.
/// </returns>
/// <param name="queryPredicate">Predicate that specifies which items to select.</param>
public Task<int> CountItemsAsync(Expression<Func<T, bool>> queryPredicate)
{
var count = _items.AsQueryable()
.Where(queryPredicate) // filter
.Count(); // count
return Task<int>.FromResult(count);
}
}
}