-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataProviderLiteDB.cs
123 lines (113 loc) · 4.07 KB
/
DataProviderLiteDB.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
113
114
115
116
117
118
119
120
121
122
123
using System.Collections.Generic;
using System.Linq;
using Opus.Services.Data;
using LiteDB;
namespace Opus.Services.Implementation.Data
{
/// <summary>
/// Default implementation for <see cref="IDataProvider"/>. Accesses a local LiteDB database.
/// </summary>
public class DataProviderLiteDB : IDataProvider
{
private readonly string databasePath;
/// <summary>
/// Create a new data provider instance.
/// </summary>
/// <param name="dbPath">Filepath of the database.</param>
public DataProviderLiteDB(string dbPath)
{
databasePath = dbPath;
}
/// <summary>
/// Save an instance into the database.
/// </summary>
/// <typeparam name="T">Type of the data to save.</typeparam>
/// <param name="instance">Instance to save.</param>
/// <returns>The saved instance.</returns>
public T Save<T>(T instance) where T : IDataObject
{
using (var db = new LiteDatabase(databasePath))
{
var collection = db.GetCollection<T>();
/* var exists = collection.FindOne(x => x.Id == instance.Id);
if (exists == null)
collection.Insert(instance);
else
collection.Update(instance);*/
collection.Upsert(instance);
return instance;
}
}
/// <summary>
/// Remove an instance from the database.
/// </summary>
/// <typeparam name="T">Type of the data to remove.</typeparam>
/// <param name="instance">Instance to remove.</param>
/// <returns>The removed instance.</returns>
public T Delete<T>(T instance) where T : IDataObject
{
T found;
using (var db = new LiteDatabase(databasePath))
{
db.GetCollection<T>().Delete(instance.Id);
found = instance;
}
return found;
}
/// <summary>
/// Find all instances of data from the database.
/// </summary>
/// <typeparam name="T">Type of the data to retrieve.</typeparam>
/// <returns>All found instances.</returns>
public List<T> GetAll<T>()
{
List<T> found;
using (var db = new LiteDatabase(databasePath))
{
found = db.GetCollection<T>().FindAll().ToList();
}
return found;
}
/// <summary>
/// Find a particular instance of data.
/// </summary>
/// <typeparam name="T">Type of the stored data.</typeparam>
/// <param name="instance">Instance to find.</param>
/// <returns>Found instance or null, if none found.</returns>
public T? GetOne<T>(T instance)
{
using (var db = new LiteDatabase(databasePath))
{
var collection = db.GetCollection<T>();
var allRecords = collection.FindAll();
return allRecords.FirstOrDefault(x => x.Equals(instance));
}
}
/// <summary>
/// Find a particular instance of data by its id.
/// </summary>
/// <typeparam name="T">Type of the stored data.</typeparam>
/// <param name="id">Id of the instance to find.</param>
/// <returns>Found instance or null, if none found.</returns>
public T GetOneById<T>(int id) where T : IDataObject
{
using (var db = new LiteDatabase(databasePath))
{
var collection = db.GetCollection<T>();
var found = collection.FindById(id);
return found;
}
}
/// <summary>
/// Clear the whole database of all instances.
/// </summary>
/// <typeparam name="T">Type of the instances to clear.</typeparam>
public void Clear<T>()
{
using (var db = new LiteDatabase(databasePath))
{
db.GetCollection<T>().DeleteAll();
}
}
}
}