diff --git a/app/Http/Controllers/EventCategoryController.php b/app/Http/Controllers/EventCategoryController.php new file mode 100644 index 0000000..c6db2da --- /dev/null +++ b/app/Http/Controllers/EventCategoryController.php @@ -0,0 +1,85 @@ +validated(); + + $event = $request->user()->events()->create($validated); + $event->save(); + //dd($event, $validated); + return (new EventResource($event)) + ->response() + ->setStatusCode(201); + } + + /** + * Display the specified resource. + * + * @param \App\Models\Event $event + * @return \Illuminate\Http\Response + */ + public function show(Event $event) + { + // + } + + /** + * 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. + * + * @param \Illuminate\Http\Request $request + * @param \App\Models\Event $event + * @return \Illuminate\Http\Response + */ + public function update(Request $request, Event $event) + { + // + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Event $event + * @return \Illuminate\Http\Response + */ + public function destroy(Event $event) + { + // + } +} diff --git a/app/Http/Requests/EventRequest.php b/app/Http/Requests/EventRequest.php new file mode 100644 index 0000000..879f6ab --- /dev/null +++ b/app/Http/Requests/EventRequest.php @@ -0,0 +1,63 @@ +user()); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'name' => 'required', + 'description' => 'nullable|string', + 'category_id' => 'required|exists:event_categories,id', + 'start_date' => 'required|date', + 'end_date' => 'date|after_or_equal:start_date', + 'location' => 'string|nullable' + ]; + } + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes() + { + return [ + 'name' => 'nom', + 'start_date' => 'date de début', + 'end_date' => 'date de fin', + 'location' => 'lieu', + ]; + } + + /** + * Get the error messages for the defined validation rules. + * + * @return array + */ + public function messages() + { + return [ + 'name.required' => 'A :attribute is required', + 'start_date.required' => 'A :attribute is required', + ]; + } +} diff --git a/app/Http/Resources/Event.php b/app/Http/Resources/Event.php new file mode 100644 index 0000000..d1d33b3 --- /dev/null +++ b/app/Http/Resources/Event.php @@ -0,0 +1,41 @@ + [ + 'type' => 'events', + 'event_id' => $this->id, + 'attributes' => [ + 'data' => [ + 'name' => $this->name, + 'description' => $this->description, + 'start_date' => $this->start_date, + 'end_date' => $this->end_date, + 'location' => $this->location, + 'category' => [ + 'data' => [ + 'category_id' => $this->category_id + ], + ], + ] + ], + ], + 'links' => [ + 'self' => url('/events/'.$this->id), + ] + ]; + } +} diff --git a/app/Models/Event.php b/app/Models/Event.php new file mode 100644 index 0000000..bc96b28 --- /dev/null +++ b/app/Models/Event.php @@ -0,0 +1,10 @@ +path = 'images/default-cover.jpg'; }); } + + public function events(): HasMany + { + return $this->hasMany(Event::class); + } } diff --git a/database/migrations/2020_07_19_081942_create_event_categories_table.php b/database/migrations/2020_07_19_081942_create_event_categories_table.php new file mode 100644 index 0000000..54fe094 --- /dev/null +++ b/database/migrations/2020_07_19_081942_create_event_categories_table.php @@ -0,0 +1,33 @@ +id(); + $table->string('name'); + $table->string('description')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('event_categories'); + } +} diff --git a/database/migrations/2020_07_19_085105_create_events_table.php b/database/migrations/2020_07_19_085105_create_events_table.php new file mode 100644 index 0000000..7d8d29e --- /dev/null +++ b/database/migrations/2020_07_19_085105_create_events_table.php @@ -0,0 +1,38 @@ +id(); + $table->unsignedBigInteger('user_id'); + $table->unsignedBigInteger('category_id'); + $table->string('name'); + $table->string('description')->nullable(); + $table->timestamp('start_date'); + $table->timestamp('end_date')->nullable(); + $table->string('location')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('events'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 91cb6d1..2559ad3 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder */ public function run() { - // $this->call(UsersTableSeeder::class); + $this->call(EventCategorySeeder::class); } } diff --git a/database/seeds/EventCategorySeeder.php b/database/seeds/EventCategorySeeder.php new file mode 100644 index 0000000..d3fdfec --- /dev/null +++ b/database/seeds/EventCategorySeeder.php @@ -0,0 +1,22 @@ + 'Non-classée', + 'description' => 'Evénement sans catégorie', + ]; + + $category = \App\Models\EventCategory::create($data); + $category->save(); + } +} diff --git a/routes/api.php b/routes/api.php index d53eb87..a690ba9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -26,6 +26,7 @@ Route::middleware('auth:api')->group(function () { '/meteo' => 'MeteoController', '/to-do-lists' => 'ToDoListController', '/to-do-lists/{toDoList}/to-do' => 'ToDoController', + '/events' => 'EventController', // '/users/{user}/posts' => 'UserPostController', // '/friend-request' => 'FriendRequestController', ]); diff --git a/tests/Feature/EventsTest.php b/tests/Feature/EventsTest.php new file mode 100644 index 0000000..ca588f3 --- /dev/null +++ b/tests/Feature/EventsTest.php @@ -0,0 +1,67 @@ +withoutExceptionHandling(); + $this->actingAs($user = factory(User::class)->create(), 'api'); + + app(\DatabaseSeeder::class)->call(\EventCategorySeeder::class); + + //dd(EventCategory::all()); + $response = $this->post('/api/events', [ + 'name' => 'Test name event', + 'description' => 'Test description event', + 'category_id' => 1, + 'start_date' => '2020-07-20 09:00:00', + 'end_date' => '2020-07-26 09:00:00', + 'location' => 'Marcillac', + ])->assertStatus(201); + + $event = Event::first(); + + $this->assertEquals($user->id, $event->user_id); + $this->assertEquals(1, $event->category_id); + $this->assertEquals('Test name event', $event->name); + $this->assertEquals('Test description event', $event->description); + $this->assertEquals('2020-07-20 09:00:00', $event->start_date); + $this->assertEquals('2020-07-26 09:00:00', $event->end_date); + $this->assertEquals('Marcillac', $event->location); + $response->assertJson([ + 'data' => [ + 'type' => 'events', + 'event_id' => $event->id, + 'attributes' => [ + 'data' => [ + 'name' => $event->name, + 'description' => $event->description, + 'start_date' => $event->start_date, + 'end_date' => $event->end_date, + 'location' => $event->location, + 'category' => [ + 'data' => [ + 'category_id' => 1 + ], + ], + ] + ], + ], + 'links' => [ + 'self' => url('/events/'.$event->id), + ] + ]); + } +} diff --git a/tests/Feature/ImagesTest.php b/tests/Feature/ImagesTest.php index fcd8782..d87df54 100644 --- a/tests/Feature/ImagesTest.php +++ b/tests/Feature/ImagesTest.php @@ -99,7 +99,7 @@ class ImagesTest extends TestCase 'profile_image' => [ 'data' => [ 'type' => 'images', - 'image_id' => 2, + 'image_id' => 3, 'attributes' => [] ] ]