-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryDb.cs
92 lines (79 loc) · 3.21 KB
/
InMemoryDb.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
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WebApp.Filters;
using WebApp.Models;
using WebApp.Validators;
namespace WebApp.Tests
{
public class InMemoryDb : IRepository
{
private readonly DbContextOptions<OysterContext> _option;
private readonly IValidator<Order> _validator;
public InMemoryDb(DbContextOptions<OysterContext> option, IValidator<Order> validator)
{
_option = option;
_validator = validator;
}
public async Task AddOrder(Order order)
{
using var context = new OysterContext(_option);
var repository = new Repository(context, _validator);
await repository.AddOrder(order);
}
public async Task<Order> GetOrder(Guid orderId)
{
using var context = new OysterContext(_option);
var repository = new Repository(context, _validator);
return await repository.GetOrder(orderId);
}
public async Task<List<Order>> GetOrders()
{
using var context = new OysterContext(_option);
var repository = new Repository(context, _validator);
return await repository.GetOrders();
}
public async Task<OrderTableResponse> GetOrders(int page, int take)
{
using var context = new OysterContext(_option);
var repository = new Repository(context, _validator);
return await repository.GetOrders(page, take);
}
public async Task<OrderTableResponse> GetOrders(List<IFilter> filters, int page, int take)
{
using var context = new OysterContext(_option);
var repository = new Repository(context, _validator);
return await repository.GetOrders(filters, page, take);
}
public static T GetEntityFromInMemoryDatabase<T>(DbContextOptions<OysterContext> option,
Func<T, bool> predicate)
where T : class
{
using var context = new OysterContext(option);
if (typeof(T) == typeof(Order))
return (T) (object) context.Orders
.Include(x => x.subOrders)
.First((Func<Order, bool>) predicate);
else if (typeof(T) == typeof(SubOrder))
return (T) (object) context.SubOrders.First((Func<SubOrder, bool>) predicate);
else
throw new Exception("Entity type not supported");
}
public static T GetEntityListFromInMemoryDatabase<T>(DbContextOptions<OysterContext> option,
Func<T, bool> predicate)
where T : class
{
using var context = new OysterContext(option);
if (typeof(T) == typeof(Order))
return (T) (object) context.Orders
.Include(x => x.subOrders)
.First((Func<Order, bool>) predicate);
else if (typeof(T) == typeof(SubOrder))
return (T) (object) context.SubOrders.First((Func<SubOrder, bool>) predicate);
else
throw new Exception("Entity type not supported");
}
}
}