DisplayPreferencesManager.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma warning disable CA1307
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Jellyfin.Data.Entities;
  6. using MediaBrowser.Controller;
  7. using Microsoft.EntityFrameworkCore;
  8. namespace Jellyfin.Server.Implementations.Users
  9. {
  10. /// <summary>
  11. /// Manages the storage and retrieval of display preferences through Entity Framework.
  12. /// </summary>
  13. public class DisplayPreferencesManager : IDisplayPreferencesManager
  14. {
  15. private readonly JellyfinDb _dbContext;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class.
  18. /// </summary>
  19. /// <param name="dbContext">The database context.</param>
  20. public DisplayPreferencesManager(JellyfinDb dbContext)
  21. {
  22. _dbContext = dbContext;
  23. }
  24. /// <inheritdoc />
  25. public DisplayPreferences GetDisplayPreferences(Guid userId, string client)
  26. {
  27. var prefs = _dbContext.DisplayPreferences
  28. .Include(pref => pref.HomeSections)
  29. .FirstOrDefault(pref =>
  30. pref.UserId == userId && string.Equals(pref.Client, client));
  31. if (prefs == null)
  32. {
  33. prefs = new DisplayPreferences(userId, client);
  34. _dbContext.DisplayPreferences.Add(prefs);
  35. }
  36. return prefs;
  37. }
  38. /// <inheritdoc />
  39. public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client)
  40. {
  41. var prefs = _dbContext.ItemDisplayPreferences
  42. .FirstOrDefault(pref => pref.UserId == userId && pref.ItemId == itemId && string.Equals(pref.Client, client));
  43. if (prefs == null)
  44. {
  45. prefs = new ItemDisplayPreferences(userId, Guid.Empty, client);
  46. _dbContext.ItemDisplayPreferences.Add(prefs);
  47. }
  48. return prefs;
  49. }
  50. /// <inheritdoc />
  51. public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client)
  52. {
  53. return _dbContext.ItemDisplayPreferences
  54. .AsQueryable()
  55. .Where(prefs => prefs.UserId == userId && prefs.ItemId != Guid.Empty && string.Equals(prefs.Client, client))
  56. .ToList();
  57. }
  58. /// <inheritdoc />
  59. public void SaveChanges()
  60. {
  61. _dbContext.SaveChanges();
  62. }
  63. }
  64. }