8000 Install package and initial server setup · laravel-json-api/tutorial-app@e4a66f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4a66f9

Browse files
committed
Install package and initial server setup
1 parent 271447e commit e4a66f9

File tree

8 files changed

+919
-4
lines changed

8 files changed

+919
-4
lines changed

app/Exceptions/Handler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Handler extends ExceptionHandler
1313
* @var array
1414
*/
1515
protected $dontReport = [
16-
//
16+
\LaravelJsonApi\Core\Exceptions\JsonApiException::class,
1717
];
1818

1919
/**
@@ -37,5 +37,9 @@ public function register()
3737
$this->reportable(function (Throwable $e) {
3838
//
3939
});
40+
41+
$this->renderable(
42+
\LaravelJsonApi\Exceptions\ExceptionParser::make()->renderable()
43+
);
4044
}
4145
}

app/JsonApi/V1/Posts/PostSchema.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\JsonApi\V1\Posts;
4+
5+
use App\Models\Post;
6+
use LaravelJsonApi\Eloquent\Contracts\Paginator;
7+
use LaravelJsonApi\Eloquent\Fields\DateTime;
8+
use LaravelJsonApi\Eloquent\Fields\ID;
9+
use LaravelJsonApi\Eloquent\Fields\Str;
10+
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
11+
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
12+
use LaravelJsonApi\Eloquent\Schema;
13+
14+
class PostSchema extends Schema
15+
{
16+
17+
/**
18+
* The model the schema corresponds to.
19+
*
20+
* @var string
21+
*/
22+
public static string $model = Post::class;
23+
24+
/**
25+
* Get the resource fields.
26+
*
27+
* @return array
28+
*/
29+
public function fields(): array
30+
{
31+
return [
32+
ID::make(),
33+
Str::make('content'),
34+
DateTime::make('createdAt')->sortable()->readOnly(),
35+
DateTime::make('publishedAt')->sortable(),
36+
Str::make('slug'),
37+
Str::make('title')->sortable(),
38+
DateTime::make('updatedAt')->sortable()->readOnly(),
39+
];
40+
}
41+
42+
/**
43+
* Get the resource filters.
44+
*
45+
* @return array
46+
*/
47+
public function filters(): array
48+
{
49+
return [
50+
WhereIdIn::make($this),
51+
];
52+
}
53+
54+
/**
55+
* Get the resource paginator.
56+
*
57+
* @return Paginator|null
58+
*/
59+
public function pagination(): ?Paginator
60+
{
61+
return PagePagination::make();
62+
}
63+
64+
}

app/JsonApi/V1/Server.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\JsonApi\V1;
4+
5+
use LaravelJsonApi\Core\Server\Server as BaseServer;
6+
7+
class Server extends BaseServer
8+
{
9+
10+
/**
11+
* The base URI namespace for this server.
12+
*
13+
* @var string
14+
*/
15+
protected string $baseUri = '/api/v1';
16+
17+
/**
18+
* Bootstrap the server when it is handling an HTTP request.
19+
*
20+
* @return void
21+
*/
22+
public function serving(): void
23+
{
24+
// no-op
25+
}
26+
27+
/**
28+
* Get the server's list of schemas.
29+
*
30+
* @return array
31+
*/
32+
protected function allSchemas(): array
33+
{
34+
return [
35+
Posts\PostSchema::class,
36+
];
37+
}
38+
}

app/Policies/PostPolicy.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\Post;
6+
use App\Models\User;
7+
use Illuminate\Auth\Access\HandlesAuthorization;
8+
9+
class PostPolicy
10+
{
11+
use HandlesAuthorization;
12+
13+
/**
14+
* Determine whether the user can view any models.
15+
*
16+
* @param \App\Models\User $user
17+
* @return \Illuminate\Auth\Access\Response|bool
18+
*/
19+
public function viewAny(User $user)
20+
{
21+
//
22+
}
23+
24+
/**
25+
* Determine whether the user can view the model.
26+
*
27+
* @param \App\Models\User|null $user
28+
* @param \App\Models\Post $post
29+
* @return \Illuminate\Auth\Access\Response|bool
30+
*/
31+
public function view(?User $user, Post $post)
32+
{
33+
if ($post->published_at) {
34+
return true;
35+
}
36+
37+
return $user && $user->is($post->author);
38+
}
39+
40+
/**
41+
* Determine whether the user can create models.
42+
*
43+
* @param \App\Models\User $user
44+
* @return \Illuminate\Auth\Access\Response|bool
45+
*/
46+
public function create(User $user)
47+
{
48+
//
49+
}
50+
51+
/**
52+
* Determine whether the user can update the model.
53+
*
54+
* @param \App\Models\User $user
55+
* @param \App\Models\Post $post
56+
* @return \Illuminate\Auth\Access\Response|bool
57+
*/
58+
public function update(User $user, Post $post)
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Determine whether the user can delete the model.
65+
*
66+
* @param \App\Models\User $user
67+
* @param \App\Models\Post $post
68+
* @return \Illuminate\Auth\Access\Response|bool
69+
*/
70+
public function delete(User $user, Post $post)
71+
{
72+
//
73+
}
74+
75+
/**
76+
* Determine whether the user can restore the model.
77+
*
78+
* @param \App\Models\User $user
79+
* @param \App\Models\Post $post
80+
* @return \Illuminate\Auth\Access\Response|bool
81+
*/
82+
public function restore(User $user, Post $post)
83+
{
84+
//
85+
}
86+
87+
/**
88+
* Determine whether the user can permanently delete the model.
89+
*
90+
* @param \App\Models\User $user
91+
* @param \App\Models\Post $post
92+
* @return \Illuminate\Auth\Access\Response|bool
93+
*/
94+
public function forceDelete(User $user, Post $post)
95+
{
96+
//
97+
}
98+
}

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
"keywords": ["framework", "laravel"],
66
"license": "MIT",
77
"require": {
8-
"php": "^7.3|^8.0",
8+
"php": "^7.4|^8.0",
99
"fruitcake/laravel-cors": "^2.0",
1010
"guzzlehttp/guzzle": "^7.0.1",
11+
"laravel-json-api/laravel": "^1.0",
1112
"laravel/framework": "^8.54",
1213
"laravel/sanctum": "^2.11",
1314
"laravel/tinker": "^2.5"
1415
},
1516
"require-dev": {
1617
"facade/ignition": "^2.5",
1718
"fakerphp/faker": "^1.9.1",
19+
"laravel-json-api/testing": "^1.0",
1820
"laravel/sail": "^1.0.1",
1921
"mockery/mockery": "^1.4.2",
2022
"nunomaduro/collision": "^5.0",

0 commit comments

Comments
 (0)
0