DisplayPreferencesManager.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. .Where(prefs => prefs.UserId == userId && prefs.ItemId != Guid.Empty && string.Equals(prefs.Client, client))
  55. .ToList();
  56. }
  57. /// <inheritdoc />
  58. public void SaveChanges()
  59. {
  60. _dbContext.SaveChanges();
  61. }
  62. }
  63. }