DisplayPreferencesManager.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 JellyfinDbProvider _dbProvider;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class.
  18. /// </summary>
  19. /// <param name="dbProvider">The Jellyfin db provider.</param>
  20. public DisplayPreferencesManager(JellyfinDbProvider dbProvider)
  21. {
  22. _dbProvider = dbProvider;
  23. }
  24. /// <inheritdoc />
  25. public DisplayPreferences GetDisplayPreferences(Guid userId, string client)
  26. {
  27. using var dbContext = _dbProvider.CreateContext();
  28. var prefs = dbContext.DisplayPreferences
  29. .Include(pref => pref.HomeSections)
  30. .FirstOrDefault(pref =>
  31. pref.UserId == userId && string.Equals(pref.Client, client));
  32. if (prefs == null)
  33. {
  34. prefs = new DisplayPreferences(userId, client);
  35. dbContext.DisplayPreferences.Add(prefs);
  36. }
  37. return prefs;
  38. }
  39. /// <inheritdoc />
  40. public ItemDisplayPreferences GetItemDisplayPreferences(Guid userId, Guid itemId, string client)
  41. {
  42. using var dbContext = _dbProvider.CreateContext();
  43. var prefs = dbContext.ItemDisplayPreferences
  44. .FirstOrDefault(pref => pref.UserId == userId && pref.ItemId == itemId && string.Equals(pref.Client, client));
  45. if (prefs == null)
  46. {
  47. prefs = new ItemDisplayPreferences(userId, Guid.Empty, client);
  48. dbContext.ItemDisplayPreferences.Add(prefs);
  49. }
  50. return prefs;
  51. }
  52. /// <inheritdoc />
  53. public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client)
  54. {
  55. using var dbContext = _dbProvider.CreateContext();
  56. return dbContext.ItemDisplayPreferences
  57. .Where(prefs => prefs.UserId == userId && prefs.ItemId != Guid.Empty && string.Equals(prefs.Client, client))
  58. .ToList();
  59. }
  60. /// <inheritdoc />
  61. public void SaveChanges(DisplayPreferences preferences)
  62. {
  63. using var dbContext = _dbProvider.CreateContext();
  64. dbContext.Update(preferences);
  65. dbContext.SaveChanges();
  66. }
  67. /// <inheritdoc />
  68. public void SaveChanges(ItemDisplayPreferences preferences)
  69. {
  70. using var dbContext = _dbProvider.CreateContext();
  71. dbContext.Update(preferences);
  72. dbContext.SaveChanges();
  73. }
  74. }
  75. }