-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from WillTheDeveloper/api
Api
- Loading branch information
Showing
11 changed files
with
226 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class AssignmentResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'title' => $this->title, | ||
'details' => $this->details, | ||
'due' => $this->duedate, | ||
'set' => $this->setdate, | ||
'subject' => $this->Subject->subject, | ||
'created' => $this->created_at | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class AssignmentUserResource extends JsonResource | ||
{ | ||
// WORK IN PROGRESS - ONLY WANT TO SHOW USER'S NAME INSIDE PIVOT TABLE | ||
|
||
public function toArray($request) | ||
{ | ||
return [ | ||
'title' => $this->title, | ||
/*'students' => $this->User->toArray()*/ | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class CommentCollection extends ResourceCollection | ||
{ | ||
/** | ||
* Transform the resource collection into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'data' => $this->collection, | ||
'links' => [ | ||
'self' => 'link-value' | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class CommentResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'comment' => $this->comment, | ||
'user' => $this->User->name, | ||
'post' => [ | ||
'title' => $this->Post->title, | ||
'user' => $this->Post->User->name, | ||
'created' => $this->Post->created_at | ||
], | ||
'commented' => $this->created_at | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class PostCollection extends ResourceCollection | ||
{ | ||
/** | ||
* Transform the resource collection into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'data' => $this->collection, | ||
'links' => [ | ||
'self' => 'link-value' | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class PostResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'title' => $this->title, | ||
'body' => $this->body, | ||
'user' => $this->User->name, | ||
'subject' => $this->Subject->subject, | ||
'views' => $this->views, | ||
'tag' => $this->Tag->tag | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class UserCollection extends ResourceCollection | ||
{ | ||
/** | ||
* Transform the resource collection into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'data' => $this->collection, | ||
'links' => [ | ||
'self' => 'link-value' | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class UserPostResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'name' => $this->name, | ||
'posts' => PostResource::collection($this->post) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
<?php | ||
|
||
use App\Http\Resources\UserResource; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Route; | ||
|
||
Route::get('/user/current', function () { | ||
return new UserResource(User::query()->findOrFail(auth()->id())); | ||
})->middleware('auth:sanctum'); | ||
// ENSURE THAT YOU USE THE MIGRATIONS THAT ARE LOCATED ON THE MASTER BRANCH! | ||
|
||
Route::get('/users', function () { | ||
return new \App\Http\Resources\UserCollection(\App\Models\User::all()); | ||
})->middleware('auth:sanctum'); | ||
Route::get('/user/posts', function () { | ||
return new \App\Http\Resources\UserPostResource(auth()->user()); | ||
})->middleware('auth:sanctum'); | ||
Route::get('/user/{id}', function ($id) { | ||
return new UserResource(User::query()->findOrFail($id)); | ||
})->middleware('auth:sanctum'); | ||
return new \App\Http\Resources\UserResource(\App\Models\User::findOrFail($id)); | ||
})->middleware(['auth:sanctum', 'admin']); | ||
|
||
|
||
Route::get('/post/{slug}', function ($slug) { | ||
return new \App\Http\Resources\PostResource(\App\Models\Post::firstWhere('slug', $slug)); | ||
})->middleware(['auth:sanctum']); | ||
Route::get('/posts', function () { | ||
return new \App\Http\Resources\PostCollection(\App\Models\Post::all()); | ||
})->middleware(['auth:sanctum', 'admin']); | ||
|
||
|
||
Route::get('/comment/{id}', function ($id) { | ||
return new \App\Http\Resources\CommentResource(\App\Models\Comment::findOrFail($id)); | ||
}); | ||
|
||
Route::get('/assignment/{id}', function ($id) { | ||
return new \App\Http\Resources\AssignmentResource(\App\Models\Assignment::findOrFail($id)); | ||
}); | ||
Route::get('/assignment/{id}/students', function ($id) { | ||
return new \App\Http\Resources\AssignmentUserResource(\App\Models\Assignment::findOrFail($id)); | ||
}); |