home/skymarketplace/public_html/controllers/CartController.php 0000644 00000002752 15003677744 0021054 0 ustar 00 cartModel = $cartModel ?? new CartModel();
}
public function addToCart($userId, $data): void
{
if (!isset($data['product_id'], $data['quantity'])) {
JsonResponse::send(['error' => 'Missing required fields'], 400);
return;
}
$success = $this->cartModel->add($userId, $data['product_id'], $data['quantity']);
if ($success) {
JsonResponse::send(['success' => 'Product added to cart']);
} else {
JsonResponse::send(['error' => 'Failed to add to cart'], 500);
}
}
public function removeFromCart($userId, $itemId): void
{
$success = $this->cartModel->remove($userId, $itemId);
if ($success) {
JsonResponse::send(['success' => 'Item removed from cart']);
} else {
JsonResponse::send(['error' => 'Failed to remove item'], 500);
}
}
public function getCart($userId): void
{
$items = $this->cartModel->getItems($userId);
JsonResponse::send(['cart' => $items]);
}
public function clearCart($userId): void
{
$this->cartModel->clear($userId);
JsonResponse::send(['success' => 'Cart cleared']);
}
}