Commit 83a940e8 authored by HankG's avatar HankG

Add Lists management repos for making things like "favorites" list etc.

parent 58660a7f
using System;
using System.IO;
using LiteDB;
using MySocialPortalLib.Repository;
using MySocialPortalLib.Service;
......@@ -10,6 +11,7 @@ namespace MySocialPortalDesktop.Factory
private const string AllPostsRepositoryDbName = "all_posts.db";
private const string LinkPreviewRepositoryDbName = "link_preview.db";
private const string LinkPreviewCacheRepositoryDbName = "link_preview_cache.db";
private const string ListsRepositoryDbName = "lists.db";
private const string MainPeopleRepositoryDbName = "all_people.db";
private const string ProfileImageRepositoryDbName = "profile_images.db";
private const string TimelineRepositoryDbName = "timeline_cache.db";
......@@ -27,6 +29,8 @@ namespace MySocialPortalDesktop.Factory
public IFileCacheRepository LinkPreviewImageCacheRepository => LinkPreviewImageCacheRepositoryLazy.Value;
public INamedListRepository ListsRepository => ListsRepositoryLazy.Value;
public IFileCacheRepository ProfileImageCacheRepository => ProfileImageCacheRepositoryLazy.Value;
public IPersonsRepository MainPeopleRepository => MainPeopleRepositoryLazy.Value;
......@@ -38,6 +42,8 @@ namespace MySocialPortalDesktop.Factory
AllPostsRepositoryLazy = new Lazy<IPostsRepository>(BuildAllPostsRepository);
LinkPreviewRepositoryLazy = new Lazy<ILinkPreviewRepository>(BuildLinkPreviewRepository);
LinkPreviewImageCacheRepositoryLazy = new Lazy<IFileCacheRepository>(BuildLinkPreviewImageCacheRepository);
ListsRepositoryLazy = new Lazy<INamedListRepository>(BuildListRepository);
ListsRepositoryBackingLazy = new Lazy<LiteRepository>(BuildListRepositoryBacking);
MainPeopleRepositoryLazy = new Lazy<IPersonsRepository>(BuildPeopleRepository);
ProfileImageCacheRepositoryLazy = new Lazy<IFileCacheRepository>(BuildProfileImageCacheRepository);
TimelineRepositoryLazy = new Lazy<ITimelineRepository>(BuildTimelineRepository);
......@@ -49,6 +55,10 @@ namespace MySocialPortalDesktop.Factory
private Lazy<IFileCacheRepository> LinkPreviewImageCacheRepositoryLazy { get; }
private Lazy<INamedListRepository> ListsRepositoryLazy { get; }
private Lazy<LiteRepository> ListsRepositoryBackingLazy { get; }
private Lazy<IPersonsRepository> MainPeopleRepositoryLazy { get; }
private Lazy<IFileCacheRepository> ProfileImageCacheRepositoryLazy { get; }
......@@ -69,6 +79,16 @@ namespace MySocialPortalDesktop.Factory
{
return new FileCacheLiteDbRepository(BuildDbPath(LinkPreviewCacheRepositoryDbName));
}
private INamedListRepository BuildListRepository()
{
return new NamedListLiteDbRepository(ListsRepositoryBackingLazy.Value);
}
private LiteRepository BuildListRepositoryBacking()
{
return new LiteRepository(BuildDbPath(ListsRepositoryDbName));
}
private IFileCacheRepository BuildProfileImageCacheRepository()
{
......@@ -106,6 +126,7 @@ namespace MySocialPortalDesktop.Factory
(ProfileImageCacheRepository as IDisposable)?.Dispose();
(TimelineRepository as IDisposable)?.Dispose();
(LinkPreviewImageCacheRepository as IDisposable)?.Dispose();
(ListsRepositoryBackingLazy.Value as IDisposable)?.Dispose();
}
_disposed = true;
......
using System.Collections.Generic;
namespace MySocialPortalLib.Repository
{
public interface INamedListRepository
{
void Add(string id, string listName);
void RemoveIdFromList(string id, string listName);
void RemoveList(string listName);
void RemoveId(string id);
IList<string> GetAllLists();
IList<string> GetAllListsForId(string id);
IList<string> GetAllIdsForList(string list);
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using LiteDB;
namespace MySocialPortalLib.Repository
{
public class NamedListLiteDbRepository : INamedListRepository
{
private const string DefaultCollectionName = "NamedLists";
private NamedListLiteDbRepository()
{
throw new MethodAccessException();
}
public NamedListLiteDbRepository(LiteRepository repository, string collectionName = DefaultCollectionName)
{
Repository = repository;
CollectionName = collectionName;
}
public void Add(string id, string listName)
{
var entry = new Entry
{
ListName = listName,
MemberId = id
};
if (Repository.Query<Entry>(CollectionName)
.Where(e => e.ListName == listName && e.MemberId == id)
.Count() == 0)
{
Repository.Insert(entry, CollectionName);
}
}
public IList<string> GetAllLists()
{
return Repository.Database.Execute($"SELECT DISTINCT(*.ListName) From {CollectionName}")
.Current["expr"]
.AsArray
.Select(e => e.AsString)
.ToList();
}
public IList<string> GetAllListsForId(string id)
{
return Repository.Query<Entry>(CollectionName)
.Where(e => e.MemberId == id)
.ToList()
.Select(e => e.ListName)
.ToList();
}
public IList<string> GetAllIdsForList(string list)
{
return Repository.Query<Entry>(CollectionName)
.Where(e => e.ListName == list)
.ToList()
.Select(e => e.MemberId)
.ToList();
}
public void RemoveIdFromList(string id, string listName)
{
Repository.DeleteMany<Entry>(e => e.MemberId == id && e.ListName == listName, CollectionName);
}
public void RemoveList(string listName)
{
Repository.DeleteMany<Entry>(e => e.ListName == listName, CollectionName);
}
public void RemoveId(string id)
{
Repository.DeleteMany<Entry>(e => e.MemberId == id, CollectionName);
}
private LiteRepository Repository { get; }
private string CollectionName { get; }
private class Entry
{
public string Id { get; set; }
public string ListName { get; set; }
public string MemberId { get; set; }
public Entry()
{
Id = Guid.NewGuid().ToString();
}
}
}
}
\ No newline at end of file
......@@ -32,6 +32,12 @@ namespace MySocialPortalLibTest.Factory
Assert.NotNull(RepositoryFactory.Instance.LinkPreviewImageCacheRepository);
}
[Fact]
public void TestListsRepository()
{
Assert.NotNull(RepositoryFactory.Instance.ListsRepository);
}
[Fact]
public void TestMainPeopleRepository()
{
......
using System.IO;
using LiteDB;
using MySocialPortalLib.Repository;
using Xunit;
namespace MySocialPortalLibTest.Repository
{
public class NamedListLiteDbRepositoryTest
{
[Fact]
public void TestInsertionAndQuery()
{
var repo = GetRepo();
repo.Add("id2", "list1");
repo.Add("id1", "list1");
var ids = repo.GetAllIdsForList("list1");
Assert.Equal(3, ids.Count);
Assert.Contains("id1", ids);
Assert.Contains("id2", ids);
Assert.Contains("id3", ids);
Assert.Empty(repo.GetAllIdsForList("list5"));
Assert.Empty(repo.GetAllIdsForList(""));
Assert.Empty(repo.GetAllIdsForList(null));
}
[Fact]
public void TestGetListNames()
{
var repo = GetRepo();
var lists = repo.GetAllLists();
Assert.Equal(2, lists.Count);
Assert.Contains("list1", lists);
Assert.Contains("list2", lists);
}
[Fact]
public void TestGetListForId()
{
var repo = GetRepo();
var lists = repo.GetAllListsForId("id1");
Assert.Equal(2, lists.Count);
Assert.Contains("list1", lists);
Assert.Contains("list2", lists);
Assert.Empty(repo.GetAllListsForId("id20"));
Assert.Empty(repo.GetAllListsForId(""));
Assert.Empty(repo.GetAllListsForId(null));
}
[Fact]
public void TestRemoveIdFromList()
{
var repo = GetRepo();
repo.RemoveIdFromList("id1", "list1");
Assert.DoesNotContain("id1", repo.GetAllIdsForList("list1"));
Assert.DoesNotContain("list1", repo.GetAllListsForId("id1"));
repo.RemoveIdFromList("","");
repo.RemoveIdFromList(null,null);
}
[Fact]
public void TestRemoveList()
{
var repo = GetRepo();
repo.RemoveList("list1");
Assert.DoesNotContain("list1", repo.GetAllLists());
Assert.DoesNotContain("list1", repo.GetAllListsForId("id1"));
repo.RemoveList("Someotherlist");
}
[Fact]
public void TestRemoveId()
{
var repo = GetRepo();
repo.RemoveId("id1");
Assert.DoesNotContain("id1", repo.GetAllIdsForList("list1"));
repo.RemoveId("aoeuaeuaoe");
repo.RemoveId(null);
repo.RemoveId("");
}
private NamedListLiteDbRepository GetRepo()
{
var fileStream = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None, 4096,
FileOptions.RandomAccess | FileOptions.DeleteOnClose);
var repo = new LiteRepository(fileStream);
var listRepo = new NamedListLiteDbRepository(repo);
listRepo.Add("id1", "list1");
listRepo.Add("id1", "list2");
listRepo.Add("id3", "list1");
return listRepo;
}
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment