| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace OzonRepositories.Data; |
| | | 6 | | |
| | | 7 | | public class BitrixStockAutoScheduleRepository : IBitrixStockAutoScheduleRepository |
| | | 8 | | { |
| | | 9 | | private readonly ApplicationDbContext _context; |
| | | 10 | | |
| | 0 | 11 | | public BitrixStockAutoScheduleRepository(ApplicationDbContext context) |
| | | 12 | | { |
| | 0 | 13 | | _context = context; |
| | 0 | 14 | | } |
| | | 15 | | |
| | | 16 | | public async Task<List<BitrixStockAutoSchedule>> GetAllAsync(bool includeClient = false, |
| | | 17 | | CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 0 | 19 | | var q = _context.BitrixStockAutoSchedules.AsQueryable(); |
| | 0 | 20 | | if (includeClient) |
| | | 21 | | { |
| | 0 | 22 | | q = q.Include(s => s.OzonClient); |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | return await q.OrderByDescending(s => s.CreatedAt).ToListAsync(cancellationToken); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public Task<BitrixStockAutoSchedule?> GetByIdAsync(int id, CancellationToken cancellationToken = default) => |
| | 0 | 29 | | _context.BitrixStockAutoSchedules.Include(s => s.OzonClient).FirstOrDefaultAsync(s => s.Id == id, cancellationTo |
| | | 30 | | |
| | | 31 | | public async Task<int> AddAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = default) |
| | | 32 | | { |
| | 0 | 33 | | schedule.CreatedAt = DateTime.UtcNow; |
| | 0 | 34 | | schedule.UpdatedAt = DateTime.UtcNow; |
| | 0 | 35 | | _context.BitrixStockAutoSchedules.Add(schedule); |
| | 0 | 36 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 37 | | return schedule.Id; |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task UpdateAsync(BitrixStockAutoSchedule schedule, CancellationToken cancellationToken = default) |
| | | 41 | | { |
| | 0 | 42 | | schedule.UpdatedAt = DateTime.UtcNow; |
| | 0 | 43 | | _context.BitrixStockAutoSchedules.Update(schedule); |
| | 0 | 44 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task DeleteAsync(int id, CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 0 | 49 | | var entity = await _context.BitrixStockAutoSchedules.FindAsync(new object[] { id }, cancellationToken); |
| | 0 | 50 | | if (entity != null) |
| | | 51 | | { |
| | 0 | 52 | | _context.BitrixStockAutoSchedules.Remove(entity); |
| | 0 | 53 | | await _context.SaveChangesAsync(cancellationToken); |
| | | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<BitrixStockAutoScheduleRun> AddRunAsync(BitrixStockAutoScheduleRun run, |
| | | 58 | | CancellationToken cancellationToken = default) |
| | | 59 | | { |
| | 0 | 60 | | _context.BitrixStockAutoScheduleRuns.Add(run); |
| | 0 | 61 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 62 | | return run; |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task UpdateRunAsync(BitrixStockAutoScheduleRun run, CancellationToken cancellationToken = default) |
| | | 66 | | { |
| | 0 | 67 | | _context.BitrixStockAutoScheduleRuns.Update(run); |
| | 0 | 68 | | await _context.SaveChangesAsync(cancellationToken); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | public async Task<List<BitrixStockAutoScheduleRun>> GetRunsAsync(int scheduleId, int take = 20, |
| | | 72 | | CancellationToken cancellationToken = default) => |
| | 0 | 73 | | await _context.BitrixStockAutoScheduleRuns.AsNoTracking() |
| | 0 | 74 | | .Where(r => r.ScheduleId == scheduleId) |
| | 0 | 75 | | .OrderByDescending(r => r.StartedAt) |
| | 0 | 76 | | .Take(take) |
| | 0 | 77 | | .ToListAsync(cancellationToken); |
| | | 78 | | } |