8000 Add changes for updating and deleting resources · laravel-json-api/tutorial-app@677569c · GitHub
[go: up one dir, main page]

Skip to content

Commit 677569c

Browse files
committed
Add changes for updating and deleting resources
1 parent adef833 commit 677569c

File tree

5 files changed

+172
-5
lines changed

5 files changed

+172
-5
lines changed

app/JsonApi/V1/Posts/PostRequest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ class PostRequest extends ResourceRequest
1818
*/
1919
public function rules(): array
2020
{
21+
$post = $this->model();
22+
$uniqueSlug = Rule::unique('posts', 'slug');
23+
24+
if ($post) {
25+
$uniqueSlug->ignoreModel($post);
26+
}
27+
2128
return [
2229
'content' => ['required', 'string'],
2330
'publishedAt' => ['nullable', JsonApiRule::dateTime()],
24-
'slug' => ['required', 'string', Rule::unique('posts', 'slug')],
31+
'slug' => ['required', 'string', $uniqueSlug],
2532
'tags' => JsonApiRule::toMany(),
2633
'title' => ['required', 'string'],
2734
];

app/Policies/PostPolicy.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,43 @@ public function create(User $user)
9393
*/
9494
public function update(User $user, Post $post)
9595
{
96-
//
96+
return $user->is($post->author);
97+
}
98+
99+
/**
100+
* Determine whether the user can update the model's tags relationship.
101+
*
102+
* @param User $user
103+
* @param Post $post
104+
* @return bool|\Illuminate\Auth\Access\Response
105+
*/
106+
public function updateTags(User $user, Post $post)
107+
{
108+
return $this->update($user, $post);
109+
}
110+
111+
/**
112+
* Determine whether the user can attach tags to the model's tags relationship.
113+
*
114+
* @param User $user
115+
* @param Post $post
116+
* @return bool|\Illuminate\Auth\Access\Response
117+
*/
118+
public function attachTags(User $user, Post $post)
119+
{
120+
return $this->update($user, $post);
121+
}
122+
123+
/**
124+
* Determine whether the user can detach tags from the model's tags relationship.
125+
*
126+
* @param User $user
127+
* @param Post $post
128+
* @return bool|\Illuminate\Auth\Access\Response
129+
*/
130+
public function detachTags(User $user, Post $post)
131+
{
132+
return $this->update($user, $post);
97133
}
98134

99135
/**
@@ -105,7 +141,7 @@ public function update(User $user, Post $post)
105141
*/
106142
public function delete(User $user, Post $post)
107143
{
108-
//
144+
return $this->update($user, $post);
109145
}
110146

111147
/**

http/06-updating-resources.http

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Chapter 6: Updating Resources
2+
3+
PATCH {{host}}/api/v1/posts/2?include=tags
4+
Authorization: Bearer {{token}}
5+
Accept: application/vnd.api+json
6+
Content-Type: application/vnd.api+json
7+
8+
{
9+
"data": {
10+
"type": "posts",
11+
"id": "2",
12+
"attributes": {
13+
"publishedAt": "2021-10-23T11:55:00.000000Z",
14+
"title": "How to Create and Update JSON:API Resources"
15+
},
16+
"relationships": {
17+
"tags": {
18+
"data": [
19+
{
20+
"type": "tags",
21+
"id": "1"
22+
},
23+
{
24+
"type": "tags",
25+
"id": "2"
26+
}
27+
]
28+
}
29+
}
30+
}
31+
}
32+
33+
###
34+
35+
PATCH {{host}}/api/v1/posts/2/relationships/tags
36+
Authorization: Bearer {{token}}
37+
Accept: application/vnd.api+json
38+
Content-Type: application/vnd.api+json
39+
40+
{
41+
"data": []
42+
}
43+
44+
###
45+
46+
GET {{host}}/api/v1/posts/2/tags
47+
Authorization: Bearer {{token}}
48+
Accept: application/vnd.api+json
49+
50+
###
51+
52+
PATCH {{host}}/api/v1/posts/2/relationships/tags
53+
Authorization: Bearer {{token}}
54+
Accept: application/vnd.api+json
55+
Content-Type: application/vnd.api+json
56+
57+
{
58+
"data": [
59+
{
60+
"type": "tags",
61+
"id": "1"
62+
}
63+
]
64+
}
65+
66+
###
67+
68+
GET {{host}}/api/v1/posts/2/tags
69+
Authorization: Bearer {{token}}
70+
Accept: application/vnd.api+json
71+
72+
###
73+
74+
POST {{host}}/api/v1/posts/2/relationships/tags
75+
Authorization: Bearer {{token}}
76+
Accept: application/vnd.api+json
77+
Content-Type: application/vnd.api+json
78+
79+
{
80+
"data": [
81+
{
82+
"type": "tags",
83+
"id": "2"
84+
}
85+
]
86+
}
87+
88+
###
89+
90+
GET {{host}}/api/v1/posts/2/tags
91+
Authorization: Bearer {{token}}
92+
Accept: application/vnd.api+json
93+
94+
###
95+
96+
DELETE {{host}}/api/v1/posts/2/relationships/tags
97+
Authorization: Bearer {{token}}
98+
Accept: application/vnd.api+json
99+
Content-Type: application/vnd.api+json
100+
101+
{
102+
"data": [
103+
{
104+
"type": "tags",
105+
"id": "1"
106+
}
107+
]
108+
}
109+
110+
###
111+
112+
GET {{host}}/api/v1/posts/2/tags
113+
Authorization: Bearer {{token}}
114+
Accept: application/vnd.api+json

http/07-deleting-resources.http

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Chapter 7: Deleting Resources
2+
3+
DELETE {{host}}/api/v1/posts/2
4+
Authorization: Bearer {{token}}
5+
Accept: application/vnd.api+json
6+
7+
###
8+
9+
GET {{host}}/api/v1/posts/2
10+
Authorization: Bearer {{token}}
11+
Accept: application/vnd.api+json

routes/api.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
JsonApiRoute::server('v1')->prefix('v1')->resources(function ($server) {
2424
$server->resource('posts', JsonApiController::class)
25-
->only('index', 'show', 'store')
2625
->relationships(function ($relations) {
2726
$relations->hasOne('author')->readOnly();
2827
$relations->hasMany('comments')->readOnly();
29-
$relations->hasMany('tags')->readOnly();
28+
$relations->hasMany('tags');
3029
});
3130
});

0 commit comments

Comments
 (0)
0