Merge pull request #3849 from barronpm/scoped-displaypreferences

Make DisplayPreferencesManager Scoped
This commit is contained in:
Anthony Lavado 2020-09-03 15:23:49 -04:00 committed by GitHub
commit f02bcd9cf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 43 deletions

View File

@ -153,7 +153,6 @@ namespace Jellyfin.Api.Controllers
{ {
var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Parse(key.Substring("landing-".Length)), existingDisplayPreferences.Client); var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Parse(key.Substring("landing-".Length)), existingDisplayPreferences.Client);
itemPreferences.ViewType = Enum.Parse<ViewType>(displayPreferences.ViewType); itemPreferences.ViewType = Enum.Parse<ViewType>(displayPreferences.ViewType);
_displayPreferencesManager.SaveChanges(itemPreferences);
} }
var itemPrefs = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Empty, existingDisplayPreferences.Client); var itemPrefs = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Empty, existingDisplayPreferences.Client);
@ -167,8 +166,7 @@ namespace Jellyfin.Api.Controllers
itemPrefs.ViewType = viewType; itemPrefs.ViewType = viewType;
} }
_displayPreferencesManager.SaveChanges(existingDisplayPreferences); _displayPreferencesManager.SaveChanges();
_displayPreferencesManager.SaveChanges(itemPrefs);
return NoContent(); return NoContent();
} }

View File

@ -14,22 +14,21 @@ namespace Jellyfin.Server.Implementations.Users
/// </summary> /// </summary>
public class DisplayPreferencesManager : IDisplayPreferencesManager public class DisplayPreferencesManager : IDisplayPreferencesManager
{ {
private readonly JellyfinDbProvider _dbProvider; private readonly JellyfinDb _dbContext;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class. /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class.
/// </summary> /// </summary>
/// <param name="dbProvider">The Jellyfin db provider.</param> /// <param name="dbContext">The database context.</param>
public DisplayPreferencesManager(JellyfinDbProvider dbProvider) public DisplayPreferencesManager(JellyfinDb dbContext)
{ {
_dbProvider = dbProvider; _dbContext = dbContext;
} }
/// <inheritdoc /> /// <inheritdoc />
public DisplayPreferences GetDisplayPreferences(Guid userId, string client) public DisplayPreferences GetDisplayPreferences(Guid userId, string client)
{ {
using var dbContext = _dbProvider.CreateContext(); var prefs = _dbContext.DisplayPreferences
var prefs = dbContext.DisplayPreferences
.Include(pref => pref.HomeSections) .Include(pref => pref.HomeSections)
.FirstOrDefault(pref => .FirstOrDefault(pref =>
pref.UserId == userId && string.Equals(pref.Client, client)); pref.UserId == userId && string.Equals(pref.Client, client));
@ -37,7 +36,7 @@ namespace Jellyfin.Server.Implementations.Users
if (prefs == null) if (prefs == null)
{ {
prefs = new DisplayPreferences(userId, client); prefs = new DisplayPreferences(userId, client);
dbContext.DisplayPreferences.Add(prefs); _dbContext.DisplayPreferences.Add(prefs);
} }
return prefs; return prefs;
@ -46,14 +45,13 @@ namespace Jellyfin.Server.Implementations.Users
/// <inheritdoc /> /// <inheritdoc />
public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client) public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client)
{ {
using var dbContext = _dbProvider.CreateContext(); var prefs = _dbContext.ItemDisplayPreferences
var prefs = dbContext.ItemDisplayPreferences
.FirstOrDefault(pref => pref.UserId == userId && pref.ItemId == itemId && string.Equals(pref.Client, client)); .FirstOrDefault(pref => pref.UserId == userId && pref.ItemId == itemId && string.Equals(pref.Client, client));
if (prefs == null) if (prefs == null)
{ {
prefs = new ItemDisplayPreferences(userId, Guid.Empty, client); prefs = new ItemDisplayPreferences(userId, Guid.Empty, client);
dbContext.ItemDisplayPreferences.Add(prefs); _dbContext.ItemDisplayPreferences.Add(prefs);
} }
return prefs; return prefs;
@ -62,27 +60,15 @@ namespace Jellyfin.Server.Implementations.Users
/// <inheritdoc /> /// <inheritdoc />
public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client) public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client)
{ {
using var dbContext = _dbProvider.CreateContext(); return _dbContext.ItemDisplayPreferences
return dbContext.ItemDisplayPreferences
.Where(prefs => prefs.UserId == userId && prefs.ItemId != Guid.Empty && string.Equals(prefs.Client, client)) .Where(prefs => prefs.UserId == userId && prefs.ItemId != Guid.Empty && string.Equals(prefs.Client, client))
.ToList(); .ToList();
} }
/// <inheritdoc /> /// <inheritdoc />
public void SaveChanges(DisplayPreferences preferences) public void SaveChanges()
{ {
using var dbContext = _dbProvider.CreateContext(); _dbContext.SaveChanges();
dbContext.Update(preferences);
dbContext.SaveChanges();
}
/// <inheritdoc />
public void SaveChanges(ItemDisplayPreferences preferences)
{
using var dbContext = _dbProvider.CreateContext();
dbContext.Update(preferences);
dbContext.SaveChanges();
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Reflection; using System.Reflection;
using Emby.Drawing; using Emby.Drawing;
using Emby.Server.Implementations; using Emby.Server.Implementations;
@ -15,6 +16,7 @@ using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Activity; using MediaBrowser.Model.Activity;
using MediaBrowser.Model.IO; using MediaBrowser.Model.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -67,12 +69,8 @@ namespace Jellyfin.Server
Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}."); Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}.");
} }
// TODO: Set up scoping and use AddDbContextPool, ServiceCollection.AddDbContextPool<JellyfinDb>(
// can't register as Transient since tracking transient in GC is funky options => options.UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"));
// serviceCollection.AddDbContext<JellyfinDb>(
// options => options
// .UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"),
// ServiceLifetime.Transient);
ServiceCollection.AddEventServices(); ServiceCollection.AddEventServices();
ServiceCollection.AddSingleton<IEventManager, EventManager>(); ServiceCollection.AddSingleton<IEventManager, EventManager>();

View File

@ -35,15 +35,8 @@ namespace MediaBrowser.Controller
IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client); IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client);
/// <summary> /// <summary>
/// Saves changes to the provided display preferences. /// Saves changes made to the database.
/// </summary> /// </summary>
/// <param name="preferences">The display preferences to save.</param> void SaveChanges();
void SaveChanges(DisplayPreferences preferences);
/// <summary>
/// Saves changes to the provided item display preferences.
/// </summary>
/// <param name="preferences">The item display preferences to save.</param>
void SaveChanges(ItemDisplayPreferences preferences);
} }
} }