Skip to content

Commit

Permalink
feat(api): add new route for ligther wishlists listings (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxime Bruchet <[email protected]>
  • Loading branch information
mabruchet and Maxime Bruchet authored Dec 11, 2024
1 parent 4f23d15 commit ec3e2b8
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.2.1</version>
<version>2.3.0</version>
<authors>
<author>
<name>Michaël Espeche</name>
Expand Down
50 changes: 45 additions & 5 deletions Controller/Front/Api/WishListController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*************************************************************************************/
/* */
/* Thelia */
Expand All @@ -20,6 +21,7 @@
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/

namespace WishList\Controller\Front\Api;

use OpenApi\Annotations as OA;
Expand Down Expand Up @@ -101,13 +103,47 @@ public function getWishListByCode(string $code, ModelFactory $modelFactory)
{
$wishList = WishListQuery::create()->filterByCode($code)->findOne();

if (null === $wishList){
return new JsonResponse([],Response::HTTP_NOT_FOUND);
if (null === $wishList) {
return new JsonResponse([], Response::HTTP_NOT_FOUND);
}

return OpenApiService::jsonResponse($modelFactory->buildModel('WishList', $wishList));
}

/**
* @Route("/lite/all", name="list_all_without_products", methods="GET")
* @OA\Get(
* path="/wishlist/lite/all",
* tags={"WishList"},
* summary="Get all wishlist of a customer without products",
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(
* type="array",
* @OA\Items(
* type="object",
* @OA\Property(
* property="wishList",
* ref="#/components/schemas/WishList"
* )
* )
* )
* )
* )
*/
public function getLiteWishLists(WishListService $wishListService, ModelFactory $modelFactory)
{
$wishLists = $wishListService->getAllWishLists();

$openApiWishLists = [];
foreach ($wishLists as $wishList) {
$openApiWishLists[] = $this->getOpenApiWishList($wishList->getId(), $modelFactory, $wishListService, true);
}

return OpenApiService::jsonResponse($openApiWishLists);
}

/**
* @Route("/all", name="list_all", methods="GET")
* @OA\Get(
Expand Down Expand Up @@ -135,7 +171,7 @@ public function getWishLists(WishListService $wishListService, ModelFactory $mod
$wishLists = $wishListService->getAllWishLists();

$openApiWishLists = [];
foreach ($wishLists as $wishList){
foreach ($wishLists as $wishList) {
$openApiWishLists[] = $this->getOpenApiWishList($wishList->getId(), $modelFactory, $wishListService);
}

Expand Down Expand Up @@ -666,14 +702,18 @@ public function createCartFromWishlist($wishListId, WishListService $wishListSer
return new JsonResponse();
}

protected function getOpenApiWishList($wishListId, ModelFactory $modelFactory, WishListService $wishListService)
protected function getOpenApiWishList($wishListId, ModelFactory $modelFactory, WishListService $wishListService, $lite = false)
{
$wishList = $wishListService->getWishList($wishListId);

if (empty($wishList)){
if (empty($wishList)) {
return null;
}

if ($lite) {
return $modelFactory->buildModel('WishListLite', $wishList);
}

return $modelFactory->buildModel('WishList', $wishList);
}
}
239 changes: 239 additions & 0 deletions Model/Api/WishListLite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<?php

namespace WishList\Model\Api;

use OpenApi\Annotations as OA;
use OpenApi\Model\Api\BaseApiModel;

/**
* Class WishList.
*
* @OA\Schema(
* description="WishList"
* )
*/
class WishListLite extends BaseApiModel
{
/**
* @var int
* @OA\Property(
* type="integer",
* )
* @Constraint\NotBlank(groups={"read", "update"})
*/
protected $id;

/**
* @var boolean
* @OA\Property(
* type="boolean",
* )
* @Constraint\NotBlank(groups={"read", "update"})
*/
protected $default;

/**
* @var boolean
* @OA\Property(
* type="boolean",
* )
* @Constraint\NotBlank(groups={"read", "update"})
*/
protected $isType;

/**
* @var integer
* @OA\Property(
* type="number",
* format="integer",
* )
* @Constraint\NotBlank(groups={"create", "update"})
*/
protected $customerId;

/**
* @var string
* @OA\Property(
* type="string",
* )
*/
protected $sessionId;

/**
* @var string
* @OA\Property(
* type="string",
* )
*/
protected $title;

/**
* @var string
* @OA\Property(
* type="string",
* )
*/
protected $code;

/**
* @var string
* @OA\Property(
* type="string",
* )
*/
protected $sharedUrl;

/**
* @var \WishList\Model\WishList $theliaModel
*/
public function createFromTheliaModel($theliaModel, $locale = null): void
{
parent::createFromTheliaModel($theliaModel, $locale);

$this->setSharedUrl($theliaModel->getUrl($locale));
}

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* @param int $id
* @return WishListLite
*/
public function setId($id): WishListLite
{
$this->id = $id;
return $this;
}

/**
* @return boolean
*/
public function getDefault()
{
return $this->default;
}

/**
* @param boolean $default
* @return WishListLite
*/
public function setDefault($default): WishListLite
{
$this->default = $default;
return $this;
}

/**
* @return boolean
*/
public function getIsType()
{
return $this->isType;
}

/**
* @param boolean $isType
* @return WishListLite
*/
public function setIsType($isType): WishListLite
{
$this->isType = $isType;
return $this;
}

/**
* @return int
*/
public function getCustomerId()
{
return $this->customerId;
}

/**
* @param int $customerId
* @return WishListLite
*/
public function setCustomerId($customerId): WishListLite
{
$this->customerId = $customerId;
return $this;
}

/**
* @return string
*/
public function getTitle()
{
return $this->title;
}

/**
* @param string $title
* @return WishListLite
*/
public function setTitle($title): WishListLite
{
$this->title = $title;
return $this;
}

/**
* @return string
*/
public function getCode()
{
return $this->code;
}

/**
* @param string $title
* @return WishListLite
*/
public function setCode($code): WishListLite
{
$this->code = $code;
return $this;
}

/**
* @return string
*/
public function getSharedUrl()
{
return $this->sharedUrl;
}

/**
* @param string $sharedUrl
* @return WishListLite
*/
public function setSharedUrl($sharedUrl): WishListLite
{
$this->sharedUrl = $sharedUrl;
return $this;
}

/**
* @return string
*/
public function getSessionId()
{
return $this->sessionId;
}

/**
* @param string $sessionId
* @return WishListLite
*/
public function setSessionId($sessionId): WishListLite
{
$this->sessionId = $sessionId;
return $this;
}
}

0 comments on commit ec3e2b8

Please sign in to comment.