| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonRepositories.Context; |
| | | 3 | | |
| | | 4 | | namespace OzonRepositories.Data.Bitrix; |
| | | 5 | | |
| | | 6 | | public class PriceHistoryRepository : MainRepository, IRepository<PriceHistory> |
| | | 7 | | { |
| | 0 | 8 | | public PriceHistoryRepository(ApplicationDbContext orderContext) : base(orderContext) |
| | | 9 | | { |
| | 0 | 10 | | } |
| | | 11 | | |
| | | 12 | | public async Task<int> Add(PriceHistory value) |
| | | 13 | | { |
| | 0 | 14 | | _context.PriceHistories.Add(value); |
| | 0 | 15 | | return await _context.SaveChangesAsync(); |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task<int> Delete(PriceHistory value) |
| | | 19 | | { |
| | 0 | 20 | | _context.PriceHistories.Remove(value); |
| | 0 | 21 | | return await _context.SaveChangesAsync(); |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public Task<List<PriceHistory>> Get() |
| | | 25 | | { |
| | 0 | 26 | | return _context.PriceHistories.ToListAsync(); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<PriceHistory?> GetAsync(int id) |
| | | 30 | | { |
| | 0 | 31 | | return await _context.PriceHistories.FirstOrDefaultAsync(c => c.Id == id); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<PriceHistory?> GetAsync(PriceHistory value) |
| | | 35 | | { |
| | 0 | 36 | | return await _context.PriceHistories.FirstOrDefaultAsync(c => c.Article == value.Article); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<List<PriceHistory>> GetByArticleAsync(string article) |
| | | 40 | | { |
| | 0 | 41 | | return await _context.PriceHistories |
| | 0 | 42 | | .Where(ph => ph.Article == article) |
| | 0 | 43 | | .OrderByDescending(ph => ph.CreateDateTime) |
| | 0 | 44 | | .ToListAsync(); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<List<PriceHistory>> GetByBitrixIdAsync(int bitrixId) |
| | | 48 | | { |
| | 0 | 49 | | return await _context.PriceHistories |
| | 0 | 50 | | .Where(ph => ph.BitrixId == bitrixId) |
| | 0 | 51 | | .OrderByDescending(ph => ph.CreateDateTime) |
| | 0 | 52 | | .ToListAsync(); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<PriceHistory?> GetLastByBitrixIdAsync(int bitrixId) |
| | | 56 | | { |
| | 0 | 57 | | return await _context.PriceHistories |
| | 0 | 58 | | .Where(ph => ph.BitrixId == bitrixId) |
| | 0 | 59 | | .OrderByDescending(ph => ph.CreateDateTime) |
| | 0 | 60 | | .FirstOrDefaultAsync(); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public async Task<Dictionary<int, PriceHistory>> GetLastByBitrixIdsAsync(List<int> bitrixIds) |
| | | 64 | | { |
| | 0 | 65 | | return await _context.PriceHistories |
| | 0 | 66 | | .Where(ph => bitrixIds.Contains(ph.BitrixId)) |
| | 0 | 67 | | .GroupBy(ph => ph.BitrixId) |
| | 0 | 68 | | .Select(g => g.OrderByDescending(ph => ph.CreateDateTime).First()) |
| | 0 | 69 | | .ToDictionaryAsync(ph => ph.BitrixId, ph => ph); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public async Task<List<PriceHistory>> GetLastRecordsAsync(int count) |
| | | 73 | | { |
| | 0 | 74 | | return await _context.PriceHistories |
| | 0 | 75 | | .OrderByDescending(ph => ph.CreateDateTime) |
| | 0 | 76 | | .Take(count) |
| | 0 | 77 | | .ToListAsync(); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task<int> Update(PriceHistory value) |
| | | 81 | | { |
| | 0 | 82 | | _context.PriceHistories.Update(value); |
| | 0 | 83 | | return await _context.SaveChangesAsync(); |
| | 0 | 84 | | } |
| | | 85 | | } |