Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit b69a7db

Browse files
committed
lots of cleanup and more tests
1 parent f3319fc commit b69a7db

30 files changed

+3553
-1384
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ RUN composer dump-autoload --optimize && \
2525

2626
RUN cp .env.example .env
2727

28+
RUN php artisan route:cache
29+
2830
EXPOSE 80
2931

3032
CMD ["/usr/bin/supervisord"]

app/Entities/User.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Illuminate\Database\Eloquent\SoftDeletes;
1010
use Illuminate\Foundation\Auth\User as Authenticatable;
1111

12+
/**
13+
* Class User
14+
* @package App\Entities
15+
*/
1216
class User extends Authenticatable
1317
{
1418
use Notifiable, UuidScopeTrait, HasApiTokens, HasRoles, SoftDeletes;

app/Exceptions/Handler.php

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44

55
use Exception;
66
use Illuminate\Auth\AuthenticationException;
7+
use Illuminate\Validation\ValidationException;
8+
use Illuminate\Database\Eloquent\ModelNotFoundException;
79
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
10+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
812

13+
/**
14+
* Class Handler
15+
* @package App\Exceptions
16+
*/
917
class Handler extends ExceptionHandler
1018
{
1119
/**
@@ -44,6 +52,15 @@ public function report(Exception $exception)
4452
*/
4553
public function render($request, Exception $exception)
4654
{
55+
if($exception instanceof NotFoundHttpException){
56+
return $this->renderNotFoundException($exception);
57+
}
58+
if($exception instanceof ModelNotFoundException) {
59+
return $this->renderModelNotFoundException($exception);
60+
}
61+
if($exception instanceof AccessDeniedHttpException) {
62+
return $this->renderAccessDeniedException($exception);
63+
}
4764
return parent::render($request, $exception);
4865
}
4966

@@ -57,9 +74,71 @@ public function render($request, Exception $exception)
5774
protected function unauthenticated($request, AuthenticationException $exception)
5875
{
5976
if ($request->expectsJson()) {
60-
return response()->json(['error' => 'Unauthenticated.'], 401);
77+
return response()->json(['message' => 'Unauthenticated.'], 401);
6178
}
6279

6380
return redirect()->guest('login');
6481
}
82+
83+
/**
84+
* Create a response object from the given validation exception.
85+
*
86+
* @param \Illuminate\Validation\ValidationException $e
87+
* @param \Illuminate\Http\Request $request
88+
* @return \Symfony\Component\HttpFoundation\Response
89+
*/
90+
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
91+
{
92+
if ($e->response) {
93+
return $e->response;
94+
}
95+
96+
$errors = $e->validator->errors()->getMessages();
97+
98+
if ($request->expectsJson()) {
99+
return response()->json([
100+
'message' => 'Validation error',
101+
'errors' => $errors
102+
], 422);
103+
}
104+
105+
return redirect()->back()->withInput(
106+
$request->input()
107+
)->withErrors($errors);
108+
}
109+
110+
/**
111+
* @return \Illuminate\Http\JsonResponse
112+
*/
113+
protected function renderNotFoundException($e)
114+
{
115+
if(request()->expectsJson()){
116+
return response()->json(['message' => 'Not Found'], 404);
117+
}
118+
return $this->renderHttpException($e);
119+
}
120+
121+
/**
122+
* @param $e
123+
* @return \Illuminate\Http\JsonResponse
124+
*/
125+
protected function renderModelNotFoundException($e)
126+
{
127+
if(request()->expectsJson()){
128+
return response()->json(['message' => 'Not Found'], 404);
129+
}
130+
throw new NotFoundHttpException();
131+
}
132+
133+
/**
134+
* @param $e
135+
* @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
136+
*/
137+
protected function renderAccessDeniedException($e)
138+
{
139+
if(request()->expectsJson()){
140+
return response()->json(['message' => 'Forbidden'], 403);
141+
}
142+
return $this->renderHttpException($e);
143+
}
65144
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
7+
/**
8+
* Class PingController
9+
* @package App\Http\Controllers\Api
10+
*/
11+
class PingController extends Controller
12+
{
13+
/**
14+
* @return \Illuminate\Http\JsonResponse
15+
*/
16+
public function index()
17+
{
18+
return response()->json([
19+
'status' => 'ok',
20+
'timestamp' => \Carbon\Carbon::now()
21+
]);
22+
}
23+
}

app/Http/Controllers/Api/Users/UsersController.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
class UsersController extends Controller
1616
{
1717

18+
/**
19+
* @var User
20+
*/
1821
protected $model;
1922

23+
/**
24+
* UsersController constructor.
25+
* @param User $model
26+
*/
2027
public function __construct(User $model)
2128
{
2229
$this->model = $model;
@@ -35,7 +42,7 @@ public function __construct(User $model)
3542
public function index(Request $request)
3643
{
3744
$paginator = $this->model->with('roles.permissions')
38-
->paginate($request->get('limit', env('PAGINATE_LIMIT', 20)));
45+
->paginate($request->get('limit', config('app.pagination_limit')));
3946
return fractal()->collection($paginator->getCollection(), new UserTransformer())
4047
->paginateWith(new IlluminatePaginatorAdapter($paginator))
4148
->respond();
@@ -78,27 +85,31 @@ public function store(Request $request)
7885
*/
7986
public function update(Request $request, $uuid)
8087
{
81-
88+
$user = $this->model->byUuid($uuid)->firstOrFail();
89+
$rules = [
90+
'name' => 'required',
91+
'email' => 'required|email|unique:users,email,'.$user->id,
92+
];
93+
if($request->method() == "PATCH") {
94+
$rules = [
95+
'name' => 'sometimes|required',
96+
'email' => 'sometimes|required|email|unique:users,email,'.$user->id,
97+
];
98+
}
99+
$this->validate($request, $rules);
100+
$user->update($request->except('_token'));
101+
return fractal($user->fresh(), new UserTransformer())->respond();
82102
}
83103

84104
/**
85105
* @param Request $request
86106
* @param $uuid
87107
* @return mixed
88108
*/
89-
public function partialUpdate(Request $request, $uuid)
90-
{
91-
92-
}
93-
94-
95-
/**
96-
* @param Request $request
97-
* @param $uuids
98-
* @return mixed
99-
*/
100-
public function destroy(Request $request, $uuids)
109+
public function destroy(Request $request, $uuid)
101110
{
102-
111+
$user = $this->model->byUuid($uuid)->firstOrFail();
112+
$user->delete();
113+
return response(null, 204);
103114
}
104115
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
/**
6+
* Class ApiDocsController
7+
* @package App\Http\Controllers
8+
*/
9+
class ApiDocsController extends Controller
10+
{
11+
/**
12+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
13+
*/
14+
public function index()
15+
{
16+
return view('apidocs');
17+
}
18+
}

app/Http/Controllers/Auth/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LoginController extends Controller
2525
*
2626
* @var string
2727
*/
28-
protected $redirectTo = '/home';
28+
protected $redirectTo = '/';
2929

3030
/**
3131
* Create a new controller instance.

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RegisterController extends Controller
2727
*
2828
* @var string
2929
*/
30-
protected $redirectTo = '/home';
30+
protected $redirectTo = '/';
3131

3232
/**
3333
* Create a new controller instance.

app/Http/Controllers/Auth/ResetPasswordController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ResetPasswordController extends Controller
2525
*
2626
* @var string
2727
*/
28-
protected $redirectTo = '/home';
28+
protected $redirectTo = '/';
2929

3030
/**
3131
* Create a new controller instance.

app/Http/Controllers/Controller.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use Illuminate\Http\Request;
56
use Illuminate\Foundation\Bus\DispatchesJobs;
67
use Illuminate\Routing\Controller as BaseController;
78
use Illuminate\Foundation\Validation\ValidatesRequests;
@@ -10,4 +11,25 @@
1011
class Controller extends BaseController
1112
{
1213
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
14+
15+
/**
16+
* Create the response for when a request fails validation.
17+
*
18+
* @param \Illuminate\Http\Request $request
19+
* @param array $errors
20+
* @return \Symfony\Component\HttpFoundation\Response
21+
*/
22+
protected function buildFailedValidationResponse(Request $request, array $errors)
23+
{
24+
if ($request->expectsJson()) {
25+
return response([
26+
'message' => 'Validation Error',
27+
'errors' => $errors
28+
], 422);
29+
}
30+
31+
return redirect()->to($this->getRedirectUrl())
32+
->withInput($request->input())
33+
->withErrors($errors, $this->errorBag());
34+
}
1335
}

0 commit comments

Comments
 (0)