Skip to content

Commit 7af42af

Browse files
committed
Finish user endpoints
1 parent 323c7d1 commit 7af42af

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
lines changed

app/Http/Controllers/UserController.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
class UserController extends Controller
1414
{
1515
/**
16-
* View all users.
16+
* View all users
1717
*
1818
* This endpoint uses a custom Scribe strategy that parses a
19-
* `@usesPaginationParameters` annotation.
19+
* `@usesPagination` annotation to add some query parameters.
2020
*
21-
* @usesPaginationParameters
21+
* The sample response is gotten by Scribe making a test API call (aka "response call").
22+
*
23+
* @usesPagination
2224
*/
2325
public function index()
2426
{
25-
//
27+
return UserResource::collection(User::all());
2628
}
2729

2830
/**
@@ -38,9 +40,33 @@ public function store(CreateUserRequest $request)
3840
return ['user' => $user, 'token' => $token->plainTextToken];
3941
}
4042

43+
/**
44+
* Authenticate
45+
*
46+
* Get a new API token.
47+
*
48+
* <aside>Yes, we know you can impersonate any user.🙄</aside>
49+
*
50+
* @response {"token": "2|KLDoUXc68Ko0JaFDZoX9qYkUqWglwdGxQsvTGBCg"}
51+
* @responseField token The new API token. Valid forever.
52+
*/
53+
public function authenticate($id)
54+
{
55+
$token = User::findOrFail($id)->createToken('default');
56+
return ['token' => $token->plainTextToken];
57+
}
58+
4159
/**
4260
* Fetch a user
4361
*
62+
* This endpoint's response uses an Eloquent API resource, so we tell Scribe that using an annotation,
63+
* and it figures out how to generate a sample. The 404 sample is gotten from a "response file".
64+
*
65+
* <aside class="success">Also, pretty cool: this endpoint's URL parameters were figured out entirely by Scribe!</aside>
66+
*
67+
* @apiResource App\Http\Resources\UserResource
68+
* @apiResourceModel App\Models\User with=sideProjects
69+
* @responseFile 404 responses/not_found.json {"resource": "user"}
4470
*/
4571
public function show($id)
4672
{

app/Http/Resources/UserResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function toArray($request)
1818
'id' => $this->id,
1919
'name' => $this->name,
2020
'email' => $this->email,
21+
'side_projects' => $this->sideProjects,
2122
];
2223
}
2324
}

app/Models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ class User extends Authenticatable
4141
protected $casts = [
4242
'email_verified_at' => 'datetime',
4343
];
44+
45+
public function sideProjects()
46+
{
47+
return $this->hasMany(SideProject::class);
48+
}
4449
}

database/factories/SideProjectFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class SideProjectFactory extends Factory
2222
public function definition()
2323
{
2424
return [
25-
//
25+
'name' => $this->faker->words(3, true),
26+
'description' => $this->faker->sentence,
2627
];
2728
}
2829
}

routes/api.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@
4545
Route::post('users/', [UserController::class, 'store']);
4646
Route::get('users/{id}', [UserController::class, 'show']);
4747
Route::get('users/', [UserController::class, 'index']);
48+
Route::post('users/{id}/auth', [UserController::class, 'authenticate']);
4849

4950
Route::apiResource('sideprojects', SideProjectController::class);
50-
51-
Route::post('users/auth', function (Request $request) {
52-
$token = $request->user()->createToken();
53-
54-
return ['token' => $token->plainTextToken];
55-
});

storage/responses/not_found.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "Not found",
3+
"resource": ""
4+
}

0 commit comments

Comments
 (0)