SettingsDao.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using CardCollector.DataBase.Entity;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace CardCollector.DataBase.EntityDao
  6. {
  7. public static class SettingsDao
  8. {
  9. public static async Task<UserSettings> GetById(long userId)
  10. {
  11. var Table = BotDatabase.Instance.Settings;
  12. return await Table.FirstOrDefaultAsync(item => item.UserId == userId) ?? await AddNew(userId);
  13. }
  14. public static async Task<UserSettings> AddNew(long userId)
  15. {
  16. var Table = BotDatabase.Instance.Settings;
  17. var entry = new UserSettings {UserId = userId};
  18. entry.InitProperties();
  19. var result = await Table.AddAsync(entry);
  20. return result.Entity;
  21. }
  22. public static async Task<Dictionary<long, UserSettings>> GetAll()
  23. {
  24. var Table = BotDatabase.Instance.Settings;
  25. return await Table.ToDictionaryAsync(item => item.UserId, item => item);
  26. }
  27. }
  28. }