finish basic crud api
This commit is contained in:
@@ -19,16 +19,6 @@ class EventController extends Controller
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Show the form for creating a new resource.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a newly created resource in storage.
|
* Store a newly created resource in storage.
|
||||||
*
|
*
|
||||||
@@ -60,27 +50,22 @@ class EventController extends Controller
|
|||||||
->setStatusCode(200);
|
->setStatusCode(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Show the form for editing the specified resource.
|
|
||||||
*
|
|
||||||
* @param \App\Models\Event $event
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function edit(Event $event)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the specified resource in storage.
|
* Update the specified resource in storage.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @param \App\Models\Event $event
|
* @param \App\Models\Event $event
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, Event $event)
|
public function update(EventRequest $request, Event $event)
|
||||||
{
|
{
|
||||||
//
|
$this->authorize('update', $event);
|
||||||
|
|
||||||
|
$event->update($request->all());
|
||||||
|
|
||||||
|
return (new EventResource($event))
|
||||||
|
->response()
|
||||||
|
->setStatusCode(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,6 +76,10 @@ class EventController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function destroy(Event $event)
|
public function destroy(Event $event)
|
||||||
{
|
{
|
||||||
//
|
$this->authorize('delete', $event);
|
||||||
|
|
||||||
|
$event->delete();
|
||||||
|
|
||||||
|
return response([], 204);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,9 @@ class EventRequest extends FormRequest
|
|||||||
'name' => 'required',
|
'name' => 'required',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
'category_id' => 'required|exists:event_categories,id',
|
'category_id' => 'required|exists:event_categories,id',
|
||||||
|
'private' => 'boolean',
|
||||||
'start_date' => 'required|date',
|
'start_date' => 'required|date',
|
||||||
'end_date' => 'date|after_or_equal:start_date',
|
'end_date' => 'date|after_or_equal:start_date|nullable',
|
||||||
'location' => 'string|nullable'
|
'location' => 'string|nullable'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
94
app/Policies/EventPolicy.php
Normal file
94
app/Policies/EventPolicy.php
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Policies;
|
||||||
|
|
||||||
|
use App\Models\Event;
|
||||||
|
use App\User;
|
||||||
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||||
|
|
||||||
|
class EventPolicy
|
||||||
|
{
|
||||||
|
use HandlesAuthorization;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can view any models.
|
||||||
|
*
|
||||||
|
* @param \App\User $user
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function viewAny(User $user)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can view the model.
|
||||||
|
*
|
||||||
|
* @param \App\User $user
|
||||||
|
* @param \App\Models\Event $event
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function view(User $user, Event $event)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can create models.
|
||||||
|
*
|
||||||
|
* @param \App\User $user
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function create(User $user)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can update the model.
|
||||||
|
*
|
||||||
|
* @param \App\User $user
|
||||||
|
* @param \App\Models\Event $event
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function update(User $user, Event $event)
|
||||||
|
{
|
||||||
|
return $user->id == $event->user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can delete the model.
|
||||||
|
*
|
||||||
|
* @param \App\User $user
|
||||||
|
* @param \App\Models\Event $event
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function delete(User $user, Event $event)
|
||||||
|
{
|
||||||
|
return $user->id == $event->user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can restore the model.
|
||||||
|
*
|
||||||
|
* @param \App\User $user
|
||||||
|
* @param \App\Models\Event $event
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function restore(User $user, Event $event)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can permanently delete the model.
|
||||||
|
*
|
||||||
|
* @param \App\User $user
|
||||||
|
* @param \App\Models\Event $event
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function forceDelete(User $user, Event $event)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ class AuthServiceProvider extends ServiceProvider
|
|||||||
'App\Models\Memo' => 'App\Policies\MemoPolicy',
|
'App\Models\Memo' => 'App\Policies\MemoPolicy',
|
||||||
'App\Models\ToDoList' => 'App\Policies\ToDoListPolicy',
|
'App\Models\ToDoList' => 'App\Policies\ToDoListPolicy',
|
||||||
'App\Models\Bookmark' => 'App\Policies\BookmarkPolicy',
|
'App\Models\Bookmark' => 'App\Policies\BookmarkPolicy',
|
||||||
|
'App\Models\Event' => 'App\Policies\EventPolicy',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
13
database/factories/EventCategoryFactory.php
Normal file
13
database/factories/EventCategoryFactory.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||||
|
|
||||||
|
use App\Models\EventCategory;
|
||||||
|
use Faker\Generator as Faker;
|
||||||
|
|
||||||
|
$factory->define(EventCategory::class, function (Faker $faker) {
|
||||||
|
return [
|
||||||
|
'name' => $faker->words(3, [false]),
|
||||||
|
'description' => $faker->words(rand(10, 30), [false]),
|
||||||
|
];
|
||||||
|
});
|
||||||
23
database/factories/EventFactory.php
Normal file
23
database/factories/EventFactory.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||||
|
|
||||||
|
use App\Models\Event;
|
||||||
|
use App\Models\EventCategory;
|
||||||
|
use Faker\Generator as Faker;
|
||||||
|
|
||||||
|
$factory->define(Event::class, function (Faker $faker) {
|
||||||
|
$startDate = $faker->dateTimeThisMonth;
|
||||||
|
$endDate = $faker->dateTimeBetween($startDate, date_add($startDate, date_interval_create_from_date_string('10 days')));
|
||||||
|
$endDate = (rand(0, 1) === 0) ? null : $endDate;
|
||||||
|
return [
|
||||||
|
'user_id' => factory(\App\User::class),
|
||||||
|
'category_id' => factory(EventCategory::class),
|
||||||
|
'name' => $faker->words(3, [false]),
|
||||||
|
'description' => $faker->words(rand(10, 300), [false]),
|
||||||
|
'private' => rand(0,1),
|
||||||
|
'start_date' => $startDate,
|
||||||
|
'end_date' => $endDate,
|
||||||
|
'location' => $faker->city,
|
||||||
|
];
|
||||||
|
});
|
||||||
@@ -19,6 +19,7 @@ class CreateEventsTable extends Migration
|
|||||||
$table->unsignedBigInteger('category_id');
|
$table->unsignedBigInteger('category_id');
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->text('description')->nullable();
|
$table->text('description')->nullable();
|
||||||
|
$table->boolean('private')->default(true);
|
||||||
$table->timestamp('start_date');
|
$table->timestamp('start_date');
|
||||||
$table->timestamp('end_date')->nullable();
|
$table->timestamp('end_date')->nullable();
|
||||||
$table->string('location')->nullable();
|
$table->string('location')->nullable();
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class BookmarkTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function a_user_can_retrueved_all_this_to_do_lists()
|
public function a_user_can_retrieved_all_this_to_do_lists()
|
||||||
{
|
{
|
||||||
$this->actingAs($user = factory(User::class)->create(), 'api');
|
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||||
$bookmarkOne = factory(Bookmark::class)->create(['user_id' => $user->id]);
|
$bookmarkOne = factory(Bookmark::class)->create(['user_id' => $user->id]);
|
||||||
|
|||||||
@@ -306,6 +306,85 @@ class EventsTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function a_to_event_can_be_patch()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||||
|
$event = factory(Event::class)->create(['user_id' => $user->id]);
|
||||||
|
|
||||||
|
$response = $this->patch('/api/events/' . $event->id, [
|
||||||
|
'category_id' => $event->category_id,
|
||||||
|
'name' => 'Event Update',
|
||||||
|
'description' => 'Event Update description',
|
||||||
|
'start_date' => '2020-07-20 09:00:00',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$event = $event->fresh();
|
||||||
|
|
||||||
|
$this->assertEquals('Event Update', $event->name);
|
||||||
|
$this->assertEquals('Event Update description', $event->description);
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertJson([
|
||||||
|
'data' => [
|
||||||
|
'event_id' => $event->id,
|
||||||
|
'attributes' => [
|
||||||
|
'data' => [
|
||||||
|
'name' => 'Event Update',
|
||||||
|
'description' => 'Event Update description',
|
||||||
|
'start_date' => '2020-07-20 09:00:00',
|
||||||
|
'category' => [
|
||||||
|
'data' => [
|
||||||
|
'category_id' => $event->category_id,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function only_the_owner_can_patch_the_event()
|
||||||
|
{
|
||||||
|
$user = factory(User::class)->create();
|
||||||
|
$event = factory(Event::class)->create(['id' => 123, 'user_id' => $user->id]);
|
||||||
|
|
||||||
|
$this->actingAs($anotherUser = factory(User::class)->create(), 'api');
|
||||||
|
|
||||||
|
$this->patch('/api/events/'. $event->id, $this->data())
|
||||||
|
->assertStatus(403);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function a_event_can_be_delete()
|
||||||
|
{
|
||||||
|
$this->actingAs($user = factory(User::class)->create(), 'api');
|
||||||
|
|
||||||
|
$event = factory(Event::class)->create(['user_id' => $user->id]);
|
||||||
|
|
||||||
|
$response = $this->delete('/api/events/' . $event->id);
|
||||||
|
|
||||||
|
$event = $event->fresh();
|
||||||
|
|
||||||
|
$this->assertCount(0, Event::all());
|
||||||
|
|
||||||
|
$response->assertStatus(204);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function only_the_owner_can_delete_the_bookmark()
|
||||||
|
{
|
||||||
|
$user = factory(User::class)->create();
|
||||||
|
$event = factory(Event::class)->create();
|
||||||
|
|
||||||
|
$this->actingAs($anotherUser = factory(User::class)->create(), 'api');
|
||||||
|
|
||||||
|
$response = $this->delete('/api/events/' . $event->id);
|
||||||
|
|
||||||
|
$response->assertStatus(403);
|
||||||
|
}
|
||||||
|
|
||||||
private function data()
|
private function data()
|
||||||
{
|
{
|
||||||
@@ -313,6 +392,7 @@ class EventsTest extends TestCase
|
|||||||
'name' => 'Test name event',
|
'name' => 'Test name event',
|
||||||
'description' => 'Test description event',
|
'description' => 'Test description event',
|
||||||
'category_id' => 1,
|
'category_id' => 1,
|
||||||
|
'private' => false,
|
||||||
'start_date' => '2020-07-20 09:00:00',
|
'start_date' => '2020-07-20 09:00:00',
|
||||||
'end_date' => '2020-07-26 09:00:00',
|
'end_date' => '2020-07-26 09:00:00',
|
||||||
'location' => 'Marcillac',
|
'location' => 'Marcillac',
|
||||||
|
|||||||
Reference in New Issue
Block a user