start add Memos
This commit is contained in:
64
app/Http/Controllers/MemosController.php
Normal file
64
app/Http/Controllers/MemosController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\Memo as MemoResource;
|
||||
use App\Models\Memo;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class MemosController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$this->authorize('viewAny', Memo::class);
|
||||
|
||||
return MemoResource::collection(request()->user()->memos);
|
||||
}
|
||||
|
||||
public function store()
|
||||
{
|
||||
$this->authorize('create', Memo::class);
|
||||
|
||||
$memo = request()->user()->memos()->create($this->validateData());
|
||||
|
||||
return (new MemoResource($memo))
|
||||
->response()
|
||||
->setStatusCode(Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
public function show(Memo $memo)
|
||||
{
|
||||
$this->authorize('view', $memo);
|
||||
|
||||
return new MemoResource($memo);
|
||||
}
|
||||
|
||||
public function update(Memo $memo)
|
||||
{
|
||||
$this->authorize('update', $memo);
|
||||
|
||||
$memo->update($this->validateData());
|
||||
|
||||
return (new MemoResource($memo))
|
||||
->response()
|
||||
->setStatusCode(Response::HTTP_OK);
|
||||
}
|
||||
|
||||
public function destroy(Memo $memo)
|
||||
{
|
||||
$this->authorize('delete', $memo);
|
||||
|
||||
$memo->delete();
|
||||
|
||||
return response([], Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
private function validateData()
|
||||
{
|
||||
return request()->validate([
|
||||
'name' => 'required',
|
||||
'memo' => 'required',
|
||||
]);
|
||||
}
|
||||
}
|
||||
30
app/Http/Resources/Memo.php
Normal file
30
app/Http/Resources/Memo.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class Memo extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'data' => [
|
||||
'memo_id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'memo' => $this->memo,
|
||||
'last_updated' => $this->updated_at->diffForHumans(),
|
||||
//'tags' => TagResource::collection($this->tags),
|
||||
],
|
||||
'links' => [
|
||||
'self' => $this->path(),
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
27
app/Models/Memo.php
Normal file
27
app/Models/Memo.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
class Memo extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public function path()
|
||||
{
|
||||
return '/memos/' . $this->id;
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// public function tags(): MorphToMany
|
||||
// {
|
||||
// return $this->morphToMany(Tag::class, 'taggable')->withTimestamps()->withPivot('user_id');
|
||||
// }
|
||||
}
|
||||
94
app/Policies/MemoPolicy.php
Normal file
94
app/Policies/MemoPolicy.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Memo;
|
||||
use App\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class MemoPolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
/**
|
||||
* Determine whether the user can view any memos.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @return mixed
|
||||
*/
|
||||
public function viewAny(User $user)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can view the memo.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @param \App\Models\Memo $memo
|
||||
* @return mixed
|
||||
*/
|
||||
public function view(User $user, Memo $memo)
|
||||
{
|
||||
return $user->id == $memo->user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can create memos.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @return mixed
|
||||
*/
|
||||
public function create(User $user)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can update the memo.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @param \App\Models\Memo $memo
|
||||
* @return mixed
|
||||
*/
|
||||
public function update(User $user, Memo $memo)
|
||||
{
|
||||
return $user->id == $memo->user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can delete the memo.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @param \App\Models\Memo $memo
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete(User $user, Memo $memo)
|
||||
{
|
||||
return $user->id == $memo->user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can restore the memo.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @param \App\Models\Memo $memo
|
||||
* @return mixed
|
||||
*/
|
||||
public function restore(User $user, Memo $memo)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can permanently delete the memo.
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @param \App\Models\Memo $memo
|
||||
* @return mixed
|
||||
*/
|
||||
public function forceDelete(User $user, Memo $memo)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
protected $policies = [
|
||||
// 'App\Model' => 'App\Policies\ModelPolicy',
|
||||
'App\User' => 'App\Policies\UserPolicy',
|
||||
'App\Models\Memo' => 'App\Policies\MemoPolicy',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Models\Memo;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
@@ -42,4 +43,9 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->role === 2;
|
||||
}
|
||||
|
||||
public function memos()
|
||||
{
|
||||
return $this->hasMany(Memo::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user