Skip to content

Commit 57c492f

Browse files
committed
GET /shops/me/lists
1 parent a6ac49c commit 57c492f

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

src/controller/ShoppingListController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class ShoppingListController extends BaseController {
2424
public function __construct(ShoppingListDao $shoppingListDao, ProductDao $productDao) {
2525
$this->shoppingListDao = $shoppingListDao;
2626
$this->productDao = $productDao;
27-
$this->registerRoute('/users/me/lists', 'GET', '*', 'getMyLists');
27+
$this->registerRoute('/users/me/lists', 'GET', 'USER', 'getMyLists');
28+
$this->registerRoute('/shops/me/lists', 'GET', 'OWNER', 'getMyShopLists');
2829
$this->registerRoute('/lists', 'POST', 'USER', 'addList');
2930
$this->registerRoute('/lists/:id', 'DELETE', '*', 'deleteList');
3031
$this->registerRoute('/lists/:id', 'POST', 'OWNER', 'prepareList');
@@ -42,6 +43,18 @@ public function getMyLists() {
4243
}, $lists);
4344
}
4445

46+
/**
47+
* Get the lists of the current shop owner
48+
* @return ShoppingList[]
49+
*/
50+
public function getMyShopLists() {
51+
$shopId = AuthService::getAuthContext()['shopId'];
52+
$lists = $this->shoppingListDao->getListsByShopId($shopId);
53+
return array_map(function ($list) {
54+
return new ShoppingList($list);
55+
}, $lists);
56+
}
57+
4558
/**
4659
* Add a new list
4760
* @param NewShoppingList $newShoppingList

src/dao/ShoppingListDao.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ class ShoppingListDao extends Dao {
2525
*/
2626
public function getListsByUserId(int $userId) {
2727
$results = $this->query("SELECT * FROM ShoppingListDetail WHERE userId = ?", [$userId]);
28-
// Group by list ID
29-
$lists = [];
30-
foreach ($results as $result) {
31-
$index = $result['shoppingListId'];
32-
$list = @$lists[$index] ?? [];
33-
array_push($list, $result);
34-
$lists[$index] = $list;
35-
}
36-
return array_values($lists);
28+
return $this->groupResultByListId($results);
29+
}
30+
31+
/**
32+
* Get an array of ShoppingListDetail grouped by list ID
33+
* @param int $shopId
34+
* @return array
35+
*/
36+
public function getListsByShopId(int $shopId) {
37+
$results = $this->query("SELECT * FROM ShoppingListDetail WHERE shopId = ?", [$shopId]);
38+
return $this->groupResultByListId($results);
3739
}
3840

3941
/**
@@ -81,4 +83,15 @@ public function prepareShoppingList(int $id) {
8183
public function getListById(int $id) {
8284
return $this->query("SELECT * FROM ShoppingListDetail WHERE shoppingListId = ?", [$id]);
8385
}
86+
87+
private function groupResultByListId(array $results): array {
88+
$lists = [];
89+
foreach ($results as $result) {
90+
$index = $result['shoppingListId'];
91+
$list = @$lists[$index] ?? [];
92+
array_push($list, $result);
93+
$lists[$index] = $list;
94+
}
95+
return array_values($lists);
96+
}
8497
}

0 commit comments

Comments
 (0)