Add retrieve and save all display prefs

This commit is contained in:
Eric Reed 2014-09-25 16:20:17 -04:00
parent 8e2d244749
commit 08c6b9a42b
3 changed files with 128 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using MediaBrowser.Model.Entities; using System.Collections.Generic;
using MediaBrowser.Model.Entities;
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -28,6 +29,15 @@ namespace MediaBrowser.Controller.Persistence
CancellationToken cancellationToken); CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Saves all display preferences for a user
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="userId">The user id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
Task SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId,
CancellationToken cancellationToken);
/// <summary>
/// Gets the display preferences. /// Gets the display preferences.
/// </summary> /// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param> /// <param name="displayPreferencesId">The display preferences id.</param>
@ -35,5 +45,12 @@ namespace MediaBrowser.Controller.Persistence
/// <param name="client">The client.</param> /// <param name="client">The client.</param>
/// <returns>Task{DisplayPreferences}.</returns> /// <returns>Task{DisplayPreferences}.</returns>
DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client); DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client);
/// <summary>
/// Gets all display preferences for the given user.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>Task{DisplayPreferences}.</returns>
IEnumerable<DisplayPreferences> GetAllDisplayPreferences(Guid userId);
} }
} }

View File

@ -98,6 +98,10 @@ namespace MediaBrowser.Model.Entities
/// </summary> /// </summary>
/// <value><c>true</c> if [show sidebar]; otherwise, <c>false</c>.</value> /// <value><c>true</c> if [show sidebar]; otherwise, <c>false</c>.</value>
public bool ShowSidebar { get; set; } public bool ShowSidebar { get; set; }
/// <summary>
/// Gets or sets the client
/// </summary>
public string Client { get; set; }
/// <summary> /// <summary>
/// Increases the size of the image. /// Increases the size of the image.

View File

@ -1,4 +1,5 @@
using MediaBrowser.Common.Configuration; using System.Collections.Generic;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
@ -176,6 +177,84 @@ namespace MediaBrowser.Server.Implementations.Persistence
} }
} }
/// <summary>
/// Save all display preferences associated with a user in the repo
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="userId">The user id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
public async Task SaveAllDisplayPreferences(IEnumerable<DisplayPreferences> displayPreferences, Guid userId, CancellationToken cancellationToken)
{
if (displayPreferences == null)
{
throw new ArgumentNullException("displayPreferences");
}
cancellationToken.ThrowIfCancellationRequested();
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
IDbTransaction transaction = null;
try
{
transaction = _connection.BeginTransaction();
foreach (var displayPreference in displayPreferences)
{
var serialized = _jsonSerializer.SerializeToBytes(displayPreference);
using (var cmd = _connection.CreateCommand())
{
cmd.CommandText = "replace into userdisplaypreferences (id, userid, client, data) values (@1, @2, @3, @4)";
cmd.Parameters.Add(cmd, "@1", DbType.Guid).Value = new Guid(displayPreference.Id);
cmd.Parameters.Add(cmd, "@2", DbType.Guid).Value = userId;
cmd.Parameters.Add(cmd, "@3", DbType.String).Value = displayPreference.Client;
cmd.Parameters.Add(cmd, "@4", DbType.Binary).Value = serialized;
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
}
}
transaction.Commit();
}
catch (OperationCanceledException)
{
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
catch (Exception e)
{
_logger.ErrorException("Failed to save display preferences:", e);
if (transaction != null)
{
transaction.Rollback();
}
throw;
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
_writeLock.Release();
}
}
/// <summary> /// <summary>
/// Gets the display preferences. /// Gets the display preferences.
/// </summary> /// </summary>
@ -217,6 +296,32 @@ namespace MediaBrowser.Server.Implementations.Persistence
}; };
} }
/// <summary>
/// Gets all display preferences for the given user.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>Task{DisplayPreferences}.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
public IEnumerable<DisplayPreferences> GetAllDisplayPreferences(Guid userId)
{
var cmd = _connection.CreateCommand();
cmd.CommandText = "select data from userdisplaypreferences where userId=@userId";
cmd.Parameters.Add(cmd, "@userId", DbType.Guid).Value = userId;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (reader.Read())
{
using (var stream = reader.GetMemoryStream(0))
{
yield return _jsonSerializer.DeserializeFromStream<DisplayPreferences>(stream);
}
}
}
}
/// <summary> /// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary> /// </summary>