first work on To Do Lists

This commit is contained in:
2020-04-19 22:46:28 +02:00
parent c71f3ca4d8
commit c6b94aa1f1
17 changed files with 710 additions and 2 deletions

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers;
use App\Http\Resources\ToDo as ToDoResource;
use App\Http\Resources\ToDoList as ToDoListResource;
use App\Models\ToDo;
use App\Models\ToDoList;
use Illuminate\Http\Request;
class ToDoController extends Controller
{
public function store(ToDoList $toDoList)
{
$this->authorize('create', ToDoList::class);
$toDo = $toDoList->toDos()->create($this->validateData());
return (new ToDoResource($toDo))
->response()
->setStatusCode(201);
}
public function update(ToDoList $toDoList, ToDo $toDo)
{
$this->authorize('update', $toDoList);
$toDo->update($this->validateData());
return (new ToDoResource($toDo))
->response()
->setStatusCode(200);
}
private function validateData()
{
return request()->validate([
'name' => 'required',
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers;
use App\Http\Resources\ToDoList as ToDoListResource;
use App\Models\ToDoList;
use Illuminate\Http\Request;
class ToDoListController extends Controller
{
public function store()
{
$this->authorize('create', ToDoList::class);
$toDoList = request()->user()->toDoLists()->create($this->validateData());
return (new ToDoListResource($toDoList))
->response()
->setStatusCode(201);
}
public function show(ToDoList $toDoList)
{
$this->authorize('view', $toDoList);
return new ToDoListResource($toDoList);
}
public function update(ToDoList $toDoList)
{
$this->authorize('update', $toDoList);
$toDoList->update($this->validateData());
return (new ToDoListResource($toDoList))
->response()
->setStatusCode(200);
}
public function destroy(ToDoList $toDoList)
{
$this->authorize('delete', $toDoList);
$toDoList->delete();
return response([], 204);
}
private function validateData()
{
return request()->validate([
'name' => 'required',
]);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ToDo extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'type' => 'to-dos',
'to_do_id' => $this->id,
'attributes' => [
'data' => [
'name' => $this->name,
'order' => $this->order,
'checked_at' => optional($this->checked_at)->diffForHumans(),
'last_updated' => $this->updated_at->diffForHumans(),
]
]
],
'links' => [
'self' => url('/to-do-lists/'.$this->to_do_list_id),
]
];
}
}

View File

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

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Http\Resources;
use App\Http\Resources\User as UserResource;
use Illuminate\Http\Resources\Json\JsonResource;
class ToDoList extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'type' => 'to-do-lists',
'to_do_list_id' => $this->id,
'attributes' => [
'data' => [
'name' => $this->name,
'to_dos' => new ToDoCollection($this->toDos),
'posted_by' => new UserResource($this->user),
'last_updated' => $this->updated_at->diffForHumans(),
// 'cover_image' => new ImageResource($this->coverImage),
]
]
//'tags' => TagResource::collection($this->tags),
],
'links' => [
'self' => $this->path(),
]
];
}
}

18
app/Models/ToDo.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ToDo extends Model
{
protected $guarded = [];
protected $dates = ['checked_at'];
public function toDoList() :BelongsTo
{
return $this->belongsTo(ToDoList::class);
}
}

28
app/Models/ToDoList.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ToDoList extends Model
{
protected $guarded = [];
public function path()
{
return '/to-do-lists/' . $this->id;
}
public function users(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function toDos(): HasMany
{
return $this->hasMany(ToDo::class);
}
}

View File

@@ -4,6 +4,7 @@ namespace App;
use App\Models\Image;
use App\Models\Memo;
use App\Models\ToDoList;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
@@ -82,4 +83,9 @@ class User extends Authenticatable
$userImage->path = 'images/default-cover.jpg';
});
}
public function toDoLists(): HasMany
{
return $this->hasMany(ToDoList::class);
}
}

View File

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

View File

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