Commit e2cb7680 authored by HankG's avatar HankG

Add ListAll method to the PersonsRepository interface and LiteDb implementation

parent 257e4436
using System.Collections.Generic;
using MySocialPortalLib.Model;
namespace MySocialPortalLib.Repository
......@@ -10,6 +11,8 @@ namespace MySocialPortalLib.Repository
Person FindByNetworkId(string networkName, string networkId);
IList<Person> ListAll();
void UpdatePerson(Person person);
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.IO;
using LiteDB;
using MySocialPortalLib.Model;
......@@ -27,26 +28,7 @@ namespace MySocialPortalLib.Repository
PersonsRepository = new LiteRepository(filepath);
PersonsCollectionName = postsCollectionName;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
PersonsRepository?.Dispose();
}
_disposed = true;
}
public void AddPerson(Person person)
{
PersonsRepository.Insert(person, PersonsCollectionName);
......@@ -73,11 +55,37 @@ namespace MySocialPortalLib.Repository
.FirstOrDefault();
}
public IList<Person> ListAll()
{
return PersonsRepository.Query<Person>(PersonsCollectionName)
.ToList();
}
public void UpdatePerson(Person person)
{
PersonsRepository.Upsert(person, PersonsCollectionName);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
PersonsRepository?.Dispose();
}
_disposed = true;
}
private LiteRepository PersonsRepository { get; }
private string PersonsCollectionName { get; }
......
using System;
using System.IO;
using System.Linq;
using MySocialPortalLib.Model;
using MySocialPortalLib.Repository;
using MySocialPortalLib.Service;
......@@ -56,6 +57,21 @@ namespace MySocialPortalLibTest.Service
Assert.Null(db.FindByNetworkId("Network3", ""));
}
[Fact]
public void TestListAll()
{
var db = GetTempDb();
var p1 = new Person {Id = Guid.NewGuid().ToString()};
var p2 = new Person {Id = Guid.NewGuid().ToString()};
db.AddPerson(p1);
db.AddPerson(p2);
var allPeople = db.ListAll();
Assert.Equal(2, allPeople.Count);
var ids = allPeople.Select(p => p.Id).ToList();
Assert.Contains(p1.Id, ids);
Assert.Contains(p2.Id, ids);
}
private PersonsLiteDbRepository GetTempDb()
{
return new PersonsLiteDbRepository(new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate,
......
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