/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Repositories/CartRepository.php
184 строки
4 KB
vivanenko
fix taps and others structure
27 май 2025, 18:06
27 май 2025, 18:06
8cdc7d9
Код
Авторство
О чём код?
<?php namespace App\Repositories; use App\Contracts\CartRepositoryInterface; use App\Models\Cart; use App\Models\CartItem; use App\Models\Tariff; use Illuminate\Database\Eloquent\Collection; class CartRepository implements CartRepositoryInterface { /** * Get or create cart for user */ public function getOrCreateCart(int $userId): Cart { return Cart::firstOrCreate(['user_id' => $userId]); } /** * Get cart by user ID */ public function getCartByUserId(int $userId): ?Cart { return Cart::where('user_id', $userId)->with('items.itemable')->first(); } /** * Add tariff to cart */ public function addTariffToCart(int $userId, Tariff $tariff, int $quantity = 1): CartItem { $cart = $this->getOrCreateCart($userId); // Check if tariff already exists in cart $existingItem = $this->getCartItem($userId, $tariff->id); if ($existingItem) { // If exists, increase quantity $existingItem->increment('quantity', $quantity); return $existingItem->fresh(); } // Create new cart item $sku = CartItem::generateSku(Tariff::class, $tariff->id); $cartItem = CartItem::create([ 'user_id' => $userId, 'cart_id' => $cart->id, 'sku' => $sku, 'price' => $tariff->getPrice(), 'quantity' => $quantity, 'itemable_type' => Tariff::class, 'itemable_id' => $tariff->id, 'currency' => 'RUB', ]); return $cartItem; } /** * Remove tariff from cart */ public function removeTariffFromCart(int $userId, int $tariffId): bool { $cartItem = $this->getCartItem($userId, $tariffId); if (!$cartItem) { return false; } return $cartItem->delete(); } /** * Update tariff quantity in cart */ public function updateTariffQuantity(int $userId, int $tariffId, int $quantity): bool { $cartItem = $this->getCartItem($userId, $tariffId); if (!$cartItem) { return false; } if ($quantity <= 0) { return $cartItem->delete(); } return $cartItem->update(['quantity' => $quantity]); } /** * Get cart items for user */ public function getCartItems(int $userId): Collection { $cart = $this->getCartByUserId($userId); if (!$cart) { return new Collection(); } return $cart->items; } /** * Calculate total cart price (with discounts) */ public function calculateCartTotal(int $userId): float { $cart = $this->getCartByUserId($userId); if (!$cart) { return 0.0; } $totals = $cart->getTotalWithDiscounts(); return $totals['total']; } /** * Calculate cart subtotal (without discounts) */ public function calculateCartSubtotal(int $userId): float { $cart = $this->getCartByUserId($userId); if (!$cart) { return 0.0; } return $cart->getTotalAmount(); } /** * Clear cart for user */ public function clearCart(int $userId): bool { $cart = $this->getCartByUserId($userId); if (!$cart) { return true; } return $cart->clear(); } /** * Get cart item by user and tariff */ public function getCartItem(int $userId, int $tariffId): ?CartItem { return CartItem::where('user_id', $userId) ->where('itemable_type', Tariff::class) ->where('itemable_id', $tariffId) ->whereNotNull('cart_id') ->first(); } /** * Check if tariff exists in cart */ public function hasTariffInCart(int $userId, int $tariffId): bool { return $this->getCartItem($userId, $tariffId) !== null; } /** * Get cart items count */ public function getCartItemsCount(int $userId): int { $cart = $this->getCartByUserId($userId); if (!$cart) { return 0; } return $cart->getItemsCount(); } }