Skip to content

Commit d37f997

Browse files
authored
Merge pull request #3 from dtwk2/development
Fixes issues with duplication of keys (when adding bookmarks).
2 parents 592be06 + 888ddb4 commit d37f997

File tree

2 files changed

+76
-61
lines changed

2 files changed

+76
-61
lines changed

source/Demos/CachedPathSuggestBoxDemo/Infrastructure/CachedPathInformationSuggest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void Insert(string path)
2929

3030
internal void Delete(string text)
3131
{
32-
throw new NotImplementedException();
32+
LiteRepository.Instance.Remove(text);
3333
}
3434

3535
/// <summary>

source/Demos/CachedPathSuggestBoxDemo/Infrastructure/LiteRepository.cs

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,87 @@
77

88
namespace CachedPathSuggestBoxDemo.Infrastructure
99
{
10-
/// <summary>
11-
/// Implements a static service provider for searching and editing a LiteDB file.
12-
/// </summary>
13-
public sealed class LiteRepository
14-
{
15-
#region fields
16-
private const string DbPath = @"..\..\..\Data\KeyValue.litedb";
10+
/// <summary>
11+
/// Implements a static service provider for searching and editing a LiteDB file.
12+
/// </summary>
13+
public sealed class LiteRepository
14+
{
15+
#region fields
16+
private const string DbPath = @"..\..\..\Data\KeyValue.litedb";
1717

18-
// Collection needs a name because object, keyvaluepair<string,string> is generic.
19-
private const string CollectionName = "collection";
20-
#endregion fields
18+
// Collection needs a name because object, keyvaluepair<string,string> is generic.
19+
private const string CollectionName = "collection";
20+
#endregion fields
2121

22-
#region ctors
23-
/// <summary>
24-
/// Explicit static constructor to tell C# compiler
25-
/// not to mark type as beforefieldinit
26-
/// </summary>
27-
static LiteRepository()
28-
{
29-
}
22+
#region ctors
23+
/// <summary>
24+
/// Explicit static constructor to tell C# compiler
25+
/// not to mark type as beforefieldinit
26+
/// </summary>
27+
static LiteRepository()
28+
{
29+
}
3030

31-
/// <summary>
32-
/// Class constructor
33-
/// </summary>
34-
private LiteRepository()
35-
{
36-
Directory.GetParent(DbPath).Create();
31+
/// <summary>
32+
/// Class constructor
33+
/// </summary>
34+
private LiteRepository()
35+
{
36+
Directory.GetParent(DbPath).Create();
3737

38-
// Id needed to ensure Upsert works.
39-
BsonMapper.Global.Entity<KeyValuePair<string, string>>().Id(x => x.Value);
40-
}
41-
#endregion ctors
38+
// Id needed to ensure Upsert works.
39+
BsonMapper.Global.Entity<KeyValuePair<string, DateTime>>().Id(x => x.Key);
40+
}
41+
#endregion ctors
4242

43-
/// <summary>
44-
/// Gets the repository instance from this static instance.
45-
/// </summary>
46-
public static LiteRepository Instance { get; } = new LiteRepository();
43+
/// <summary>
44+
/// Gets the repository instance from this static instance.
45+
/// </summary>
46+
public static LiteRepository Instance { get; } = new LiteRepository();
4747

48-
/// <summary>
49-
/// Inserts a new string into the collection of bookmarked strings.
50-
/// </summary>
51-
/// <param name="k"></param>
52-
/// <param name="collectionName"></param>
53-
public void Insert(string k, string? collectionName = null)
54-
{
55-
using var db = new LiteDatabase(DbPath);
56-
var col = db.GetCollection<KeyValuePair<string, DateTime>>(collectionName ?? CollectionName);
48+
/// <summary>
49+
/// Inserts a new string into the collection of bookmarked strings.
50+
/// </summary>
51+
/// <param name="k"></param>
52+
/// <param name="collectionName"></param>
53+
public void Insert(string k, string? collectionName = null)
54+
{
55+
using var db = new LiteDatabase(DbPath);
56+
var col = db.GetCollection<KeyValuePair<string, DateTime>>(collectionName ?? CollectionName);
5757

58-
// Make sure string is not already present
59-
col.Upsert(new KeyValuePair<string, DateTime>(k, DateTime.Now));
60-
}
58+
// Make sure string is not already present
59+
col.Upsert(new KeyValuePair<string, DateTime>(k, DateTime.Now));
60+
}
6161

62-
/// <summary>
63-
/// Filters the collection of bookmark strings by the given string and
64-
/// returns the resulting collection.
65-
/// </summary>
66-
/// <param name="key"></param>
67-
/// <param name="collectionName"></param>
68-
/// <returns></returns>
69-
public IEnumerable<KeyValuePair<string, DateTime>> Filter(string key, string? collectionName = null)
70-
{
71-
using var db = new LiteDatabase(DbPath);
72-
var col = db.GetCollection<KeyValuePair<string, DateTime>>(collectionName ?? CollectionName);
73-
return string.IsNullOrWhiteSpace(key) ?
74-
col.Query().ToArray() :
75-
col.Find(Query.Contains(nameof(KeyValuePair<string, DateTime>.Key), key)).ToArray();
76-
}
77-
}
62+
/// <summary>
63+
/// Inserts a new string into the collection of bookmarked strings.
64+
/// </summary>
65+
/// <param name="k"></param>
66+
/// <param name="collectionName"></param>
67+
public void Remove(string key, string? collectionName = null)
68+
{
69+
using var db = new LiteDatabase(DbPath);
70+
var col = db.GetCollection<KeyValuePair<string, DateTime>>(collectionName ?? CollectionName);
71+
72+
// Make sure string is not already present
73+
if (col.Exists(Query.Contains(nameof(KeyValuePair<string, DateTime>.Key), key)) == false)
74+
col.Delete(key);
75+
}
76+
77+
/// <summary>
78+
/// Filters the collection of bookmark strings by the given string and
79+
/// returns the resulting collection.
80+
/// </summary>
81+
/// <param name="key"></param>
82+
/// <param name="collectionName"></param>
83+
/// <returns></returns>
84+
public IEnumerable<KeyValuePair<string, DateTime>> Filter(string key, string? collectionName = null)
85+
{
86+
using var db = new LiteDatabase(DbPath);
87+
var col = db.GetCollection<KeyValuePair<string, DateTime>>(collectionName ?? CollectionName);
88+
return string.IsNullOrWhiteSpace(key) ?
89+
col.Query().ToArray() :
90+
col.Find(Query.Contains(nameof(KeyValuePair<string, DateTime>.Key), key)).ToArray();
91+
}
92+
}
7893
}

0 commit comments

Comments
 (0)