-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMessageRepository.cs
77 lines (63 loc) · 1.82 KB
/
MessageRepository.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
using dpp.opentakrouter.Models;
using SQLite;
using System;
using System.Collections.Generic;
namespace dpp.opentakrouter
{
public class MessageRepository : IMessageRepository
{
private readonly IDatabaseContext _context;
private readonly SQLiteConnection _db;
public MessageRepository(IDatabaseContext context)
{
_context = context;
_db = _context.Database;
_db.CreateTable<StoredMessage>();
EvictExpired();
}
public int Add(StoredMessage msg)
{
_db.BeginTransaction();
var r = _db.Insert(msg);
_db.Commit();
return r;
}
public int Delete(string q)
{
var m = Get(q);
return _db.Delete(m);
}
public StoredMessage Get(string UID)
{
return _db.Table<StoredMessage>().Where(m => m.Uid == UID).FirstOrDefault();
}
public IEnumerable<StoredMessage> Search(string query)
{
return _db.Table<StoredMessage>().Where(m => m.Data.Contains(query));
}
public int Update(StoredMessage msg)
{
_db.BeginTransaction();
var r = _db.Update(msg);
_db.Commit();
return r;
}
public int Upsert(StoredMessage m)
{
var e = Get(m.Uid);
if (e is null)
{
return Add(m);
}
return Update(e);
}
public int EvictExpired()
{
return _db.Table<StoredMessage>().Where(m => m.Expiration < DateTime.Now).Delete();
}
public IEnumerable<StoredMessage> GetActive()
{
return _db.Table<StoredMessage>().Where(m => m.Expiration >= DateTime.Now);
}
}
}