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);
itemPreferences.ViewType = Enum.Parse<ViewType>(displayPreferences.ViewType);
_displayPreferencesManager.SaveChanges(itemPreferences);
}
var itemPrefs = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Empty, existingDisplayPreferences.Client);
@ -167,8 +166,7 @@ namespace Jellyfin.Api.Controllers
itemPrefs.ViewType = viewType;
}
_displayPreferencesManager.SaveChanges(existingDisplayPreferences);
_displayPreferencesManager.SaveChanges(itemPrefs);
_displayPreferencesManager.SaveChanges();
return NoContent();
}

View File

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

View File

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

View File

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