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 f38c743..710fb5d 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -115,17 +115,20 @@ public function spotInfo($spotId) AND `longitude` 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 - ]); + WHERE `distance` < '.$radius.' + ORDER BY `distance` ASC'); //radius search with raw expression + $avgRate = 0; + 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 + ]); } } @@ -212,15 +215,17 @@ 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) { + 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 ]); } } @@ -242,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, @@ -282,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 @@ -295,8 +298,31 @@ public function articleInfo($articleId) { $articleInfo = Article::where("id", $articleId)->get(); return response()->json([ - 'item' => $articleInfo, + 'item' => $articleInfo ]); } /*==========article API end==========*/ + + /*==========DB update==========*/ + public function shopRating() + { + $avgRate = 0; + $shopList = Shop::get(); + 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 5c0ada3..4d3f489 100644 --- a/routes/api.php +++ b/routes/api.php @@ -40,6 +40,8 @@ Route::delete("comment/{id}", "CommentController@destroy"); /*==========comment API Route ==========*/ +Route::get("update", "TaskController@shopRating"); + Route::fallback(function(){ return response()->json(['message' => 'Not Found!', 'code' => 404], 404); });