Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
86 / 86 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
ProductController | |
100.00% |
84 / 84 |
|
100.00% |
8 / 8 |
18 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLatestProducts | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
addToCart | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
3 | |||
getAllProducts | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
getCartItems | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
updateCartQuantity | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
deleteCartItem | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
deleteAllCartItems | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace Controllers; |
3 | |
4 | require_once __DIR__ . '/../Models/Cart.php'; |
5 | require_once __DIR__ . '/../Models/Product.php'; |
6 | |
7 | use Models\Cart; |
8 | use Models\Product; |
9 | |
10 | class ProductController { |
11 | private $conn; |
12 | |
13 | public function __construct($conn) { |
14 | $this->conn = $conn; |
15 | } |
16 | |
17 | public function getLatestProducts($limit = 6) { |
18 | try { |
19 | $query = "SELECT * FROM products LIMIT :limit"; |
20 | $stmt = $this->conn->prepare($query); |
21 | $stmt->bindValue(':limit', $limit, \PDO::PARAM_INT); |
22 | $stmt->execute(); |
23 | |
24 | $products = []; |
25 | foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
26 | $product = new Product(); |
27 | $product->setId($row['id']); |
28 | $product->setName($row['name']); |
29 | $product->setPrice($row['price']); |
30 | $product->setImage($row['image']); |
31 | $products[] = $product; |
32 | } |
33 | return $products; |
34 | } catch (\Exception $e) { |
35 | error_log("Error al obtener productos: " . $e->getMessage()); |
36 | return []; |
37 | } |
38 | } |
39 | |
40 | public function addToCart($userId, $productData) { |
41 | try { |
42 | $cart = new Cart(); |
43 | $cart->setUserId($userId); |
44 | $cart->setName($productData['product_name']); |
45 | $cart->setPrice($productData['product_price']); |
46 | $cart->setQuantity($productData['product_quantity']); |
47 | $cart->setImage($productData['product_image']); |
48 | |
49 | // Verificar si el producto ya está en el carrito |
50 | $stmt = $this->conn->prepare("SELECT * FROM cart WHERE user_id = ? AND name = ?"); |
51 | $stmt->execute([$userId, $cart->getName()]); |
52 | |
53 | if($stmt->rowCount() > 0) { |
54 | return ['success' => false, 'message' => 'El producto ya está en el carrito']; |
55 | } |
56 | |
57 | // Añadir al carrito |
58 | $query = "INSERT INTO cart (user_id, name, price, quantity, image) |
59 | VALUES (?, ?, ?, ?, ?)"; |
60 | $stmt = $this->conn->prepare($query); |
61 | |
62 | $stmt->execute([ |
63 | $cart->getUserId(), |
64 | $cart->getName(), |
65 | $cart->getPrice(), |
66 | $cart->getQuantity(), |
67 | $cart->getImage() |
68 | ]); |
69 | |
70 | return ['success' => true, 'message' => 'Producto añadido al carrito']; |
71 | } catch (\Exception $e) { |
72 | error_log("Error al añadir al carrito: " . $e->getMessage()); |
73 | return ['success' => false, 'message' => 'Error al añadir al carrito']; |
74 | } |
75 | } |
76 | |
77 | public function getAllProducts() { |
78 | try { |
79 | $query = "SELECT * FROM products"; |
80 | $stmt = $this->conn->prepare($query); |
81 | $stmt->execute(); |
82 | |
83 | $products = []; |
84 | foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
85 | $product = new Product(); |
86 | $product->setId($row['id']); |
87 | $product->setName($row['name']); |
88 | $product->setPrice($row['price']); |
89 | $product->setImage($row['image']); |
90 | $products[] = $product; |
91 | } |
92 | return $products; |
93 | } catch (\Exception $e) { |
94 | error_log("Error al obtener todos los productos: " . $e->getMessage()); |
95 | return []; |
96 | } |
97 | } |
98 | |
99 | public function getCartItems($userId) { |
100 | try { |
101 | $query = "SELECT * FROM cart WHERE user_id = ?"; |
102 | $stmt = $this->conn->prepare($query); |
103 | $stmt->execute([$userId]); |
104 | return $stmt->fetchAll(\PDO::FETCH_ASSOC); |
105 | } catch (\Exception $e) { |
106 | error_log("Error al obtener items del carrito: " . $e->getMessage()); |
107 | return []; |
108 | } |
109 | } |
110 | |
111 | public function updateCartQuantity($cartId, $quantity) { |
112 | try { |
113 | $query = "UPDATE cart SET quantity = ? WHERE id = ?"; |
114 | $stmt = $this->conn->prepare($query); |
115 | $stmt->execute([$quantity, $cartId]); |
116 | return ['success' => true, 'message' => '¡Cantidad actualizada!']; |
117 | } catch (\Exception $e) { |
118 | error_log("Error al actualizar cantidad: " . $e->getMessage()); |
119 | return ['success' => false, 'message' => 'Error al actualizar cantidad']; |
120 | } |
121 | } |
122 | |
123 | public function deleteCartItem($cartId) { |
124 | try { |
125 | $query = "DELETE FROM cart WHERE id = ?"; |
126 | $stmt = $this->conn->prepare($query); |
127 | $stmt->execute([$cartId]); |
128 | return true; |
129 | } catch (\Exception $e) { |
130 | error_log("Error al eliminar item: " . $e->getMessage()); |
131 | return false; |
132 | } |
133 | } |
134 | |
135 | public function deleteAllCartItems($userId) { |
136 | try { |
137 | $query = "DELETE FROM cart WHERE user_id = ?"; |
138 | $stmt = $this->conn->prepare($query); |
139 | $stmt->execute([$userId]); |
140 | return true; |
141 | } catch (\Exception $e) { |
142 | error_log("Error al eliminar todos los items: " . $e->getMessage()); |
143 | return false; |
144 | } |
145 | } |
146 | } |