DbAccess.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using BanTur.Core.Entity;
  2. using BanTur.Core.EntityFramework;
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. namespace BanTur.Core.Utility
  9. {
  10. public static class DbAccess
  11. {
  12. static DbAccess()
  13. {
  14. if (File.Exists(PathHelper.DbFilePath) == false)
  15. {
  16. using var ctx = GetContext();
  17. ctx.Database.EnsureCreated();
  18. var configProps = typeof(Config).GetProperties(BindingFlags.Public | BindingFlags.Static).Select(p => new { p.Name, p.PropertyType }).ToArray();
  19. foreach (var prop in configProps)
  20. {
  21. ctx.ConfigEntries.Add(prop.PropertyType.IsValueType
  22. ? new ConfigEntry { Key = prop.Name, Type = prop.PropertyType.Name, Value = Activator.CreateInstance(prop.PropertyType)?.ToString() }
  23. : new ConfigEntry { Key = prop.Name, Type = prop.PropertyType.Name });
  24. }
  25. ctx.SaveChanges();
  26. }
  27. }
  28. private static BanTurContext GetContext() => new BanTurContext();
  29. public static FeedSource[] GetFeedSources()
  30. {
  31. using var ctx = GetContext();
  32. return ctx.FeedSources.Where(p => p.Enable).ToArray();
  33. }
  34. public static bool CheckExist(BangumiEntry entry)
  35. {
  36. using var ctx = GetContext();
  37. return ctx.BangumiEntries.Any(p => p.Magnet == entry.Magnet);
  38. }
  39. public static void AddEntry(BangumiEntry entry)
  40. {
  41. using var ctx = GetContext();
  42. ctx.BangumiEntries.Add(entry);
  43. ctx.SaveChanges();
  44. }
  45. public static BangumiEntry[] GetNewEntries()
  46. {
  47. using var ctx = GetContext();
  48. return ctx.BangumiEntries.Where(p => p.Status == BangumiEntryStatus.New).ToArray();
  49. }
  50. public static BangumiEntry GetEntry(int id)
  51. {
  52. using var ctx = GetContext();
  53. return ctx.BangumiEntries.FirstOrDefault(p => p.Id == id);
  54. }
  55. public static bool UpdateStatus(int id, BangumiEntryStatus status, string filePath = null)
  56. {
  57. using var ctx = GetContext();
  58. var ce = ctx.BangumiEntries.FirstOrDefault(p => p.Id == id);
  59. if (ce == null) return false;
  60. ce.Status = status;
  61. ce.FilePath = filePath;
  62. return ctx.SaveChanges() == 1;
  63. }
  64. public static class Config
  65. {
  66. private static string GetConfig([CallerMemberName] string key = null)
  67. {
  68. using var ctx = GetContext();
  69. return ctx.ConfigEntries.Where(p => p.Key == key).Select(p => p.Value).FirstOrDefault();
  70. }
  71. private static T GetConfig<T>([CallerMemberName] string key = null)
  72. {
  73. var value = GetConfig(key);
  74. return (T)Convert.ChangeType(value, typeof(T));
  75. }
  76. public static bool EnableMail => GetConfig<bool>();
  77. public static string SendMailHost => GetConfig();
  78. public static int SendMailPort => GetConfig<int>();
  79. public static string SendMailUser => GetConfig();
  80. public static string SendMailPass => GetConfig();
  81. public static string SendMailTarget => GetConfig();
  82. public static string SendMailTargetName => GetConfig();
  83. public static string WorkingDirectory => GetConfig();
  84. public static string DownloadDirectory => GetConfig();
  85. public static string WebProxy => GetConfig();
  86. }
  87. }
  88. }