| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace OzonRepositories.Data |
| | | 10 | | { |
| | | 11 | | public class FidStatRepository : MainRepository |
| | | 12 | | { |
| | 0 | 13 | | public FidStatRepository(ApplicationDbContext orderContext) : base(orderContext) { } |
| | | 14 | | |
| | | 15 | | public async Task<int> Add(FidStatSnapshot snapshot) |
| | | 16 | | { |
| | 0 | 17 | | await _context.FidStatSnapshots.AddAsync(snapshot); |
| | 0 | 18 | | return await SaveChanges(); |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<List<FidStatSnapshot>> GetAll() |
| | | 22 | | { |
| | 0 | 23 | | return await _context.FidStatSnapshots |
| | 0 | 24 | | .OrderByDescending(s => s.PostTime) |
| | 0 | 25 | | .ToListAsync(); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<List<FidStatSnapshot>> GetFiltered( |
| | | 29 | | string? fidName, |
| | | 30 | | DateTime? dateFrom, |
| | | 31 | | DateTime? dateTo, |
| | | 32 | | int skip, |
| | | 33 | | int take) |
| | | 34 | | { |
| | 0 | 35 | | var query = _context.FidStatSnapshots.AsQueryable(); |
| | | 36 | | |
| | 0 | 37 | | if (!string.IsNullOrWhiteSpace(fidName)) |
| | 0 | 38 | | query = query.Where(s => s.FidName == fidName); |
| | | 39 | | |
| | 0 | 40 | | if (dateFrom.HasValue) |
| | 0 | 41 | | query = query.Where(s => s.PostTime >= new DateTimeOffset(dateFrom.Value)); |
| | | 42 | | |
| | 0 | 43 | | if (dateTo.HasValue) |
| | 0 | 44 | | query = query.Where(s => s.PostTime <= new DateTimeOffset(dateTo.Value.AddDays(1))); |
| | | 45 | | |
| | 0 | 46 | | return await query |
| | 0 | 47 | | .OrderByDescending(s => s.PostTime) |
| | 0 | 48 | | .Skip(skip) |
| | 0 | 49 | | .Take(take) |
| | 0 | 50 | | .ToListAsync(); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public async Task<int> GetFilteredCount(string? fidName, DateTime? dateFrom, DateTime? dateTo) |
| | | 54 | | { |
| | 0 | 55 | | var query = _context.FidStatSnapshots.AsQueryable(); |
| | | 56 | | |
| | 0 | 57 | | if (!string.IsNullOrWhiteSpace(fidName)) |
| | 0 | 58 | | query = query.Where(s => s.FidName == fidName); |
| | | 59 | | |
| | 0 | 60 | | if (dateFrom.HasValue) |
| | 0 | 61 | | query = query.Where(s => s.PostTime >= new DateTimeOffset(dateFrom.Value)); |
| | | 62 | | |
| | 0 | 63 | | if (dateTo.HasValue) |
| | 0 | 64 | | query = query.Where(s => s.PostTime <= new DateTimeOffset(dateTo.Value.AddDays(1))); |
| | | 65 | | |
| | 0 | 66 | | return await query.CountAsync(); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<FidStatSnapshot?> GetByIdWithItems(int id) |
| | | 70 | | { |
| | 0 | 71 | | return await _context.FidStatSnapshots |
| | 0 | 72 | | .Include(s => s.Items) |
| | 0 | 73 | | .FirstOrDefaultAsync(s => s.Id == id); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task<List<string>> GetDistinctFidNames() |
| | | 77 | | { |
| | 0 | 78 | | return await _context.FidStatSnapshots |
| | 0 | 79 | | .Select(s => s.FidName) |
| | 0 | 80 | | .Distinct() |
| | 0 | 81 | | .OrderBy(n => n) |
| | 0 | 82 | | .ToListAsync(); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public async Task<FidStatSnapshot?> GetLatest(string fidName) |
| | | 86 | | { |
| | 0 | 87 | | return await _context.FidStatSnapshots |
| | 0 | 88 | | .Include(s => s.Items) |
| | 0 | 89 | | .Where(s => s.FidName == fidName) |
| | 0 | 90 | | .OrderByDescending(s => s.PostTime) |
| | 0 | 91 | | .FirstOrDefaultAsync(); |
| | 0 | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |