< 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: 52
Coverable lines: 52
Total lines: 116
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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Jellyfin.Database.Implementations;
 5using Jellyfin.Database.Implementations.Entities;
 6using MediaBrowser.Controller;
 7using Microsoft.EntityFrameworkCore;
 8
 9namespace Jellyfin.Server.Implementations.Users;
 10
 11/// <summary>
 12/// Manages the storage and retrieval of display preferences through Entity Framework.
 13/// </summary>
 14public sealed class DisplayPreferencesManager : IDisplayPreferencesManager
 15{
 16    private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory;
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class.
 20    /// </summary>
 21    /// <param name="dbContextFactory">The database context factory.</param>
 22    public DisplayPreferencesManager(IDbContextFactory<JellyfinDbContext> dbContextFactory)
 23    {
 024        _dbContextFactory = dbContextFactory;
 025    }
 26
 27    /// <inheritdoc />
 28    public DisplayPreferences GetDisplayPreferences(Guid userId, Guid itemId, string client)
 29    {
 030        using var dbContext = _dbContextFactory.CreateDbContext();
 031        var prefs = dbContext.DisplayPreferences
 032            .Include(pref => pref.HomeSections)
 033            .FirstOrDefault(pref =>
 034                pref.UserId.Equals(userId) && pref.Client == client && pref.ItemId.Equals(itemId));
 35
 036        if (prefs is null)
 37        {
 038            prefs = new DisplayPreferences(userId, itemId, client);
 039            dbContext.DisplayPreferences.Add(prefs);
 040            dbContext.SaveChanges();
 41        }
 42
 043        return prefs;
 044    }
 45
 46    /// <inheritdoc />
 47    public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client)
 48    {
 049        using var dbContext = _dbContextFactory.CreateDbContext();
 050        var prefs = dbContext.ItemDisplayPreferences
 051            .FirstOrDefault(pref => pref.UserId.Equals(userId) && pref.ItemId.Equals(itemId) && pref.Client == client);
 52
 053        if (prefs is null)
 54        {
 055            prefs = new ItemDisplayPreferences(userId, Guid.Empty, client);
 056            dbContext.ItemDisplayPreferences.Add(prefs);
 057            dbContext.SaveChanges();
 58        }
 59
 060        return prefs;
 061    }
 62
 63    /// <inheritdoc />
 64    public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client)
 65    {
 066        using var dbContext = _dbContextFactory.CreateDbContext();
 067        return dbContext.ItemDisplayPreferences
 068            .Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && prefs.Client == client)
 069            .ToList();
 070    }
 71
 72    /// <inheritdoc />
 73    public Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client)
 74    {
 075        using var dbContext = _dbContextFactory.CreateDbContext();
 076        return dbContext.CustomItemDisplayPreferences
 077            .Where(prefs => prefs.UserId.Equals(userId)
 078                            && prefs.ItemId.Equals(itemId)
 079                            && prefs.Client == client)
 080            .ToDictionary(prefs => prefs.Key, prefs => prefs.Value);
 081    }
 82
 83    /// <inheritdoc />
 84    public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?> cus
 85    {
 086        using var dbContext = _dbContextFactory.CreateDbContext();
 087        dbContext.CustomItemDisplayPreferences.Where(prefs => prefs.UserId.Equals(userId)
 088                            && prefs.ItemId.Equals(itemId)
 089                            && prefs.Client == client)
 090                            .ExecuteDelete();
 91
 092        foreach (var (key, value) in customPreferences)
 93        {
 094            dbContext.CustomItemDisplayPreferences
 095                .Add(new CustomItemDisplayPreferences(userId, itemId, client, key, value));
 96        }
 97
 098        dbContext.SaveChanges();
 099    }
 100
 101    /// <inheritdoc/>
 102    public void UpdateDisplayPreferences(DisplayPreferences displayPreferences)
 103    {
 0104        using var dbContext = _dbContextFactory.CreateDbContext();
 0105        dbContext.DisplayPreferences.Attach(displayPreferences).State = EntityState.Modified;
 0106        dbContext.SaveChanges();
 0107    }
 108
 109    /// <inheritdoc/>
 110    public void UpdateItemDisplayPreferences(ItemDisplayPreferences itemDisplayPreferences)
 111    {
 0112        using var dbContext = _dbContextFactory.CreateDbContext();
 0113        dbContext.ItemDisplayPreferences.Attach(itemDisplayPreferences).State = EntityState.Modified;
 0114        dbContext.SaveChanges();
 0115    }
 116}