-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONStore.cs
More file actions
186 lines (165 loc) · 5.59 KB
/
Copy pathJSONStore.cs
File metadata and controls
186 lines (165 loc) · 5.59 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace LStoreJSON
{
public class JSONStore
{
//For IO oprations
private StreamReader sr;
private StreamWriter sw;
private string dbPath;
//for internal oprations
private Dictionary<string, List<object>> inMemoryDB;
private HashSet<Type> changedObjects;
public JSONStore(string reletiveDatabasePath = "database\\")
{
dbPath = reletiveDatabasePath;
inMemoryDB = new Dictionary<string, List<object>>();
changedObjects = new HashSet<Type>();
if (!Directory.Exists(dbPath))
{
Directory.CreateDirectory(dbPath);
}
}
public void Add<T>(T o, bool skipValidation = true)
{
string fileName = o.GetType().ToString();
List<T> existingElements = ReadObjects<T>();
if(skipValidation)
{
inMemoryDB[fileName].Add(o);
AddItemToSaveList(o);
}
else
{
if (existingElements.FindIndex(e => e.GetKeyValeue<T>().Equals(o.GetKeyValeue<T>())) >= 0)
{
throw new Exception("Key \"" + o.GetKeyValeue<T>().ToString() + "\" already exists");
}
else
{
inMemoryDB[fileName].Add(o);
AddItemToSaveList(o);
}
}
}
public bool Remove<T>(T o)
{
string fileName = o.GetType().ToString();
List<T> existingElements = ReadObjects<T>();
T item = Single<T>(o.GetKeyValeue<T>());
AddItemToSaveList(o);
return inMemoryDB[fileName].Remove(item);
}
/// <summary>
/// Commit the changes made to the database
/// </summary>
public void SaveChanges()
{
foreach (Type t in changedObjects)
{
SaveChanges(t);
}
changedObjects.Clear();
}
/// <summary>
/// Commit the changes made to the database for the given type
/// </summary>
public void SaveChanges(Type t)
{
string fileName = t.ToString();
string filePath = dbPath + fileName;
if (!inMemoryDB.ContainsKey(fileName))
{
throw new KeyNotFoundException("No changes to key \"" + fileName + "\" found");
}
else
{
using (sw = new StreamWriter(filePath, false, Encoding.UTF8))
{
sw.WriteLine(JsonConvert.SerializeObject(inMemoryDB[fileName]));
}
}
}
/// <summary>
/// Returns an object of the supplied type given the object contains a key
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <returns>Object of the supplied type and key</returns>
public T Single<T>(object Id)
{
return ReadObjects<T>().Find(e => e.GetKeyValeue<T>().ToString().Equals(Id));
}
/// <summary>
/// Returns all the objects stored of the supplied type
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <returns>List of the data for the given type</returns>
public List<T> All<T>()
{
return ReadObjects<T>();
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IReadOnlyCollection<T> InnerList<T>()
{
return ReadObjects<T>().AsReadOnly();
}
private List<T> ReadObjects<T>()
{
string fileName = typeof(T).ToString();
string filePath = dbPath + fileName;
if (inMemoryDB.ContainsKey(fileName))
{
return inMemoryDB[fileName].ConvertObjectToGenericType<T>();
}
else
{
string data;
try
{
using (sr = new StreamReader(filePath, Encoding.UTF8))
{
data = sr.ReadToEnd();
}
}
catch (FileNotFoundException)
{
data = "";
}
List<T> dataList = string.IsNullOrWhiteSpace(data) ? new List<T>() : JsonConvert.DeserializeObject<List<T>>(data);
inMemoryDB.Add(fileName, dataList.ConvertGenericTypeToObjectList<T>());
return dataList;
}
}
private void AddItemToSaveList(object inType)
{
changedObjects.Add(inType.GetType());
}
/// <summary>
/// Determines if the type of object is saveable via the JSONStore class
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <returns>Is the supplied type saveable</returns>
public static bool IsTypeSaveable<T>()
{
bool output = false;
foreach (System.Reflection.PropertyInfo info in typeof(T).GetProperties())
{
if (Attribute.IsDefined(info, typeof(System.ComponentModel.DataAnnotations.KeyAttribute)))
{
output = true;
break;
}
}
return output;
}
}
}