| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace OzonRepositories.Data |
| | | 7 | | { |
| | | 8 | | public class StatusTransitionRepository : MainRepository, IRepository<StatusTransition> |
| | | 9 | | { |
| | 1 | 10 | | public StatusTransitionRepository(ApplicationDbContext orderContext) : base(orderContext) |
| | | 11 | | { |
| | 1 | 12 | | } |
| | | 13 | | |
| | | 14 | | public async Task<int> Add(StatusTransition value) |
| | | 15 | | { |
| | 0 | 16 | | value.CreatedAt = DateTime.UtcNow; |
| | 0 | 17 | | await _context.StatusTransitions.AddAsync(value); |
| | 0 | 18 | | return await _context.SaveChangesAsync(); |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<int> Delete(StatusTransition value) |
| | | 22 | | { |
| | 0 | 23 | | _context.StatusTransitions.Remove(value); |
| | 0 | 24 | | return await _context.SaveChangesAsync(); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<List<StatusTransition>> Get() |
| | | 28 | | { |
| | 0 | 29 | | return await _context.StatusTransitions |
| | 0 | 30 | | .Include(st => st.FromStatus!) |
| | 0 | 31 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 32 | | .Include(st => st.ToStatus!) |
| | 0 | 33 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 34 | | .OrderBy(st => st.DisplayOrder) |
| | 0 | 35 | | .ThenBy(st => st.Id) |
| | 0 | 36 | | .ToListAsync(); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<List<StatusTransition>> GetActive() |
| | | 40 | | { |
| | 1 | 41 | | return await _context.StatusTransitions |
| | 1 | 42 | | .Where(st => st.IsActive) |
| | 1 | 43 | | .Include(st => st.FromStatus!) |
| | 1 | 44 | | .ThenInclude(s => s!.StatusColor) |
| | 1 | 45 | | .Include(st => st.ToStatus!) |
| | 1 | 46 | | .ThenInclude(s => s!.StatusColor) |
| | 1 | 47 | | .OrderBy(st => st.DisplayOrder) |
| | 1 | 48 | | .ThenBy(st => st.Id) |
| | 1 | 49 | | .ToListAsync(); |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | public async Task<StatusTransition?> GetAsync(int id) |
| | | 53 | | { |
| | 0 | 54 | | return await _context.StatusTransitions |
| | 0 | 55 | | .Include(st => st.FromStatus!) |
| | 0 | 56 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 57 | | .Include(st => st.ToStatus!) |
| | 0 | 58 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 59 | | .FirstOrDefaultAsync(st => st.Id == id); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task<StatusTransition?> GetAsync(StatusTransition value) |
| | | 63 | | { |
| | 0 | 64 | | return (await _context.StatusTransitions |
| | 0 | 65 | | .Include(st => st.FromStatus!) |
| | 0 | 66 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 67 | | .Include(st => st.ToStatus!) |
| | 0 | 68 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 69 | | .FirstOrDefaultAsync(st => |
| | 0 | 70 | | st.FromStatusId == value.FromStatusId && |
| | 0 | 71 | | st.ToStatusId == value.ToStatusId && |
| | 0 | 72 | | st.TransactionType == value.TransactionType))!; |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task<List<StatusTransition>> GetByFromStatusId(int fromStatusId) |
| | | 76 | | { |
| | 0 | 77 | | return await _context.StatusTransitions |
| | 0 | 78 | | .Where(st => st.IsActive && st.FromStatusId == fromStatusId) |
| | 0 | 79 | | .Include(st => st.FromStatus!) |
| | 0 | 80 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 81 | | .Include(st => st.ToStatus!) |
| | 0 | 82 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 83 | | .OrderBy(st => st.DisplayOrder) |
| | 0 | 84 | | .ThenBy(st => st.Id) |
| | 0 | 85 | | .ToListAsync(); |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Получает активные переходы по типу транзакции. |
| | | 90 | | /// Используется для динамического определения допустимых исходных и целевых статусов. |
| | | 91 | | /// </summary> |
| | | 92 | | public async Task<List<StatusTransition>> GetByTransactionType(TransactionType transactionType) |
| | | 93 | | { |
| | 0 | 94 | | return await _context.StatusTransitions |
| | 0 | 95 | | .Where(st => st.IsActive && st.TransactionType == transactionType) |
| | 0 | 96 | | .Include(st => st.FromStatus!) |
| | 0 | 97 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 98 | | .Include(st => st.ToStatus!) |
| | 0 | 99 | | .ThenInclude(s => s!.StatusColor) |
| | 0 | 100 | | .OrderBy(st => st.DisplayOrder) |
| | 0 | 101 | | .ThenBy(st => st.Id) |
| | 0 | 102 | | .ToListAsync(); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | public async Task<int> Update(StatusTransition value) |
| | | 106 | | { |
| | 0 | 107 | | value.UpdatedAt = DateTime.UtcNow; |
| | 0 | 108 | | _context.StatusTransitions.Update(value); |
| | 0 | 109 | | return await _context.SaveChangesAsync(); |
| | 0 | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |