Skip to content
Snippets Groups Projects
Commit 1932c568 authored by Louis Leslie's avatar Louis Leslie
Browse files

Fixed search, order and detail controllers

parent 8bc8264d
Branches
No related merge requests found
......@@ -24,12 +24,20 @@ class DetailController extends BaseController{
// get biodata model
$bio_model = new Model\BiodataModel();
// get user profile pictures in review.
foreach($reviews as $review){
$bio = $bio_model->findByID($review->username);
if(!empty($bio)){
$review->image_id = $bio->pic_id;
$rating_sum = 0;
$book->avgrating = 0;
if(count($reviews) > 0){
foreach($reviews as $review){
// get user profile pictures in review
$bio = $bio_model->findByID($review->username);
if(!empty($bio)){
$review->image_id = $bio->pic_id;
}
// and get average rating for the book
$rating_sum += $review->rating;
}
$book->avgrating = (float)($rating_sum / count($reviews));
}
$results = array(
......
......@@ -19,7 +19,7 @@ class OrderController extends BaseController{
$token = $token[0];
if($token->isExpired()){
$this->setResponse(401, 'Not logged in');
return false;
return;
} else {
if(isset($params['data'], $params['data']['book_id'], $params['data']['amount'])){
$order_model = new Model\OrderModel();
......@@ -33,18 +33,22 @@ class OrderController extends BaseController{
));
// create new Order
$result = 0;
$order = $order_model->create($new_order);
return $order;
if($order){
$result = $new_order->order_id;
}
return $result;
} else {
$this->setResponse(400);
return false;
return;
}
}
}
} else {
$this->setResponse(401, 'Not logged in');
return false;
return;
}
}
......
......@@ -12,19 +12,12 @@ class SearchController extends BaseController {
*/
public function run($params){
if(isset($params['data'], $params['data']['title'])){
if(!isset($params['data']['page'])){
$page = 1;
} else {
$page = $params['data']['page'];
}
$limit = 10;
$book_model = new Model\BookModel();
$review_model = new Model\ReviewModel();
// get books by input query with limit
$data = array(":title"=>'%'.$params['data']['title'].'%');
$offset = $limit * ((int)$page-1);
$criteria = 'title LIKE :title LIMIT ' . (string)$limit . ' OFFSET ' . (string)$offset;
$criteria = 'title LIKE :title';
$qresults = $book_model->find($criteria, $data);
// get books count by input query
......@@ -45,11 +38,10 @@ class SearchController extends BaseController {
$rating_sum = 0;
foreach($review_results as $review_result){
$rating_sum += $review_result->rating;
$rating_count += 1;
}
$result->avgrating = round((float)($rating_sum / $rating_count), 2);
$result->avgrating = (float)($rating_sum / count($review_results));
}
$result->votes = $rating_count;
$result->votes = count($review_results);
}
$results = array(
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment