add bookmark back

This commit is contained in:
2020-05-09 18:37:31 +02:00
parent 2f8e8ca378
commit a1b8962fe6
12 changed files with 520 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\BookmarkRequest;
use App\Http\Resources\BookmarkCollection;
use App\Models\Bookmark;
use App\Http\Resources\Bookmark as BookmarkResource;
use Illuminate\Http\Request;
class BookmarkController extends Controller
{
public function index()
{
$this->authorize('viewAny', Bookmark::class);
return new BookmarkCollection(request()->user()->bookmarks);
}
public function store(BookmarkRequest $request)
{
$this->authorize('create', Bookmark::class);
$bookmark = request()->user()->bookmarks()->create($request->all());
return (new BookmarkResource($bookmark))
->response()
->setStatusCode(201);
}
public function show(Bookmark $bookmark)
{
$this->authorize('view', $bookmark);
return new BookmarkResource($bookmark);
}
public function update(BookmarkRequest $request, Bookmark $bookmark)
{
$this->authorize('update', $bookmark);
$bookmark->update($request->all());
return (new BookmarkResource($bookmark))
->response()
->setStatusCode(200);
}
public function destroy(Bookmark $bookmark)
{
$this->authorize('delete', $bookmark);
$bookmark->delete();
return response([], 204);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BookmarkRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'max:65',
'url' => ['required', 'url', 'max:255'],
'favicon' => ['url', 'max:255'],
];
}
public function attributes()
{
return [
'name' => 'Nom du marque-page',
];
}
public function messages()
{
return [
'url.required' => 'Une url est nécessaire',
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Bookmark extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'type' => 'bookmark',
'bookmark_id' => $this->id,
'attributes' => [
'data' => [
'name' => $this->name,
'url' => $this->url,
'favicon' => $this->favicon,
'created_at' => $this->created_at->diffForHumans(),
'last_updated' => $this->updated_at->diffForHumans(),
]
],
],
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class BookmarkCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
'bookmarks_count' => $this->count(),
'links' => [
'self' => url('/bookmarks'),
]
];
}
}

10
app/Models/Bookmark.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Bookmark extends Model
{
protected $guarded = [];
}

View File

@@ -2,6 +2,7 @@
namespace App;
use App\Models\Bookmark;
use App\Models\Image;
use App\Models\Memo;
use App\Models\ToDoList;
@@ -64,6 +65,11 @@ class User extends Authenticatable
return $this->hasMany(ToDoList::class);
}
public function bookmarks() : HasMany
{
return $this->hasMany(Bookmark::class);
}
public function images(): MorphMany
{
return $this->morphMany(Image::class, 'imageable');

View File

@@ -0,0 +1,94 @@
<?php
namespace App\Policies;
use App\Models\Bookmark;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class BookmarkPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any bookmarks.
*
* @param \App\User $user
* @return mixed
*/
public function viewAny(User $user)
{
return true;
}
/**
* Determine whether the user can view the bookmark.
*
* @param \App\User $user
* @param \App\Models\Bookmark $bookmark
* @return mixed
*/
public function view(User $user, Bookmark $bookmark)
{
return $user->id == $bookmark->user_id;
}
/**
* Determine whether the user can create bookmarks.
*
* @param \App\User $user
* @return mixed
*/
public function create(User $user)
{
return true;
}
/**
* Determine whether the user can update the bookmark.
*
* @param \App\User $user
* @param \App\Models\Bookmark $bookmark
* @return mixed
*/
public function update(User $user, Bookmark $bookmark)
{
return $user->id == $bookmark->user_id;
}
/**
* Determine whether the user can delete the bookmark.
*
* @param \App\User $user
* @param \App\Models\Bookmark $bookmark
* @return mixed
*/
public function delete(User $user, Bookmark $bookmark)
{
return $user->id == $bookmark->user_id;
}
/**
* Determine whether the user can restore the bookmark.
*
* @param \App\User $user
* @param \App\Models\Bookmark $bookmark
* @return mixed
*/
public function restore(User $user, Bookmark $bookmark)
{
return false;
}
/**
* Determine whether the user can permanently delete the bookmark.
*
* @param \App\User $user
* @param \App\Models\Bookmark $bookmark
* @return mixed
*/
public function forceDelete(User $user, Bookmark $bookmark)
{
return false;
}
}

View File

@@ -18,6 +18,7 @@ class AuthServiceProvider extends ServiceProvider
'App\User' => 'App\Policies\UserPolicy',
'App\Models\Memo' => 'App\Policies\MemoPolicy',
'App\Models\ToDoList' => 'App\Policies\ToDoListPolicy',
'App\Models\Bookmark' => 'App\Policies\BookmarkPolicy',
];
/**