jellyfin/MediaBrowser.Server.Implementations/Social/SharingRepository.cs

159 lines
5.3 KiB
C#
Raw Normal View History

2015-06-30 23:59:45 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Social;
using MediaBrowser.Server.Implementations.Persistence;
using System;
using System.Data;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Social
{
public class SharingRepository : BaseSqliteRepository
{
2016-06-11 15:55:05 +00:00
public SharingRepository(ILogManager logManager, IApplicationPaths appPaths, IDbConnector dbConnector)
: base(logManager, dbConnector)
2015-06-30 23:59:45 +00:00
{
2016-06-11 15:55:05 +00:00
DbFilePath = Path.Combine(appPaths.DataPath, "shares.db");
2015-06-30 23:59:45 +00:00
}
/// <summary>
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
2016-06-11 15:55:05 +00:00
public async Task Initialize()
2015-06-30 23:59:45 +00:00
{
2016-06-11 15:55:05 +00:00
using (var connection = await CreateConnection().ConfigureAwait(false))
{
string[] queries = {
2015-06-30 23:59:45 +00:00
"create table if not exists Shares (Id GUID, ItemId TEXT, UserId TEXT, ExpirationDate DateTime, PRIMARY KEY (Id))",
"create index if not exists idx_Shares on Shares(Id)",
"pragma shrink_memory"
};
2016-06-11 15:55:05 +00:00
connection.RunQueries(queries, Logger);
}
2015-06-30 23:59:45 +00:00
}
public async Task CreateShare(SocialShareInfo info)
{
if (info == null)
{
throw new ArgumentNullException("info");
}
if (string.IsNullOrWhiteSpace(info.Id))
{
throw new ArgumentNullException("info.Id");
}
var cancellationToken = CancellationToken.None;
cancellationToken.ThrowIfCancellationRequested();
2016-06-11 15:55:05 +00:00
using (var connection = await CreateConnection().ConfigureAwait(false))
2015-06-30 23:59:45 +00:00
{
2016-06-11 15:55:05 +00:00
using (var saveShareCommand = connection.CreateCommand())
2015-06-30 23:59:45 +00:00
{
2016-06-11 15:55:05 +00:00
saveShareCommand.CommandText = "replace into Shares (Id, ItemId, UserId, ExpirationDate) values (@Id, @ItemId, @UserId, @ExpirationDate)";
saveShareCommand.Parameters.Add(saveShareCommand, "@Id");
saveShareCommand.Parameters.Add(saveShareCommand, "@ItemId");
saveShareCommand.Parameters.Add(saveShareCommand, "@UserId");
saveShareCommand.Parameters.Add(saveShareCommand, "@ExpirationDate");
IDbTransaction transaction = null;
try
{
transaction = connection.BeginTransaction();
saveShareCommand.GetParameter(0).Value = new Guid(info.Id);
saveShareCommand.GetParameter(1).Value = info.ItemId;
saveShareCommand.GetParameter(2).Value = info.UserId;
saveShareCommand.GetParameter(3).Value = info.ExpirationDate;
saveShareCommand.Transaction = transaction;
saveShareCommand.ExecuteNonQuery();
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
Logger.ErrorException("Failed to save share:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
}
2015-06-30 23:59:45 +00:00
}
}
}
public SocialShareInfo GetShareInfo(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentNullException("id");
}
2016-06-11 15:55:05 +00:00
using (var connection = CreateConnection(true).Result)
{
var cmd = connection.CreateCommand();
cmd.CommandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = @id";
2015-06-30 23:59:45 +00:00
2016-06-11 15:55:05 +00:00
cmd.Parameters.Add(cmd, "@id", DbType.Guid).Value = new Guid(id);
2015-06-30 23:59:45 +00:00
2016-06-11 15:55:05 +00:00
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
2015-06-30 23:59:45 +00:00
{
2016-06-11 15:55:05 +00:00
if (reader.Read())
{
return GetSocialShareInfo(reader);
}
2015-06-30 23:59:45 +00:00
}
2016-06-11 15:55:05 +00:00
return null;
}
2015-06-30 23:59:45 +00:00
}
private SocialShareInfo GetSocialShareInfo(IDataReader reader)
{
var info = new SocialShareInfo();
info.Id = reader.GetGuid(0).ToString("N");
info.ItemId = reader.GetString(1);
info.UserId = reader.GetString(2);
info.ExpirationDate = reader.GetDateTime(3).ToUniversalTime();
return info;
}
public async Task DeleteShare(string id)
{
}
}
}