-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTaskController.php
More file actions
328 lines (304 loc) · 12.2 KB
/
Copy pathTaskController.php
File metadata and controls
328 lines (304 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Spot;
use App\Shop;
use App\Article;
class TaskController extends Controller
{
/*==========spot search API==========*/
//random five spots for home page
public function spotRandom()
{
$spotRandom= Spot::inRandomOrder()->take(5)->get();
return response()->json([
'item'=>$spotRandom
]);
}
//show list of all spots for search
public function spotIndex()
{
$spot = Spot::orderBy("id", "desc")
->select([
'id',
'name',
'county',
'img1'
])
->paginate(15); //Add pagination
$spotTotal = $spot->count(); //count the number of spot
return response()->json([
'item' => $spot,
'spotTotal' =>$spotTotal
]);
}
//show list of spots with location and level for search
public function spotSearch(Request $request)
{
if ($request->location && $request->level) {
$location = $request->location;
$level = $request->level;
$spotResult = Spot::where("location", $location)
->where("level", $level)
->paginate(15); //Add pagination
$spotResult -> appends(["location"=>$location,"level"=>$level]); // get the url of pagination with search parameters
$spotTotal = $spotResult->count(); //count the number of spot searching results
return response()->json([
'item' => $spotResult,
'spotTotal' => $spotTotal
]);
}
else {
if ($request->location) {
$parm = $request->location;
$spotLocation = Spot::where("location", $parm)->paginate(15); //Add pagination
$spotLocation -> appends(["location"=>$parm]); // get the url of pagination with search parameters
$spotTotal = $spotLocation->count(); //count the number of spot searching results
return response()->json([
'item' => $spotLocation,
'spotTotal' => $spotTotal
]);
}
if ($request->level) {
$parm = $request->level;
$spotLevel = Spot::where("level", $parm)->paginate(15); //Add pagination
$spotLevel -> appends(["level"=>$parm]); // get the url of pagination with search parameters
$spotTotal = $spotLevel->count(); //count the number of spot searching results
return response()->json([
'item' => $spotLevel,
'spotTotal' => $spotTotal
]);
}
}
}
//show certain information of a spot
public function spotInfo($spotId)
{
// return with info and comments
$spotInfo = Spot::where('id', $spotId)->get();
if ($spotInfo->isEmpty()) {
return response()->json([
'code' => 404,
'message' => 'this spot does not exist'
]);
} else {
$spotComment = Spot::find($spotId)->Comments()->latest()->get();
$lat = $spotInfo->pluck('latitude');
$lat = floatval($lat[0]);
$long = $spotInfo->pluck('longitude');
$long = floatval($long[0]);
$radius = 20;
$commentTotal = $spotComment->count();
$shopNearby = DB::select('SELECT * FROM(
SELECT
`id`, `name`,
3956 * ACOS(COS(RADIANS( '.$lat.' )) * COS(RADIANS(`latitude`)) * COS(RADIANS( '.$long.' ) - RADIANS(`longitude`)) + SIN(RADIANS( '.$lat.' )) * SIN(RADIANS(`latitude`))) AS `distance`
FROM `shops`
WHERE
`latitude`
BETWEEN '.$lat.' - ('.$radius.'/69)
AND '.$lat.' + ('.$radius.'/69)
AND `longitude`
BETWEEN '.$long.' - '.$radius.'/(69 * COS(RADIANS('.$lat.')))
AND '.$long.' + '.$radius.'/(69 * COS(RADIANS('.$lat.'))))r
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
]);
}
}
/*==========spot search API end==========*/
/*==========shop search API==========*/
//random five shops for home page
public function shopRandom()
{
$shopRandom = Shop::inRandomOrder()->take(5)->get();
return response()->json([
'item' => $shopRandom
]);
}
//show list of all shops for search
public function shopIndex()
{
$shop = Shop::orderBy("id", "desc")
->select([
'id',
'name',
'county',
'img1'
])
->paginate(15); //Add pagination
$shopTotal = $shop->count(); //count the number of spot
return response()->json([
'item' => $shop,
'shopTotal' => $shopTotal
]);
}
//show list of shops with location and service for search
public function shopSearch(Request $request)
{
if ($request->location && $request->service){
$location = $request->location;
$service = $request->service;
$shopResult = Shop::where("location", $location)
->where("service","LIKE", "%".$service."%")
->paginate(15); //Add pagination
$shopResult -> appends(["location"=>$location,"service"=>$service]); // get the url of pagination with search parameters
$shopTotal = $shopResult->count(); //count the number of shop searching results
return response()->json([
'item' => $shopResult,
'shopTotal' => $shopTotal
]);
}
else {
if ($request->location) {
$parm = $request->location;
$shopLocation = Shop::where("location", $parm)->paginate(15); //Add pagination
$shopLocation -> appends(["location"=>$parm]); // get the url of pagination with search parameters
$shopTotal = $shopLocation->count(); //count the number of shop searching results
return response()->json([
'item' => $shopLocation,
'shopTotal' => $shopTotal
]);
}
if ($request->service) {
$parm = $request->service;
$shopService = Shop::where("service","LIKE", "%".$parm."%")->paginate(15); //Add pagination
$shopService -> appends(["service"=>$parm]); // get the url of pagination with search parameters
$shopTotal = $shopService->count(); //count the number of shop searching results
return response()->json([
'item' => $shopService,
'shopTotal' => $shopTotal
]);
}
}
}
//show certain information of a shop
public function shopInfo($shopId)
{
// returns with shop info and comments
$shopInfo = Shop::where('id', $shopId)->get();
if ($shopInfo->isEmpty()) {
return response()->json([
'code' => 404,
'message' => 'this shop does not exist'
]);
} else {
$shopComment = Shop::find($shopId)->Comments()->latest()->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,
'commentTotal' => $commentTotal,
'avgRate' => $avgRate/$commentTotal
]);
}
}
/*==========shop search API end==========*/
/*==========keyword search API==========*/
public function keywordSearch($keyword)
{
$decodeKeyword = urldecode($keyword);//decode keyword from frontend
$spotResult = Spot::where("name","LIKE","%".$decodeKeyword."%")
->orWhere("description","LIKE","%".$decodeKeyword."%")
->select(['id','name'])
->paginate(15);
$shopResult = Shop::where("name","LIKE","%".$decodeKeyword."%")
->orWhere("description","LIKE","%".$decodeKeyword."%")
->select(['id','name'])
->paginate(15);
$articleResult = Article::where("title","LIKE","%".$decodeKeyword."%")
->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,
'article' => $articleResult,
'spotTotal' => $spotTotal,
'shopTotal' => $shopTotal,
'articleTotal' => $articleTotal
]);
}
/*==========keyword search API end==========*/
/*==========article API==========*/
//show list of all articles
public function articleIndex(){
$articleList = Article::orderBy("id", "desc")->paginate(15); //Add pagination
$articleTotal = $articleList->count(); //count the number of articles
return response()->json([
'item' => $articleList,
'articleTotal' => $articleTotal
]);
}
//random five articles for home page
public function articleRandom()
{
$random = Article::inRandomOrder()->take(5)->get();
return response()->json([
'item' => $random
]);
}
//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
]);
}
//show certain information of an article
public function articleInfo($articleId)
{
$articleInfo = Article::where("id", $articleId)->get();
return response()->json([
'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
// ]);
}
}