SharingRepository.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Emby.Server.Implementations.Data;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Social;
  10. using SQLitePCL.pretty;
  11. namespace Emby.Server.Implementations.Social
  12. {
  13. public class SharingRepository : BaseSqliteRepository, ISharingRepository
  14. {
  15. public SharingRepository(ILogger logger, IApplicationPaths appPaths)
  16. : base(logger)
  17. {
  18. DbFilePath = Path.Combine(appPaths.DataPath, "shares.db");
  19. }
  20. /// <summary>
  21. /// Opens the connection to the database
  22. /// </summary>
  23. /// <returns>Task.</returns>
  24. public void Initialize()
  25. {
  26. using (var connection = CreateConnection())
  27. {
  28. string[] queries = {
  29. "create table if not exists Shares (Id GUID, ItemId TEXT, UserId TEXT, ExpirationDate DateTime, PRIMARY KEY (Id))",
  30. "create index if not exists idx_Shares on Shares(Id)",
  31. "pragma shrink_memory"
  32. };
  33. connection.RunQueries(queries);
  34. }
  35. }
  36. public async Task CreateShare(SocialShareInfo info)
  37. {
  38. if (info == null)
  39. {
  40. throw new ArgumentNullException("info");
  41. }
  42. if (string.IsNullOrWhiteSpace(info.Id))
  43. {
  44. throw new ArgumentNullException("info.Id");
  45. }
  46. lock (WriteLock)
  47. {
  48. using (var connection = CreateConnection())
  49. {
  50. connection.RunInTransaction(db =>
  51. {
  52. var commandText = "replace into Shares (Id, ItemId, UserId, ExpirationDate) values (?, ?, ?, ?)";
  53. db.Execute(commandText,
  54. info.Id.ToGuidParamValue(),
  55. info.ItemId,
  56. info.UserId,
  57. info.ExpirationDate.ToDateTimeParamValue());
  58. });
  59. }
  60. }
  61. }
  62. public SocialShareInfo GetShareInfo(string id)
  63. {
  64. if (string.IsNullOrWhiteSpace(id))
  65. {
  66. throw new ArgumentNullException("id");
  67. }
  68. lock (WriteLock)
  69. {
  70. using (var connection = CreateConnection(true))
  71. {
  72. var commandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = ?";
  73. var paramList = new List<object>();
  74. paramList.Add(id.ToGuidParamValue());
  75. foreach (var row in connection.Query(commandText, paramList.ToArray()))
  76. {
  77. return GetSocialShareInfo(row);
  78. }
  79. }
  80. }
  81. return null;
  82. }
  83. private SocialShareInfo GetSocialShareInfo(IReadOnlyList<IResultSetValue> reader)
  84. {
  85. var info = new SocialShareInfo();
  86. info.Id = reader[0].ReadGuid().ToString("N");
  87. info.ItemId = reader[1].ToString();
  88. info.UserId = reader[2].ToString();
  89. info.ExpirationDate = reader[3].ReadDateTime();
  90. return info;
  91. }
  92. public async Task DeleteShare(string id)
  93. {
  94. }
  95. }
  96. }