forked from tuandm/laravue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
149 lines (128 loc) · 5.61 KB
/
api.php
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
<?php
use App\Http\Resources\UserResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use \App\Laravue\Faker;
use \App\Laravue\JsonResponse;
use \App\Laravue\Acl;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::namespace('Api')->group(function() {
Route::post('auth/login', 'AuthController@login');
Route::group(['middleware' => 'auth:sanctum'], function () {
// Auth routes
Route::get('auth/user', 'AuthController@user');
Route::post('auth/logout', 'AuthController@logout');
Route::get('/user', function (Request $request) {
return new UserResource($request->user());
});
// Api resource routes
Route::apiResource('roles', 'RoleController')->middleware('permission:' . Acl::PERMISSION_PERMISSION_MANAGE);
Route::apiResource('users', 'UserController')->middleware('permission:' . Acl::PERMISSION_USER_MANAGE);
Route::apiResource('permissions', 'PermissionController')->middleware('permission:' . Acl::PERMISSION_PERMISSION_MANAGE);
// Custom routes
Route::put('users/{user}', 'UserController@update');
Route::get('users/{user}/permissions', 'UserController@permissions')->middleware('permission:' . Acl::PERMISSION_PERMISSION_MANAGE);
Route::put('users/{user}/permissions', 'UserController@updatePermissions')->middleware('permission:' .Acl::PERMISSION_PERMISSION_MANAGE);
Route::get('roles/{role}/permissions', 'RoleController@permissions')->middleware('permission:' . Acl::PERMISSION_PERMISSION_MANAGE);
});
});
// Fake APIs
Route::get('/table/list', function () {
$rowsNumber = mt_rand(20, 30);
$data = [];
for ($rowIndex = 0; $rowIndex < $rowsNumber; $rowIndex++) {
$row = [
'author' => Faker::randomString(mt_rand(5, 10)),
'display_time' => Faker::randomDateTime()->format('Y-m-d H:i:s'),
'id' => mt_rand(100000, 100000000),
'pageviews' => mt_rand(100, 10000),
'status' => Faker::randomInArray(['deleted', 'published', 'draft']),
'title' => Faker::randomString(mt_rand(20, 50)),
];
$data[] = $row;
}
return response()->json(new JsonResponse(['items' => $data]));
});
Route::get('/orders', function () {
$rowsNumber = 8;
$data = [];
for ($rowIndex = 0; $rowIndex < $rowsNumber; $rowIndex++) {
$row = [
'order_no' => 'LARAVUE' . mt_rand(1000000, 9999999),
'price' => mt_rand(10000, 999999),
'status' => Faker::randomInArray(['success', 'pending']),
];
$data[] = $row;
}
return response()->json(new JsonResponse(['items' => $data]));
});
Route::get('/articles', function () {
$rowsNumber = 10;
$data = [];
for ($rowIndex = 0; $rowIndex < $rowsNumber; $rowIndex++) {
$row = [
'id' => mt_rand(100, 10000),
'display_time' => Faker::randomDateTime()->format('Y-m-d H:i:s'),
'title' => Faker::randomString(mt_rand(20, 50)),
'author' => Faker::randomString(mt_rand(5, 10)),
'comment_disabled' => Faker::randomBoolean(),
'content' => Faker::randomString(mt_rand(100, 300)),
'content_short' => Faker::randomString(mt_rand(30, 50)),
'status' => Faker::randomInArray(['deleted', 'published', 'draft']),
'forecast' => mt_rand(100, 9999) / 100,
'image_uri' => 'https://via.placeholder.com/400x300',
'importance' => mt_rand(1, 3),
'pageviews' => mt_rand(10000, 999999),
'reviewer' => Faker::randomString(mt_rand(5, 10)),
'timestamp' => Faker::randomDateTime()->getTimestamp(),
'type' => Faker::randomInArray(['US', 'VI', 'JA']),
];
$data[] = $row;
}
return response()->json(new JsonResponse(['items' => $data, 'total' => mt_rand(1000, 10000)]));
});
Route::get('articles/{id}', function ($id) {
$article = [
'id' => $id,
'display_time' => Faker::randomDateTime()->format('Y-m-d H:i:s'),
'title' => Faker::randomString(mt_rand(20, 50)),
'author' => Faker::randomString(mt_rand(5, 10)),
'comment_disabled' => Faker::randomBoolean(),
'content' => Faker::randomString(mt_rand(100, 300)),
'content_short' => Faker::randomString(mt_rand(30, 50)),
'status' => Faker::randomInArray(['deleted', 'published', 'draft']),
'forecast' => mt_rand(100, 9999) / 100,
'image_uri' => 'https://via.placeholder.com/400x300',
'importance' => mt_rand(1, 3),
'pageviews' => mt_rand(10000, 999999),
'reviewer' => Faker::randomString(mt_rand(5, 10)),
'timestamp' => Faker::randomDateTime()->getTimestamp(),
'type' => Faker::randomInArray(['US', 'VI', 'JA']),
];
return response()->json(new JsonResponse($article));
});
Route::get('articles/{id}/pageviews', function ($id) {
$pageviews = [
'PC' => mt_rand(10000, 999999),
'Mobile' => mt_rand(10000, 999999),
'iOS' => mt_rand(10000, 999999),
'android' => mt_rand(10000, 999999),
];
$data = [];
foreach ($pageviews as $device => $pageview) {
$data[] = [
'key' => $device,
'pv' => $pageview,
];
}
return response()->json(new JsonResponse(['pvData' => $data]));
});