From 75510e7f94ddcb7bfa897862f1aa4203190c4106 Mon Sep 17 00:00:00 2001 From: Luke3501 Date: Tue, 3 Sep 2019 11:36:09 +0800 Subject: [PATCH 1/4] return average rate, fix #71 --- app/Http/Controllers/TaskController.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index f38c743..a1bb710 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -116,16 +116,19 @@ public function spotInfo($spotId) BETWEEN '.$long.' - '.$radius.'/(69 * COS(RADIANS('.$lat.'))) AND '.$long.' + '.$radius.'/(69 * COS(RADIANS('.$lat.'))))r WHERE `distance` < '. $radius .' - ORDER BY `distance` ASC'); - return response()->json([ - 'item' => $spotInfo, - 'comment' => $spotComment, - 'lat' => $lat, - 'long' => $long, - // 'location' => $spotLocation, - 'Nearby' => $shopNearby, - 'commentTotal' => $commentTotal - ]); + ORDER BY `distance` ASC'); //radius search raw expression + $avgRate = 0; + foreach ($spotComment as $key => $value) { + if(isset($value->rating)) + $avgRate += $value->rating; + } + return response()->json([ + 'item' => $spotInfo, + 'comment' => $spotComment, + 'Nearby' => $shopNearby, + 'commentTotal' => $commentTotal, + 'avgRate' => $avgRate / $commentTotal + ]); } } From b9faae265e033753ce3d8e213afeea9177af4baf Mon Sep 17 00:00:00 2001 From: Luke3501 Date: Tue, 3 Sep 2019 11:52:27 +0800 Subject: [PATCH 2/4] add shop rating calculating, fix #71 --- app/Http/Controllers/TaskController.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index a1bb710..304103e 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -121,13 +121,13 @@ public function spotInfo($spotId) foreach ($spotComment as $key => $value) { if(isset($value->rating)) $avgRate += $value->rating; - } + } //average rate calculate return response()->json([ 'item' => $spotInfo, 'comment' => $spotComment, 'Nearby' => $shopNearby, 'commentTotal' => $commentTotal, - 'avgRate' => $avgRate / $commentTotal + 'avgRate' => $avgRate/$commentTotal ]); } } @@ -218,12 +218,16 @@ public function shopInfo($shopId) // $shopLocation = $shopInfo->pluck('location')->first(); // $spotNearBy = Shop::where('location', $shopLocation)->first()->get(); $commentTotal = $shopComment->count(); + $avgRate = 0; + foreach ($shopComment as $key => $value) { + if(isset($value->rating)) + $avgRate += $value->rating; + } //average rate calculate return response()->json([ 'item' => $shopInfo, 'comment' => $shopComment, - // 'location' => $shopLocation, - // 'shopNearBy' => $spotNearBy - 'commentTotal' => $commentTotal + 'commentTotal' => $commentTotal, + 'avgRate' => $avgRate/$commentTotal ]); } } From aa496f92a4d6daf0b4ac0a3fa9bada948297d95c Mon Sep 17 00:00:00 2001 From: Luke3501 Date: Fri, 6 Sep 2019 11:34:53 +0800 Subject: [PATCH 3/4] sprint_6 test --- app/Http/Controllers/TaskController.php | 29 +++++++++++++++---------- routes/api.php | 3 +++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index 304103e..3d2b9d5 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -116,7 +116,7 @@ public function spotInfo($spotId) BETWEEN '.$long.' - '.$radius.'/(69 * COS(RADIANS('.$lat.'))) AND '.$long.' + '.$radius.'/(69 * COS(RADIANS('.$lat.'))))r WHERE `distance` < '. $radius .' - ORDER BY `distance` ASC'); //radius search raw expression + ORDER BY `distance` ASC'); //radius search with raw expression $avgRate = 0; foreach ($spotComment as $key => $value) { if(isset($value->rating)) @@ -215,8 +215,6 @@ public function shopInfo($shopId) ]); } else { $shopComment = Shop::find($shopId)->Comments()->latest()->get(); - // $shopLocation = $shopInfo->pluck('location')->first(); - // $spotNearBy = Shop::where('location', $shopLocation)->first()->get(); $commentTotal = $shopComment->count(); $avgRate = 0; foreach ($shopComment as $key => $value) { @@ -249,12 +247,10 @@ public function keywordSearch($keyword) ->orWhere("content","LIKE","%".$decodeKeyword."%") ->select(['id','title']) ->paginate(15); - //count the number of search results $spotTotal = $spotResult->count(); $shopTotal = $shopResult->count(); $articleTotal = $articleResult->count(); - return response()->json([ 'spot' => $spotResult, 'shop' => $shopResult, @@ -289,12 +285,12 @@ public function articleRandom() //display articles by category public function articleCategory($category) { - $categoryResult = Article::where("category", $category)->paginate(15); //Add pagination - $categoryTotal = $categoryResult->count(); //count the number of article category results - return response()->json([ - 'item' => $categoryResult, - 'categoryTotal' => $categoryTotal - ]); + $categoryResult = Article::where("category", $category)->paginate(15); //Add pagination + $categoryTotal = $categoryResult->count(); //count the number of article category results + return response()->json([ + 'item' => $categoryResult, + 'categoryTotal' => $categoryTotal + ]); } //show certain information of an article @@ -302,8 +298,17 @@ public function articleInfo($articleId) { $articleInfo = Article::where("id", $articleId)->get(); return response()->json([ - 'item' => $articleInfo, + 'item' => $articleInfo ]); } /*==========article API end==========*/ + + /*==========DB update==========*/ + public function calRating() + { + $shopList = Shop::get(); + return response()->json([ + 'item' => $shopList + ]); + } } diff --git a/routes/api.php b/routes/api.php index 5c0ada3..6ce6f5e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,6 +3,7 @@ use Illuminate\Http\Request; use App\Http\Controllers\CommentController; use App\Http\Controllers\TaskController; +use Illuminate\Routing\Route; /* |-------------------------------------------------------------------------- @@ -40,6 +41,8 @@ Route::delete("comment/{id}", "CommentController@destroy"); /*==========comment API Route ==========*/ +// Route::get("update", "TaskController@calRating"); + Route::fallback(function(){ return response()->json(['message' => 'Not Found!', 'code' => 404], 404); }); From 3e561982d4bc0dcb68bf71504fdb7deb00232259 Mon Sep 17 00:00:00 2001 From: Luke3501 Date: Wed, 11 Sep 2019 17:50:01 +0800 Subject: [PATCH 4/4] rating calculate in progess --- app/Http/Controllers/CommentController.php | 2 -- app/Http/Controllers/TaskController.php | 24 +++++++++++++++++----- routes/api.php | 3 +-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index a1159e7..89fe0eb 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -6,8 +6,6 @@ use App\Comment; -use Illuminate\Support\Facades\DB; - class CommentController extends Controller { public function create(Request $request) diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index 3d2b9d5..710fb5d 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -115,7 +115,7 @@ public function spotInfo($spotId) AND `longitude` BETWEEN '.$long.' - '.$radius.'/(69 * COS(RADIANS('.$lat.'))) AND '.$long.' + '.$radius.'/(69 * COS(RADIANS('.$lat.'))))r - WHERE `distance` < '. $radius .' + WHERE `distance` < '.$radius.' ORDER BY `distance` ASC'); //radius search with raw expression $avgRate = 0; foreach ($spotComment as $key => $value) { @@ -304,11 +304,25 @@ public function articleInfo($articleId) /*==========article API end==========*/ /*==========DB update==========*/ - public function calRating() + public function shopRating() { + $avgRate = 0; $shopList = Shop::get(); - return response()->json([ - 'item' => $shopList - ]); + foreach ($shopList as $key => $value) { + $shopComment = Shop::find($value->id)->Comments()->latest()->get(); + // $result = array_merge($shopList, $shopComment); + foreach ($shopComment as $key => $value) { + if(isset($value->rating)) + $avgRate += $value->rating;//average rate calculate + } + return response()->json([ + 'item' => $shopComment, + 'rating' => $avgRate + ]); + } + // return response()->json([ + // 'item' => $shopList[0], + // 'comment' => $shopComment + // ]); } } diff --git a/routes/api.php b/routes/api.php index 6ce6f5e..4d3f489 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,7 +3,6 @@ use Illuminate\Http\Request; use App\Http\Controllers\CommentController; use App\Http\Controllers\TaskController; -use Illuminate\Routing\Route; /* |-------------------------------------------------------------------------- @@ -41,7 +40,7 @@ Route::delete("comment/{id}", "CommentController@destroy"); /*==========comment API Route ==========*/ -// Route::get("update", "TaskController@calRating"); +Route::get("update", "TaskController@shopRating"); Route::fallback(function(){ return response()->json(['message' => 'Not Found!', 'code' => 404], 404);