Commit e3e9a5d8 authored by HankG's avatar HankG

Add TimelineRepository

parent 70945137
......@@ -6,17 +6,17 @@ namespace MySocialPortalLib.Repository
{
public interface ITimelineRepository
{
void AddOrUpdate (TimelineInterval interval);
bool AddOrUpdate (TimelineInterval interval);
TimelineInterval? FindById(string id);
IList<TimelineInterval> FindByInterval(DateTimeOffset start, DateTimeOffset stop, bool inclusive);
void Remove (TimelineInterval interval);
bool Remove (TimelineInterval interval);
void RemoveById(string id);
bool RemoveById(string id);
void RemoveByRange(IEnumerable<TimelineInterval> intervals);
bool RemoveRange(IEnumerable<TimelineInterval> intervals);
long Count();
}
......
......@@ -75,7 +75,6 @@ namespace MySocialPortalLib.Repository
_disposed = true;
}
private LiteRepository LinkPreviewRepository { get; }
private string LinkPreviewCollectionName { get; }
......
using System;
using System.Collections.Generic;
using MySocialPortalLib.Model;
namespace MySocialPortalLib.Repository
{
public class TimelineInMemoryRepository : ITimelineRepository
{
public TimelineInMemoryRepository()
{
Items = new Dictionary<string, TimelineInterval>();
}
public void AddOrUpdate(TimelineInterval interval)
{
Items[interval.Id] = interval;
}
public TimelineInterval? FindById(string id)
{
Items.TryGetValue(id, out var item);
return item;
}
public IList<TimelineInterval> FindByInterval(DateTimeOffset start, DateTimeOffset stop, bool inclusive)
{
throw new NotImplementedException();
}
public void Remove(TimelineInterval interval)
{
throw new NotImplementedException();
}
public void RemoveById(string id)
{
throw new NotImplementedException();
}
public void RemoveByRange(IEnumerable<TimelineInterval> intervals)
{
throw new NotImplementedException();
}
public long Count()
{
throw new NotImplementedException();
}
private Dictionary<string, TimelineInterval> Items;
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LiteDB;
using MySocialPortalLib.Model;
namespace MySocialPortalLib.Repository
{
public class TimelineLiteDbRepository : ITimelineRepository, IDisposable
{
private const string DefaultTimelineCollectionName = "timeline";
private bool _disposed = false;
public TimelineLiteDbRepository (Stream dbStream,
string postsCollectionName = DefaultTimelineCollectionName)
{
TimelineRepository = new LiteRepository(dbStream);
TimelineCollectionName = postsCollectionName;
}
public TimelineLiteDbRepository(String filepath, string postsCollectionName = DefaultTimelineCollectionName)
{
TimelineRepository = new LiteRepository(filepath);
TimelineCollectionName = postsCollectionName;
}
public bool AddOrUpdate(TimelineInterval interval)
{
return TimelineRepository.Upsert(interval, TimelineCollectionName);
}
public TimelineInterval? FindById(string id)
{
return TimelineRepository.Query<TimelineInterval>(TimelineCollectionName)
.Where(i => i.Id == id)
.FirstOrDefault();
}
public IList<TimelineInterval> FindByInterval(DateTimeOffset start, DateTimeOffset stop, bool inclusive)
{
return TimelineRepository.Query<TimelineInterval>(TimelineCollectionName)
.Where(i => inclusive ? i.IntervalStart >= start : i.IntervalStart > start)
.Where(i => inclusive ? i.IntervalStop <= stop : i.IntervalStart < stop)
.OrderBy(i => i.IntervalStart)
.ToList();
}
public bool Remove(TimelineInterval interval)
{
return interval != null && RemoveById(interval.Id);
}
public bool RemoveById(string id)
{
return !string.IsNullOrWhiteSpace(id) && TimelineRepository.Delete<TimelineInterval>(id, TimelineCollectionName);
}
public bool RemoveRange(IEnumerable<TimelineInterval> intervals)
{
return intervals.Aggregate(true, (current, interval) => current & Remove(interval));
}
public long Count()
{
return TimelineRepository.Query<TimelineInterval>(TimelineCollectionName)
.LongCount();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
TimelineRepository?.Dispose();
}
_disposed = true;
}
private LiteRepository TimelineRepository { get; }
private string TimelineCollectionName { get; }
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using MySocialPortalLib.Model;
using MySocialPortalLib.Repository;
using Xunit;
namespace MySocialPortalLibTest.Repository
{
public class TimelineLiteDbRepositoryTest
{
[Fact]
public void TestConstruction()
{
Assert.NotNull(GetTempDb());
}
[Fact]
public void TestAddInterval()
{
var db = GetTempDb();
var interval = new TimelineInterval
{
IntervalStart = DateTimeOffset.Now,
IntervalStop = DateTimeOffset.Now.AddDays(1),
TimelineName = "Name1"
};
db.AddOrUpdate(interval);
Assert.Equal(1, db.Count());
db.AddOrUpdate(interval);
Assert.Equal(1, db.Count());
db.AddOrUpdate(new TimelineInterval());
Assert.Equal(2, db.Count());
}
[Fact]
public void TestUpdateInterval()
{
var db = GetTempDb();
string id = Guid.NewGuid().ToString();
var interval1 = new TimelineInterval
{
Id = id,
IntervalStart = DateTimeOffset.Now,
IntervalStop = DateTimeOffset.Now.AddDays(1),
TimelineName = "Name1"
};
db.AddOrUpdate(interval1);
Assert.Equal(1, db.Count());
Assert.True(interval1.AllEquals(db.FindById(id)));
var interval2 = new TimelineInterval
{
Id = id,
IntervalStart = DateTimeOffset.UnixEpoch,
IntervalStop = DateTimeOffset.UnixEpoch.AddDays(1),
TimelineName = "Name2"
};
db.AddOrUpdate(interval2);
Assert.Equal(1, db.Count());
Assert.False(interval1.AllEquals(db.FindById(id)));
Assert.True(interval2.AllEquals(db.FindById(id)));
}
[Fact]
public void TestRemoveById()
{
var db = GetTempDb();
string id = Guid.NewGuid().ToString();
var interval1 = new TimelineInterval
{
Id = id,
IntervalStart = DateTimeOffset.Now,
IntervalStop = DateTimeOffset.Now.AddDays(1),
TimelineName = "Name1"
};
db.AddOrUpdate(interval1);
db.AddOrUpdate(new TimelineInterval());
Assert.Equal(2, db.Count());
Assert.True(db.RemoveById(id));
Assert.Equal(1, db.Count());
}
[Fact]
public void TestRemove()
{
var db = GetTempDb();
string id = Guid.NewGuid().ToString();
var interval1 = new TimelineInterval
{
Id = id,
IntervalStart = DateTimeOffset.Now,
IntervalStop = DateTimeOffset.Now.AddDays(1),
TimelineName = "Name1"
};
db.AddOrUpdate(interval1);
db.AddOrUpdate(new TimelineInterval());
Assert.Equal(2, db.Count());
Assert.True(db.Remove(interval1));
Assert.Equal(1, db.Count());
}
[Fact]
public void TestRemoveRange()
{
var db = GetTempDb();
var interval1 = new TimelineInterval
{
IntervalStart = DateTimeOffset.Now,
IntervalStop = DateTimeOffset.Now.AddDays(1),
TimelineName = "Name1"
};
var interval2 = new TimelineInterval
{
IntervalStart = DateTimeOffset.Now,
IntervalStop = DateTimeOffset.Now.AddDays(1),
TimelineName = "Name1"
};
var interval3 = new TimelineInterval
{
IntervalStart = DateTimeOffset.Now,
IntervalStop = DateTimeOffset.Now.AddDays(1),
TimelineName = "Name1"
};
db.AddOrUpdate(interval1);
db.AddOrUpdate(interval2);
db.AddOrUpdate(interval3);
db.AddOrUpdate(new TimelineInterval());
Assert.Equal(4, db.Count());
Assert.True(db.RemoveRange(new List<TimelineInterval> {interval2, interval3}));
Assert.Equal(2, db.Count());
Assert.Equal(interval1, db.FindById(interval1.Id));
Assert.Null(db.FindById(interval2.Id));
Assert.Null(db.FindById(interval3.Id));
}
private TimelineLiteDbRepository GetTempDb()
{
return new TimelineLiteDbRepository(new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None, 4096,
FileOptions.RandomAccess | FileOptions.DeleteOnClose));
}
}
}
\ 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