Skip to content

Commit

Permalink
Merge pull request #119 from WillTheDeveloper/api
Browse files Browse the repository at this point in the history
Api
  • Loading branch information
WillTheDeveloper authored Sep 25, 2022
2 parents 663c062 + c761215 commit feb41d0
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 9 deletions.
26 changes: 26 additions & 0 deletions app/Http/Resources/AssignmentResource.php
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
];
}
}
18 changes: 18 additions & 0 deletions app/Http/Resources/AssignmentUserResource.php
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()*/
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/CommentCollection.php
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'
]
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Resources/CommentResource.php
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
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/PostCollection.php
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'
]
];
}
}
26 changes: 26 additions & 0 deletions app/Http/Resources/PostResource.php
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
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/UserCollection.php
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'
]
];
}
}
22 changes: 22 additions & 0 deletions app/Http/Resources/UserPostResource.php
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)
];
}
}
4 changes: 4 additions & 0 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public function toArray($request)
{
return [
'name' => $this->name,
'username' => $this->username,
'bio' => $this->bio,
'created' => $this->created_at,
'admin' => $this->is_admin
];
}
}
2 changes: 1 addition & 1 deletion app/Models/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Assignment extends Model

public function User()
{
return $this->belongsToMany(User::class)->withPivot('has_seen');
return $this->belongsToMany(User::class)->withPivot('has_seen', 'user_id');
}

public function Group()
Expand Down
37 changes: 29 additions & 8 deletions routes/api.php
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));
});

0 comments on commit feb41d0

Please sign in to comment.