| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonDomains.Models.OrderCarts; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace OzonRepositories.Data |
| | | 7 | | { |
| | | 8 | | public class OrderCartRepository : MainRepository, IRepository<OrderCart> |
| | | 9 | | { |
| | | 10 | | private readonly OrderRepository _orderRepository; |
| | | 11 | | |
| | 0 | 12 | | public OrderCartRepository(ApplicationDbContext orderContext, OrderRepository orderRepository) : base(orderConte |
| | | 13 | | { |
| | 0 | 14 | | _orderRepository = orderRepository; |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task<int> Add(OrderCart cart) |
| | | 18 | | { |
| | 0 | 19 | | if (cart.CartStatus != null) |
| | | 20 | | { |
| | 0 | 21 | | var existingCartStatus = await _context.CartStatuses |
| | 0 | 22 | | .FirstOrDefaultAsync(cs => cs.Name == cart.CartStatus.Name); |
| | | 23 | | |
| | 0 | 24 | | if (existingCartStatus == null) |
| | | 25 | | { |
| | 0 | 26 | | _context.CartStatuses.Add(cart.CartStatus); |
| | | 27 | | } |
| | | 28 | | else |
| | | 29 | | { |
| | 0 | 30 | | cart.CartStatus = existingCartStatus; |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | foreach (var item in cart.OrderItems) |
| | | 35 | | { |
| | 0 | 36 | | if (item.ItemStatus != null) |
| | | 37 | | { |
| | 0 | 38 | | var existingItemStatus = await _context.ItemStatuses |
| | 0 | 39 | | .FirstOrDefaultAsync(ist => ist.Name == item.ItemStatus.Name); |
| | | 40 | | |
| | 0 | 41 | | if (existingItemStatus == null) |
| | | 42 | | { |
| | 0 | 43 | | _context.ItemStatuses.Add(item.ItemStatus); |
| | | 44 | | } |
| | | 45 | | else |
| | | 46 | | { |
| | 0 | 47 | | item.ItemStatus = existingItemStatus; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | item.OrderCart = cart; |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | _context.OrderCarts.Add(cart); |
| | 0 | 55 | | return await _context.SaveChangesAsync(); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task<int> Delete(OrderCart cart) |
| | | 59 | | { |
| | 0 | 60 | | _context.OrderCarts.Remove(cart); |
| | 0 | 61 | | return await _context.SaveChangesAsync(); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<int> DeleteAsync(int id) |
| | | 65 | | { |
| | 0 | 66 | | var cart = await _context.OrderCarts |
| | 0 | 67 | | .Include(c => c.OrderItems) |
| | 0 | 68 | | .ThenInclude(oi => oi.StudioOrder) |
| | 0 | 69 | | .FirstOrDefaultAsync(c => c.Id == id); |
| | | 70 | | |
| | 0 | 71 | | if (cart == null) |
| | 0 | 72 | | return 0; |
| | | 73 | | |
| | 0 | 74 | | _context.OrderCarts.Remove(cart); |
| | 0 | 75 | | return await _context.SaveChangesAsync(); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public async Task<int> DeleteByOrder(int orderId) |
| | | 79 | | { |
| | 0 | 80 | | var cartsToDelete = await _context.OrderItems.Where(c => c.StudioOrderId == orderId).ToListAsync(); |
| | 0 | 81 | | _context.OrderItems.RemoveRange(cartsToDelete); |
| | 0 | 82 | | return await _context.SaveChangesAsync(); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Удаляет элементы корзины для нескольких заказов одним запросом (ускорение страницы транзакций). |
| | | 87 | | /// </summary> |
| | | 88 | | public async Task<int> DeleteByOrderIds(int[] orderIds) |
| | | 89 | | { |
| | 0 | 90 | | if (orderIds == null || orderIds.Length == 0) |
| | 0 | 91 | | return 0; |
| | | 92 | | |
| | 0 | 93 | | var distinctIds = orderIds.Distinct().ToArray(); |
| | 0 | 94 | | var itemsToDelete = await _context.OrderItems |
| | 0 | 95 | | .Where(oi => oi.StudioOrderId != null && distinctIds.Contains(oi.StudioOrderId.Value)) |
| | 0 | 96 | | .ToListAsync(); |
| | 0 | 97 | | if (itemsToDelete.Count == 0) |
| | 0 | 98 | | return 0; |
| | 0 | 99 | | _context.OrderItems.RemoveRange(itemsToDelete); |
| | 0 | 100 | | return await _context.SaveChangesAsync(); |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public async Task<List<OrderCart>> Get() |
| | | 104 | | { |
| | 0 | 105 | | var carts = await _context.OrderCarts |
| | 0 | 106 | | .Include(c => c.CartStatus) |
| | 0 | 107 | | .Include(c => c.OrderItems) |
| | 0 | 108 | | .ThenInclude(oi => oi.ItemStatus) |
| | 0 | 109 | | .ToListAsync(); |
| | | 110 | | |
| | | 111 | | // Удаляем carts без OrderItems |
| | 0 | 112 | | var emptyCarts = carts.Where(c => c.OrderItems == null || !c.OrderItems.Any()).ToList(); |
| | 0 | 113 | | if (emptyCarts.Any()) |
| | | 114 | | { |
| | 0 | 115 | | _context.OrderCarts.RemoveRange(emptyCarts); |
| | 0 | 116 | | await _context.SaveChangesAsync(); |
| | | 117 | | |
| | | 118 | | // Удаляем их из списка перед возвратом |
| | 0 | 119 | | carts = carts.Except(emptyCarts).ToList(); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | foreach (var cart in carts) |
| | | 123 | | { |
| | 0 | 124 | | foreach (var item in cart.OrderItems) |
| | | 125 | | { |
| | 0 | 126 | | if (item.StudioOrderId.HasValue) |
| | 0 | 127 | | item.StudioOrder = await _orderRepository.GetAsync(item.StudioOrderId.Value); |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | return carts; |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | public async Task<OrderItem?> GetItemId(int itemId) |
| | | 135 | | { |
| | 0 | 136 | | return await _context.OrderItems |
| | 0 | 137 | | .FirstOrDefaultAsync(c => c.Id == itemId); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | public async Task<List<OrderCart>> GetWithPagination(int skip, int take) |
| | | 141 | | { |
| | 0 | 142 | | var carts = await _context.OrderCarts |
| | 0 | 143 | | .Include(c => c.CartStatus) |
| | 0 | 144 | | .Include(c => c.OrderItems) |
| | 0 | 145 | | .ThenInclude(oi => oi.ItemStatus) |
| | 0 | 146 | | .OrderByDescending(c => c.CreatedAt) |
| | 0 | 147 | | .Skip(skip) |
| | 0 | 148 | | .Take(take) |
| | 0 | 149 | | .ToListAsync(); |
| | | 150 | | |
| | 0 | 151 | | var emptyCarts = carts.Where(c => c.OrderItems == null || !c.OrderItems.Any()).ToList(); |
| | 0 | 152 | | if (emptyCarts.Any()) |
| | | 153 | | { |
| | 0 | 154 | | _context.OrderCarts.RemoveRange(emptyCarts); |
| | 0 | 155 | | await _context.SaveChangesAsync(); |
| | 0 | 156 | | carts = carts.Except(emptyCarts).ToList(); |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | foreach (var cart in carts) |
| | | 160 | | { |
| | 0 | 161 | | foreach (var item in cart.OrderItems) |
| | | 162 | | { |
| | 0 | 163 | | if (item.StudioOrderId.HasValue) |
| | 0 | 164 | | item.StudioOrder = await _orderRepository.GetAsyncForCart(item.StudioOrderId.Value); |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | return carts; |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | public async Task<OrderCart?> GetAsync(int id) |
| | | 172 | | { |
| | 0 | 173 | | return await _context.OrderCarts |
| | 0 | 174 | | .Include(c => c.CartStatus) |
| | 0 | 175 | | .Include(c => c.OrderItems) |
| | 0 | 176 | | .ThenInclude(oi => oi.ItemStatus) |
| | 0 | 177 | | .Include(c => c.OrderItems) |
| | 0 | 178 | | .ThenInclude(oi => oi.StudioOrder) |
| | 0 | 179 | | .FirstOrDefaultAsync(c => c.Id == id); |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | public async Task<OrderCart?> GetAsync(OrderCart value) |
| | | 183 | | { |
| | 0 | 184 | | return await _context.OrderCarts |
| | 0 | 185 | | .Include(c => c.CartStatus) |
| | 0 | 186 | | .Include(c => c.OrderItems) |
| | 0 | 187 | | .ThenInclude(oi => oi.ItemStatus) |
| | 0 | 188 | | .Include(c => c.OrderItems) |
| | 0 | 189 | | .ThenInclude(oi => oi.StudioOrder) |
| | 0 | 190 | | .FirstOrDefaultAsync(c => c.Id == value.Id); |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | public async Task<int> Update(OrderCart cart) |
| | | 194 | | { |
| | 0 | 195 | | _context.OrderCarts.Update(cart); |
| | 0 | 196 | | return await _context.SaveChangesAsync(); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | public async Task<int> GetTotalCount() |
| | | 200 | | { |
| | 0 | 201 | | return await _context.OrderCarts |
| | 0 | 202 | | .CountAsync(c => c.OrderItems.Any()); |
| | 0 | 203 | | } |
| | | 204 | | } |
| | | 205 | | } |