< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.Users.DisplayPreferencesManager
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 108
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetDisplayPreferences(...)0%620%
GetItemDisplayPreferences(...)0%620%
ListItemDisplayPreferences(...)100%210%
ListCustomItemDisplayPreferences(...)100%210%
SetCustomItemDisplayPreferences(...)0%620%
SaveChanges()100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs

#LineLine coverage
 1#pragma warning disable CA1307
 2#pragma warning disable CA1309
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using System.Threading.Tasks;
 8using Jellyfin.Data.Entities;
 9using MediaBrowser.Controller;
 10using Microsoft.EntityFrameworkCore;
 11
 12namespace Jellyfin.Server.Implementations.Users
 13{
 14    /// <summary>
 15    /// Manages the storage and retrieval of display preferences through Entity Framework.
 16    /// </summary>
 17    public sealed class DisplayPreferencesManager : IDisplayPreferencesManager, IAsyncDisposable
 18    {
 19        private readonly JellyfinDbContext _dbContext;
 20
 21        /// <summary>
 22        /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class.
 23        /// </summary>
 24        /// <param name="dbContextFactory">The database context factory.</param>
 25        public DisplayPreferencesManager(IDbContextFactory<JellyfinDbContext> dbContextFactory)
 26        {
 027            _dbContext = dbContextFactory.CreateDbContext();
 028        }
 29
 30        /// <inheritdoc />
 31        public DisplayPreferences GetDisplayPreferences(Guid userId, Guid itemId, string client)
 32        {
 033            var prefs = _dbContext.DisplayPreferences
 034                .Include(pref => pref.HomeSections)
 035                .FirstOrDefault(pref =>
 036                    pref.UserId.Equals(userId) && string.Equals(pref.Client, client) && pref.ItemId.Equals(itemId));
 37
 038            if (prefs is null)
 39            {
 040                prefs = new DisplayPreferences(userId, itemId, client);
 041                _dbContext.DisplayPreferences.Add(prefs);
 42            }
 43
 044            return prefs;
 45        }
 46
 47        /// <inheritdoc />
 48        public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client)
 49        {
 050            var prefs = _dbContext.ItemDisplayPreferences
 051                .FirstOrDefault(pref => pref.UserId.Equals(userId) && pref.ItemId.Equals(itemId) && string.Equals(pref.C
 52
 053            if (prefs is null)
 54            {
 055                prefs = new ItemDisplayPreferences(userId, Guid.Empty, client);
 056                _dbContext.ItemDisplayPreferences.Add(prefs);
 57            }
 58
 059            return prefs;
 60        }
 61
 62        /// <inheritdoc />
 63        public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client)
 64        {
 065            return _dbContext.ItemDisplayPreferences
 066                .Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && string.Equals(prefs.Clie
 067                .ToList();
 68        }
 69
 70        /// <inheritdoc />
 71        public Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client)
 72        {
 073            return _dbContext.CustomItemDisplayPreferences
 074                .Where(prefs => prefs.UserId.Equals(userId)
 075                                && prefs.ItemId.Equals(itemId)
 076                                && string.Equals(prefs.Client, client))
 077                .ToDictionary(prefs => prefs.Key, prefs => prefs.Value);
 78        }
 79
 80        /// <inheritdoc />
 81        public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?>
 82        {
 083            var existingPrefs = _dbContext.CustomItemDisplayPreferences
 084                .Where(prefs => prefs.UserId.Equals(userId)
 085                                && prefs.ItemId.Equals(itemId)
 086                                && string.Equals(prefs.Client, client));
 087            _dbContext.CustomItemDisplayPreferences.RemoveRange(existingPrefs);
 88
 089            foreach (var (key, value) in customPreferences)
 90            {
 091                _dbContext.CustomItemDisplayPreferences
 092                    .Add(new CustomItemDisplayPreferences(userId, itemId, client, key, value));
 93            }
 094        }
 95
 96        /// <inheritdoc />
 97        public void SaveChanges()
 98        {
 099            _dbContext.SaveChanges();
 0100        }
 101
 102        /// <inheritdoc />
 103        public async ValueTask DisposeAsync()
 104        {
 105            await _dbContext.DisposeAsync().ConfigureAwait(false);
 106        }
 107    }
 108}