-
Notifications
You must be signed in to change notification settings - Fork 25
/
Store.cs
112 lines (99 loc) · 3.56 KB
/
Store.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma warning disable IL2026
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.JavaScript;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace TodoMVC
{
public partial class Store
{
#region in-memory
private List<Item>? liveTodos;
private List<Item> GetLocalStorage()
{
if (liveTodos!=null) return liveTodos;
return Interop.getLocalStorage();
}
private void SetLocalStorage(List<Item> items)
{
liveTodos = items;
Interop.setLocalStorage(items);
}
public void Insert(Item item)
{
var todos = GetLocalStorage();
todos.Add(item);
SetLocalStorage(todos);
}
#endregion
public void Update(Item item)
{
var todos = GetLocalStorage();
var existing = todos.FirstOrDefault(it => it.Id == item.Id);
if (existing == null)
{
todos.Add(item);
}
else
{
if (item.Title != null)
{
existing.Title = item.Title;
}
if (item.Completed.HasValue)
{
existing.Completed = item.Completed;
}
}
SetLocalStorage(todos);
}
public void Remove(long? id, string? title, bool? completed)
{
var todos = GetLocalStorage();
todos = todos.Where(it =>
{
if (id.HasValue && it.Id == id.Value) return false;
if (title != null && it.Title == title) return false;
if (completed.HasValue && it.Completed!.Value == completed.Value) return false;
return true;
}).ToList();
SetLocalStorage(todos);
}
public List<Item> Find(long? id, string? title, bool? completed)
{
var todos = GetLocalStorage();
return todos.Where(it =>
{
if (id.HasValue && it.Id != id.Value) return false;
if (title != null && it.Title != title) return false;
if (completed.HasValue && it.Completed != completed.Value) return false;
return true;
}).ToList();
}
public (int total, int completed, int active) Count()
{
var todos = GetLocalStorage();
return (todos.Count, todos.Where(it => it.Completed.Value).Count(), todos.Where(it => !it.Completed.Value).Count());
}
public static partial class Interop
{
[JsonSerializable(typeof(List<Item>))]
private partial class ItemListSerializerContext : JsonSerializerContext { }
public static List<Item> getLocalStorage()
{
var json = _getLocalStorage();
return JsonSerializer.Deserialize(json, ItemListSerializerContext.Default.ListItem) ?? new List<Item>();
}
public static void setLocalStorage(List<Item> items)
{
var json = JsonSerializer.Serialize(items, ItemListSerializerContext.Default.ListItem);
_setLocalStorage(json);
}
[JSImport("setLocalStorage", "todoMVC/store.js")]
internal static partial void _setLocalStorage(string json);
[JSImport("getLocalStorage", "todoMVC/store.js")]
internal static partial string _getLocalStorage();
}
}
}