Skip to content

Commit

Permalink
Merge pull request #7 from thelia-modules/feat/next-reviews
Browse files Browse the repository at this point in the history
feat(next-reviews): updated front controller + template
  • Loading branch information
Lucanis authored Feb 26, 2024
2 parents ad43c57 + f926191 commit 5444e0b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 74 deletions.
141 changes: 81 additions & 60 deletions Controller/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,91 @@
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Response;

#[Route(path: "/guaranteed_opinion", name: "guaranteed_opinion")]
class FrontController extends BaseFrontController
{
/**
* @throws PropelException
*/
#[Route(path: "/site_reviews/offset/{offset}/limit/{limit}", name: "site_reviews", methods: "GET")]
public function siteReviews(int $offset, int $limit): JsonResponse
{
$reviews = [];

$siteReviews = GuaranteedOpinionSiteReviewQuery::create()
->setLimit($limit)
->setOffset($offset)
->find()
;

foreach ($siteReviews as $review) {
$reviews[] = [
'rate' => $review->getRate(),
'name' => $review->getName(),
'date' => $review->getReviewDate()?->format('Y-m-d'),
'message' => $review->getReview()
];
}

return new JsonResponse([
'total' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_RATING_TOTAL_CONFIG_KEY),
'average' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_RATING_AVERAGE_CONFIG_KEY),
'reviews' => $reviews
]);
/**
* @throws PropelException
*/
#[Route(path: "/site_reviews/offset/{offset}/limit/{limit}", name: "site_reviews", methods: "GET")]
public function siteReviews(int $offset, int $limit, Request $request): JsonResponse
{
$reviews = [];

$siteReviews = GuaranteedOpinionSiteReviewQuery::create()
->setLimit($limit)
->setOffset($offset)
->find();

foreach ($siteReviews as $review) {
$reviews[] = [
'rate' => $review->getRate(),
'name' => $review->getName(),
'date' => $review->getReviewDate()?->format('Y-m-d'),
'message' => $review->getReview()
];
}

/**
* @throws PropelException
*/
#[Route(path: "/product_reviews/{id}/offset/{offset}/limit/{limit}", name: "product_reviews", methods: "GET")]
public function productReviews(int $id, int $offset, int $limit): JsonResponse
{
$reviews = [];

$productRating = GuaranteedOpinionProductRatingQuery::create()
->findOneByProductId($id);

$productReviews = GuaranteedOpinionProductReviewQuery::create()
->filterByProductId($id)
->setLimit($limit)
->setOffset($offset)
->find();

foreach ($productReviews as $review) {
$reviews[] = [
'rate' => $review->getRate(),
'name' => $review->getName(),
'date' => $review->getReviewDate()?->format('Y-m-d'),
'message' => $review->getReview()
];
}

return new JsonResponse([
'total' => $productRating?->getTotal(),
'average' => $productRating?->getAverage(),
'reviews' => $reviews
]);
$responseData = [
'total' => $productRating?->getTotal(),
'average' => $productRating?->getAverage(),
'reviews' => $reviews
];

if ($request->headers->get('Accept') === 'text/html') {
$response = $this->render('includes/next-site-reviews', $responseData, count($reviews) > 0 ? Response::HTTP_OK : Response::HTTP_NO_CONTENT);

$response->headers->set('X-Remaining-Reviews', $responseData["total"] - $offset - $limit);

return $response;
}
}

return new JsonResponse($responseData);
}

/**
* @throws PropelException
*/
#[Route(path: "/product_reviews/{id}/offset/{offset}/limit/{limit}", name: "product_reviews", methods: "GET")]
public function productReviews(int $id, int $offset, int $limit, Request $request): JsonResponse|Response
{
$reviews = [];

$productRating = GuaranteedOpinionProductRatingQuery::create()
->findOneByProductId($id);

$productReviews = GuaranteedOpinionProductReviewQuery::create()
->filterByProductId($id)
->setLimit($limit)
->setOffset($offset)
->find();

foreach ($productReviews as $review) {
$reviews[] = [
'rate' => $review->getRate(),
'name' => $review->getName(),
'date' => $review->getReviewDate()?->format('Y-m-d'),
'message' => $review->getReview()
];
}

$responseData = [
'total' => $productRating?->getTotal(),
'average' => $productRating?->getAverage(),
'reviews' => $reviews
];

if ($request->headers->get('Accept') === 'text/html') {
$response = $this->render('includes/next-product-reviews', $responseData, count($reviews) > 0 ? Response::HTTP_OK : Response::HTTP_NO_CONTENT);

$response->headers->set('X-Remaining-Reviews', $responseData["total"] - $offset - $limit);

return $response;
}

return new JsonResponse($responseData);
}
}
13 changes: 6 additions & 7 deletions Loop/GuaranteedProductLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public function parseResults(LoopResult $loopResult): LoopResult
->set('ORDER_ID', $review->getOrderId())
->set('ORDER_DATE', $review->getOrderDate()?->format('Y-m-d'))
->set('REPLY', $review->getReply())
->set('REPLY_DATE', $review->getReplyDate()?->format('Y-m-d'))
;
->set('REPLY_DATE', $review->getReplyDate()?->format('Y-m-d'));
$this->addOutputFields($loopResultRow, $review);

$loopResult->addRow($loopResultRow);
Expand All @@ -56,8 +55,8 @@ public function buildModelCriteria(): GuaranteedOpinionProductReviewQuery|ModelC
$search->filterByRate($minRate, Criteria::GREATER_EQUAL);
}

if ((null !== $page = $this->getPage()) && (null !== $limit = $this->getLimit())) {
$search->paginate($page, $limit);
if ((null !== $offset = $this->getOffset()) && (null !== $limit = $this->getLimit())) {
$search->setOffset($offset)->setLimit($limit);
}

return $search;
Expand All @@ -68,8 +67,8 @@ protected function getArgDefinitions(): ArgumentCollection
return new ArgumentCollection(
Argument::createIntTypeArgument('product'),
Argument::createIntTypeArgument('min_rate'),
Argument::createIntTypeArgument('limit', 5),
Argument::createIntTypeArgument('page', 0)
Argument::createIntTypeArgument('limit', null),
Argument::createIntTypeArgument('offset', 0)
);
}
}
}
13 changes: 6 additions & 7 deletions Loop/GuaranteedSiteLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function parseResults(LoopResult $loopResult): LoopResult
->set('ORDER_ID', $review->getOrderId())
->set('ORDER_DATE', $review->getOrderDate()?->format('Y-m-d'))
->set('REPLY', $review->getReply())
->set('REPLY_DATE', $review->getReplyDate()?->format('Y-m-d'))
;
->set('REPLY_DATE', $review->getReplyDate()?->format('Y-m-d'));
$this->addOutputFields($loopResultRow, $review);

$loopResult->addRow($loopResultRow);
Expand All @@ -53,8 +52,8 @@ public function buildModelCriteria(): GuaranteedOpinionProductReviewQuery|ModelC
$search->filterByRate($minRate, Criteria::GREATER_EQUAL);
}

if ((null !== $page = $this->getPage()) && (null !== $limit = $this->getLimit())) {
$search->paginate($page, $limit);
if ((null !== $offset = $this->getOffset()) && (null !== $limit = $this->getLimit())) {
$search->setOffset($offset)->setLimit($limit);
}

return $search;
Expand All @@ -64,8 +63,8 @@ protected function getArgDefinitions(): ArgumentCollection
{
return new ArgumentCollection(
Argument::createIntTypeArgument('min_rate'),
Argument::createIntTypeArgument('limit', 5),
Argument::createIntTypeArgument('page', 0)
Argument::createIntTypeArgument('limit', null),
Argument::createIntTypeArgument('offset', 0)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{foreach $reviews as $review}
<div>
<div>{$review->name}</div>
<div>{$review->date}</div>
<div>{$review->rating}</div>
<p>{$review->message}</p>
</div>
{/foreach}
8 changes: 8 additions & 0 deletions templates/frontOffice/default/includes/next-site-reviews.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{foreach $reviews as $review}
<div>
<div>{$review->name}</div>
<div>{$review->date}</div>
<div>{$review->rating}</div>
<p>{$review->message}</p>
</div>
{/foreach}

0 comments on commit 5444e0b

Please # to comment.