-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMenuInMemoryRepository.cs
More file actions
92 lines (83 loc) · 3.08 KB
/
Copy pathMenuInMemoryRepository.cs
File metadata and controls
92 lines (83 loc) · 3.08 KB
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
namespace BuberDinner.Infrastructure.Persistence.Memory;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using BuberDinner.Application.Abstractions.Persistence;
using BuberDinner.Domain.Host.ValueObject;
using BuberDinner.Domain.Menu;
internal class MenuInMemoryRepository : IMenuRepository
{
private static readonly List<Menu> s_menus = new();
// Parallel ETag store: in a real persistence layer the ETag is the row-version that the
// database emits on write. The in-memory equivalent is just a monotonic GUID per save —
// good enough to demonstrate If-Match concurrency control end-to-end.
private static readonly ConcurrentDictionary<string, string> s_etags = new(StringComparer.Ordinal);
private static readonly object s_lock = new();
public IEnumerable<Menu> GetAll(CancellationToken cancellationToken)
{
lock (s_lock)
return s_menus.ToList();
}
/// <summary>
/// Over-fetched, host-filtered, id-ordered slice for cursor pagination. Same shape as
/// <see cref="DinnerInMemoryRepository.GetPageForHost"/>.
/// </summary>
public IReadOnlyList<Menu> GetPageForHost(HostId hostId, Trellis.PageSize pageSize, System.Guid? afterId)
{
lock (s_lock)
{
IEnumerable<Menu> source = s_menus
.Where(m => m.HostId == hostId)
.OrderBy(m => m.Id.Value);
if (afterId is { } cursorId)
source = source.Where(m => m.Id.Value.CompareTo(cursorId) > 0);
return source.Take(pageSize.Applied + 1).ToList();
}
}
public ValueTask Add(Menu menu, CancellationToken cancellationToken)
{
lock (s_lock)
{
s_menus.Add(menu);
BumpETag(menu);
}
return ValueTask.CompletedTask;
}
public ValueTask Update(Menu menu, CancellationToken cancellationToken)
{
lock (s_lock)
{
int index = s_menus.FindIndex(m => m.Id == menu.Id);
if (index < 0)
s_menus.Add(menu);
else
s_menus[index] = menu;
BumpETag(menu);
}
return ValueTask.CompletedTask;
}
public ValueTask Delete(Menu menu, CancellationToken cancellationToken)
{
lock (s_lock)
{
s_menus.RemoveAll(m => m.Id == menu.Id);
s_etags.TryRemove(menu.Id.Value.ToString(), out _);
}
return ValueTask.CompletedTask;
}
public ValueTask<Maybe<Menu>> FindById(string id, CancellationToken cancellationToken)
{
Menu? menu;
lock (s_lock)
menu = s_menus.SingleOrDefault(m => m.Id.Value.ToString() == id);
if (menu is not null && s_etags.TryGetValue(id, out var etag))
AggregateETagWriter.SetETag(menu, etag);
return ValueTask.FromResult(Maybe.From(menu));
}
private static void BumpETag(Menu menu)
{
var newETag = Guid.NewGuid().ToString("N");
s_etags[menu.Id.Value.ToString()] = newETag;
AggregateETagWriter.SetETag(menu, newETag);
}
}