From 08c6b9a42b067af6e538f3cd65f93976ad2cd524 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Thu, 25 Sep 2014 16:20:17 -0400 Subject: [PATCH] Add retrieve and save all display prefs --- .../IDisplayPreferencesRepository.cs | 19 +++- .../Entities/DisplayPreferences.cs | 4 + .../SqliteDisplayPreferencesRepository.cs | 107 +++++++++++++++++- 3 files changed, 128 insertions(+), 2 deletions(-) diff --git a/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs b/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs index 84fedebce..66fac3462 100644 --- a/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs +++ b/MediaBrowser.Controller/Persistence/IDisplayPreferencesRepository.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Model.Entities; +using System.Collections.Generic; +using MediaBrowser.Model.Entities; using System; using System.Threading; using System.Threading.Tasks; @@ -28,6 +29,15 @@ namespace MediaBrowser.Controller.Persistence CancellationToken cancellationToken); /// + /// Saves all display preferences for a user + /// + /// The display preferences. + /// The user id. + /// The cancellation token. + /// Task. + Task SaveAllDisplayPreferences(IEnumerable displayPreferences, Guid userId, + CancellationToken cancellationToken); + /// /// Gets the display preferences. /// /// The display preferences id. @@ -35,5 +45,12 @@ namespace MediaBrowser.Controller.Persistence /// The client. /// Task{DisplayPreferences}. DisplayPreferences GetDisplayPreferences(string displayPreferencesId, Guid userId, string client); + + /// + /// Gets all display preferences for the given user. + /// + /// The user id. + /// Task{DisplayPreferences}. + IEnumerable GetAllDisplayPreferences(Guid userId); } } diff --git a/MediaBrowser.Model/Entities/DisplayPreferences.cs b/MediaBrowser.Model/Entities/DisplayPreferences.cs index cac703c91..6399670c9 100644 --- a/MediaBrowser.Model/Entities/DisplayPreferences.cs +++ b/MediaBrowser.Model/Entities/DisplayPreferences.cs @@ -98,6 +98,10 @@ namespace MediaBrowser.Model.Entities /// /// true if [show sidebar]; otherwise, false. public bool ShowSidebar { get; set; } + /// + /// Gets or sets the client + /// + public string Client { get; set; } /// /// Increases the size of the image. diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteDisplayPreferencesRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteDisplayPreferencesRepository.cs index ac1c5ef55..58ebd485c 100644 --- a/MediaBrowser.Server.Implementations/Persistence/SqliteDisplayPreferencesRepository.cs +++ b/MediaBrowser.Server.Implementations/Persistence/SqliteDisplayPreferencesRepository.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Configuration; +using System.Collections.Generic; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Entities; @@ -176,6 +177,84 @@ namespace MediaBrowser.Server.Implementations.Persistence } } + /// + /// Save all display preferences associated with a user in the repo + /// + /// The display preferences. + /// The user id. + /// The cancellation token. + /// Task. + /// item + public async Task SaveAllDisplayPreferences(IEnumerable 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(); + } + } + /// /// Gets the display preferences. /// @@ -217,6 +296,32 @@ namespace MediaBrowser.Server.Implementations.Persistence }; } + /// + /// Gets all display preferences for the given user. + /// + /// The user id. + /// Task{DisplayPreferences}. + /// item + public IEnumerable 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(stream); + } + } + } + } + /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. ///