Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprint6 #82

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use App\Comment;

use Illuminate\Support\Facades\DB;

class CommentController extends Controller
{
public function create(Request $request)
Expand Down
76 changes: 51 additions & 25 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What "r" means in the code below ?
AND '.$long.' + '.$radius.'/(69 * COS(RADIANS('.$lat.'))))r

We should ask BlueT.

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
]);
}
}

Expand Down Expand Up @@ -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
]);
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -282,21 +285,44 @@ 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
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
// ]);
}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});