Clean up and document ActivityRepository.cs

pull/2874/head
Patrick Barron 5 years ago
parent af4d617df2
commit f6899de338

@ -1,5 +1,3 @@
#pragma warning disable CS1591
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -15,11 +13,18 @@ using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Activity namespace Emby.Server.Implementations.Activity
{ {
/// <inheritdoc cref="IActivityRepository" />
public class ActivityRepository : BaseSqliteRepository, IActivityRepository public class ActivityRepository : BaseSqliteRepository, IActivityRepository
{ {
private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US")); private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US"));
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="ActivityRepository"/> class.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="appPaths">The server application paths.</param>
/// <param name="fileSystem">The filesystem.</param>
public ActivityRepository(ILoggerFactory loggerFactory, IServerApplicationPaths appPaths, IFileSystem fileSystem) public ActivityRepository(ILoggerFactory loggerFactory, IServerApplicationPaths appPaths, IFileSystem fileSystem)
: base(loggerFactory.CreateLogger(nameof(ActivityRepository))) : base(loggerFactory.CreateLogger(nameof(ActivityRepository)))
{ {
@ -27,6 +32,9 @@ namespace Emby.Server.Implementations.Activity
_fileSystem = fileSystem; _fileSystem = fileSystem;
} }
/// <summary>
/// Initializes the <see cref="ActivityRepository"/>.
/// </summary>
public void Initialize() public void Initialize()
{ {
try try
@ -45,8 +53,7 @@ namespace Emby.Server.Implementations.Activity
private void InitializeInternal() private void InitializeInternal()
{ {
using (var connection = GetConnection()) using var connection = GetConnection();
{
connection.RunQueries(new[] connection.RunQueries(new[]
{ {
"create table if not exists ActivityLog (Id INTEGER PRIMARY KEY, Name TEXT NOT NULL, Overview TEXT, ShortOverview TEXT, Type TEXT NOT NULL, ItemId TEXT, UserId TEXT, DateCreated DATETIME NOT NULL, LogSeverity TEXT NOT NULL)", "create table if not exists ActivityLog (Id INTEGER PRIMARY KEY, Name TEXT NOT NULL, Overview TEXT, ShortOverview TEXT, Type TEXT NOT NULL, ItemId TEXT, UserId TEXT, DateCreated DATETIME NOT NULL, LogSeverity TEXT NOT NULL)",
@ -55,7 +62,6 @@ namespace Emby.Server.Implementations.Activity
TryMigrate(connection); TryMigrate(connection);
} }
}
private void TryMigrate(ManagedConnection connection) private void TryMigrate(ManagedConnection connection)
{ {
@ -78,6 +84,7 @@ namespace Emby.Server.Implementations.Activity
private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLog"; private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLog";
/// <inheritdoc />
public void Create(ActivityLogEntry entry) public void Create(ActivityLogEntry entry)
{ {
if (entry == null) if (entry == null)
@ -85,12 +92,10 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException(nameof(entry)); throw new ArgumentNullException(nameof(entry));
} }
using (var connection = GetConnection()) using var connection = GetConnection();
{
connection.RunInTransaction(db => connection.RunInTransaction(db =>
{ {
using (var statement = db.PrepareStatement("insert into ActivityLog (Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)")) using var statement = db.PrepareStatement("insert into ActivityLog (Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)");
{
statement.TryBind("@Name", entry.Name); statement.TryBind("@Name", entry.Name);
statement.TryBind("@Overview", entry.Overview); statement.TryBind("@Overview", entry.Overview);
@ -111,11 +116,14 @@ namespace Emby.Server.Implementations.Activity
statement.TryBind("@LogSeverity", entry.Severity.ToString()); statement.TryBind("@LogSeverity", entry.Severity.ToString());
statement.MoveNext(); statement.MoveNext();
}
}, TransactionMode); }, TransactionMode);
} }
}
/// <summary>
/// Adds the provided <see cref="ActivityLogEntry"/> to this repository.
/// </summary>
/// <param name="entry">The activity log entry.</param>
/// <exception cref="ArgumentNullException">If entry is null.</exception>
public void Update(ActivityLogEntry entry) public void Update(ActivityLogEntry entry)
{ {
if (entry == null) if (entry == null)
@ -123,12 +131,10 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException(nameof(entry)); throw new ArgumentNullException(nameof(entry));
} }
using (var connection = GetConnection()) using var connection = GetConnection();
{
connection.RunInTransaction(db => connection.RunInTransaction(db =>
{ {
using (var statement = db.PrepareStatement("Update ActivityLog set Name=@Name,Overview=@Overview,ShortOverview=@ShortOverview,Type=@Type,ItemId=@ItemId,UserId=@UserId,DateCreated=@DateCreated,LogSeverity=@LogSeverity where Id=@Id")) using var statement = db.PrepareStatement("Update ActivityLog set Name=@Name,Overview=@Overview,ShortOverview=@ShortOverview,Type=@Type,ItemId=@ItemId,UserId=@UserId,DateCreated=@DateCreated,LogSeverity=@LogSeverity where Id=@Id");
{
statement.TryBind("@Id", entry.Id); statement.TryBind("@Id", entry.Id);
statement.TryBind("@Name", entry.Name); statement.TryBind("@Name", entry.Name);
@ -150,11 +156,10 @@ namespace Emby.Server.Implementations.Activity
statement.TryBind("@LogSeverity", entry.Severity.ToString()); statement.TryBind("@LogSeverity", entry.Severity.ToString());
statement.MoveNext(); statement.MoveNext();
}
}, TransactionMode); }, TransactionMode);
} }
}
/// <inheritdoc />
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, bool? hasUserId, int? startIndex, int? limit) public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, bool? hasUserId, int? startIndex, int? limit)
{ {
var commandText = BaseActivitySelectText; var commandText = BaseActivitySelectText;
@ -164,16 +169,10 @@ namespace Emby.Server.Implementations.Activity
{ {
whereClauses.Add("DateCreated>=@DateCreated"); whereClauses.Add("DateCreated>=@DateCreated");
} }
if (hasUserId.HasValue) if (hasUserId.HasValue)
{ {
if (hasUserId.Value) whereClauses.Add(hasUserId.Value ? "UserId not null" : "UserId is null");
{
whereClauses.Add("UserId not null");
}
else
{
whereClauses.Add("UserId is null");
}
} }
var whereTextWithoutPaging = whereClauses.Count == 0 ? var whereTextWithoutPaging = whereClauses.Count == 0 ?
@ -216,8 +215,7 @@ namespace Emby.Server.Implementations.Activity
var list = new List<ActivityLogEntry>(); var list = new List<ActivityLogEntry>();
var result = new QueryResult<ActivityLogEntry>(); var result = new QueryResult<ActivityLogEntry>();
using (var connection = GetConnection(true)) using var connection = GetConnection(true);
{
connection.RunInTransaction( connection.RunInTransaction(
db => db =>
{ {
@ -230,10 +228,7 @@ namespace Emby.Server.Implementations.Activity
statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue());
} }
foreach (var row in statement.ExecuteQuery()) list.AddRange(statement.ExecuteQuery().Select(GetEntry));
{
list.Add(GetEntry(row));
}
} }
using (var statement = statements[1]) using (var statement = statements[1])
@ -247,7 +242,6 @@ namespace Emby.Server.Implementations.Activity
} }
}, },
ReadTransactionMode); ReadTransactionMode);
}
result.Items = list; result.Items = list;
return result; return result;

Loading…
Cancel
Save