123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using BanTur.Core.Entity;
- using BanTur.Core.EntityFramework;
- using System;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- namespace BanTur.Core.Utility
- {
- public static class DbAccess
- {
- static DbAccess()
- {
- if (File.Exists(PathHelper.DbFilePath) == false)
- {
- using var ctx = GetContext();
- ctx.Database.EnsureCreated();
- var configProps = typeof(Config).GetProperties(BindingFlags.Public | BindingFlags.Static).Select(p => new { p.Name, p.PropertyType }).ToArray();
- foreach (var prop in configProps)
- {
- ctx.ConfigEntries.Add(prop.PropertyType.IsValueType
- ? new ConfigEntry { Key = prop.Name, Type = prop.PropertyType.Name, Value = Activator.CreateInstance(prop.PropertyType)?.ToString() }
- : new ConfigEntry { Key = prop.Name, Type = prop.PropertyType.Name });
- }
- ctx.SaveChanges();
- }
- }
- private static BanTurContext GetContext() => new BanTurContext();
- public static FeedSource[] GetFeedSources()
- {
- using var ctx = GetContext();
- return ctx.FeedSources.Where(p => p.Enable).ToArray();
- }
- public static bool CheckExist(BangumiEntry entry)
- {
- using var ctx = GetContext();
- return ctx.BangumiEntries.Any(p => p.Magnet == entry.Magnet);
- }
- public static void AddEntry(BangumiEntry entry)
- {
- using var ctx = GetContext();
- ctx.BangumiEntries.Add(entry);
- ctx.SaveChanges();
- }
- public static BangumiEntry[] GetNewEntries()
- {
- using var ctx = GetContext();
- return ctx.BangumiEntries.Where(p => p.Status == BangumiEntryStatus.New).ToArray();
- }
- public static BangumiEntry GetEntry(int id)
- {
- using var ctx = GetContext();
- return ctx.BangumiEntries.FirstOrDefault(p => p.Id == id);
- }
- public static bool UpdateStatus(int id, BangumiEntryStatus status, string filePath = null)
- {
- using var ctx = GetContext();
- var ce = ctx.BangumiEntries.FirstOrDefault(p => p.Id == id);
- if (ce == null) return false;
- ce.Status = status;
- ce.FilePath = filePath;
- return ctx.SaveChanges() == 1;
- }
- public static class Config
- {
- private static string GetConfig([CallerMemberName] string key = null)
- {
- using var ctx = GetContext();
- return ctx.ConfigEntries.Where(p => p.Key == key).Select(p => p.Value).FirstOrDefault();
- }
- private static T GetConfig<T>([CallerMemberName] string key = null)
- {
- var value = GetConfig(key);
- return (T)Convert.ChangeType(value, typeof(T));
- }
- public static bool EnableMail => GetConfig<bool>();
- public static string SendMailHost => GetConfig();
- public static int SendMailPort => GetConfig<int>();
- public static string SendMailUser => GetConfig();
- public static string SendMailPass => GetConfig();
- public static string SendMailTarget => GetConfig();
- public static string SendMailTargetName => GetConfig();
- public static string WorkingDirectory => GetConfig();
- public static string DownloadDirectory => GetConfig();
- public static string WebProxy => GetConfig();
- }
- }
- }
|