SpecificPackDao.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using CardCollector.DataBase.Entity;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace CardCollector.DataBase.EntityDao
  7. {
  8. public static class SpecificPackDao
  9. {
  10. private static readonly CardCollectorDatabase Instance = CardCollectorDatabase.GetSpecificInstance(typeof(SpecificPackDao));
  11. private static readonly DbSet<SpecificPacksEntity> Table = Instance.SpecificPacks;
  12. public static async Task<SpecificPacksEntity> GetInfo(long userId, int packId)
  13. {
  14. return await Table.FirstOrDefaultAsync(item => item.PackId == packId && item.UserId == userId)
  15. ?? await AddNew(userId, packId, 0);
  16. }
  17. public static async Task<SpecificPacksEntity> AddNew(long userId, int packId, int count)
  18. {
  19. var pack = await Table.FirstOrDefaultAsync(item => item.UserId == userId && item.PackId == packId) ??
  20. (await Table.AddAsync(new SpecificPacksEntity
  21. {
  22. UserId = userId,
  23. PackId = packId,
  24. Count = count
  25. })).Entity;
  26. return pack;
  27. }
  28. public static async Task<int> GetCount(long userId)
  29. {
  30. return (await Table.WhereAsync(item => item.UserId == userId)).Sum(item => item.Count);
  31. }
  32. public static async Task<List<SpecificPacksEntity>> GetUserPacks(long userId)
  33. {
  34. return (await Table.WhereAsync(item => item.UserId == userId && item.Count > 0)).ToList();
  35. }
  36. }
  37. }