From 311886b4e4452a922a47f92fd830cc547a1975f2 Mon Sep 17 00:00:00 2001 From: Zahdi <ZahdiMohcine20@gmail.com> Date: Tue, 25 Mar 2025 19:04:39 +0100 Subject: [PATCH] [Progress] wishlist is done --- src/Entity/Wishlist.php | 2 +- src/Repository/WishlistRepository.php | 47 +++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Entity/Wishlist.php b/src/Entity/Wishlist.php index 97924ccb..9c16360e 100644 --- a/src/Entity/Wishlist.php +++ b/src/Entity/Wishlist.php @@ -125,7 +125,7 @@ class Wishlist public function getItemById(int $id){ $itemsArray = (($this->items->toArray())) ; - for ($i = 0; $i < count($itemsArray); $i++) { + for ($i = 0; $i < sizeof($itemsArray); $i++) { $item = $itemsArray[$i]; if ($item->getId() == $id ) { return $item; diff --git a/src/Repository/WishlistRepository.php b/src/Repository/WishlistRepository.php index 93ae0c34..25c14b1c 100644 --- a/src/Repository/WishlistRepository.php +++ b/src/Repository/WishlistRepository.php @@ -42,21 +42,62 @@ class WishlistRepository extends ServiceEntityRepository $wishlist->setDeadline($deadline); $this->getEntityManager()->flush(); } + return $wishlist; } public function mostExpensiveList() { $wishlists = $this->findAll() ; + $rankings = array(); + foreach ($wishlists as $wishlist) { + $total = $wishlist->wishlistTotalPrice() ; + + if (sizeof(($rankings)) < 3 ) { + $rankings[] = ['wishlist' => $wishlist, 'total' => $total]; + usort($rankings, callback: function($a, $b) {return $a['total'] - $b['total'];}); + } else { + for ($i = 0; $i < sizeof($rankings) ; $i++ ) { + if ($rankings[i]['total'] < $total ) { + $rankings[i] = ['wishlist' => $wishlist, 'total' => $total] ; + } + } - + } + + } + $result = array() ; + for ($i = 0 ; $i < sizeof($rankings) ; $i++ ) { + $result[] = $rankings[i]['wishlist'] ; + } + return $result; + } + + + public function mostExpensiveItems() { + $wishlists = $this->findAll() ; + $rankings = array(); foreach ($wishlists as $wishlist) { $items = $wishlist->getItems(); - } + foreach ($items as $item) { + if ($item->getPurchaseProof()) { + if (sizeof(($rankings)) < 3 ) { + $rankings[] = $item; + usort( $rankings, function($a, $b) {return $a->getPrice() - $b->getPrice();}); + } else { + for ($i = 0; $i < sizeof($rankings) ; $i++ ) { + if ($rankings[i]->getPrice() < $item->getPrice() ) { + $rankings[i] = $item ; + } + } + } + } + } + } + return $rankings; } - // /** // * @return Wishlist[] Returns an array of Wishlist objects // */ -- GitLab