LauraBokkers / educom-webshop-database-1697464342

0 stars 0 forks source link

Deze functie doet te veel #21

Open JeroenHeemskerk opened 11 months ago

JeroenHeemskerk commented 11 months ago

Onderstaande functie haalt niet alleen de betreffende productData op, https://github.com/LauraBokkers/educom-webshop-database-1697464342/blob/d9f14f90f4347963a0074901b58e56ec8665a467/product.php#L3-L34 maar voegt hem eventueel ook aan de winkelwagen toe, dit is een 'side-effect' wat een programmeur niet verwacht.

Doe alle afhandeling van POST verzoeken (of actions) in een eigen functie (in index.php of product-service.php) die je weer kan hergebruiken voor de case "webshop" en case "shoppingcart" in processRequest().

(gelinkt aan issue #18)

 function handleActions() {
    $action = getPostVar("action");
    switch ($action) {
       case "addToCart":
           $productId = getPostVar("productId");
           addToCart($productId);
           break;
       case "removeFromCart":
           /* ... */
       case "completeOrder":
           /* ... */
     }
 }
JeroenHeemskerk commented 11 months ago

Idem voor getShoppingcartData(): https://github.com/LauraBokkers/educom-webshop-database-1697464342/blob/d9f14f90f4347963a0074901b58e56ec8665a467/shoppingcart.php#L12-L60 Deze zou alleen de cart moeten opzoeken, de bijbehorende producten (uit de database zie issue #18) erbij zoeken en de (sub)totalen berekenen.

 function getShoppingcartData() 
 { 
     $pageData = ["page" => "shoppingcart", "cart" => [], "total" => 0]; 
     try { 
         $cart = getCart(); // Nieuwe function in sessionManager.php
         $products = getProductsFromDatabase(); // Zorg dat de array 'keys' de product Id's zijn

         foreach($cart as $productId => $amount) {
            $product = $products[$productId];
            $subTotal = $amount * $product['price'];
            $pageData['total'] += $subTotal;
            $pageData['cart'][] = ['name' => $product['name'], /* ... */ 'amount' => $amount, 'subTotal' => $subTotal ]; // of gebruik array_push($pageData['cart'], [ 'name' => ... ]);
         } 
         return $pageData;
     }
     catch (..)