diff --git a/config/routes.php b/config/routes.php index 8637f8f2..9f6c7c00 100644 --- a/config/routes.php +++ b/config/routes.php @@ -119,7 +119,10 @@ $route->addGroup( $route->addRoute(['OPTIONS'], '[/{resource:.+}]', 'Api\IndexController@options'); $route->get('', 'Api\IndexController@indexV0'); + $route->get('/angeltypes', 'Api\AngelTypeController@index'); $route->get('/news', 'Api\NewsController@index'); + $route->get('/rooms', 'Api\RoomsController@index'); + $route->get('/rooms/{room_id:\d+}/shifts', 'Api\ShiftsController@entriesByRoom'); $route->addRoute( ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'], diff --git a/src/Controllers/Api/AngelTypeController.php b/src/Controllers/Api/AngelTypeController.php new file mode 100644 index 00000000..5f851fd9 --- /dev/null +++ b/src/Controllers/Api/AngelTypeController.php @@ -0,0 +1,22 @@ +orderBy('name') + ->get(['id', 'name']); + + $data = ['data' => $news]; + return $this->response + ->withContent(json_encode($data)); + } +} diff --git a/src/Controllers/Api/RoomsController.php b/src/Controllers/Api/RoomsController.php new file mode 100644 index 00000000..a5338db5 --- /dev/null +++ b/src/Controllers/Api/RoomsController.php @@ -0,0 +1,22 @@ +orderBy('name') + ->get(['id', 'name']); + + $data = ['data' => $news]; + return $this->response + ->withContent(json_encode($data)); + } +} diff --git a/src/Controllers/Api/ShiftsController.php b/src/Controllers/Api/ShiftsController.php new file mode 100644 index 00000000..ebf834ee --- /dev/null +++ b/src/Controllers/Api/ShiftsController.php @@ -0,0 +1,66 @@ +getAttribute('room_id'); + /** @var Room $room */ + $room = Room::findOrFail($roomId); + $shifts = $room->shifts() + ->with([ + 'shiftEntries.angelType', + 'shiftEntries.user.contact', + 'shiftEntries.user.personalData', + 'shiftType', + ]) + ->get(); + $shiftEntries = []; + + // Blob of not-optimized mediocre pseudo-serialization + foreach ($shifts as $shift) { + $entries = []; + foreach ($shift->shiftEntries as $entry) { + $user = $entry->user; + $userData = [ + 'id' => $user->id, + 'name' => $user->name, + 'first_name' => $user->personalData->first_name, + 'last_name' => $user->personalData->last_name, + 'pronoun' => $user->personalData->pronoun, + 'contact' => $user->contact->only(['dect', 'mobile']), + ]; + + $entries[] = [ + 'user' => $userData, + 'type' => $entry->angelType->only(['id', 'name']), + ]; + } + + $shiftEntries[] = [ + 'id' => $shift->id, + 'title' => $shift->title, + 'description' => $shift->description, + 'start' => $shift->start, + 'end' => $shift->end, + 'entries' => $entries, + 'room' => $room->only(['id', 'name']), + 'shift_type' => $shift->shiftType->only(['id', 'name', 'description']), + 'created_at' => $shift->created_at, + 'updated_at' => $shift->updated_at, + ]; + } + + $data = ['data' => $shiftEntries]; + return $this->response + ->withContent(json_encode($data)); + } +} diff --git a/tests/Unit/Controllers/Api/AngelTypeControllerTest.php b/tests/Unit/Controllers/Api/AngelTypeControllerTest.php new file mode 100644 index 00000000..0b82c4bc --- /dev/null +++ b/tests/Unit/Controllers/Api/AngelTypeControllerTest.php @@ -0,0 +1,33 @@ +initDatabase(); + AngelType::factory(3)->create(); + + $controller = new AngelTypeController(new Response()); + + $response = $controller->index(); + $this->validateApiResponse('/angeltypes', 'get', $response); + + $this->assertEquals(['application/json'], $response->getHeader('content-type')); + $this->assertJson($response->getContent()); + + $data = json_decode($response->getContent(), true); + $this->assertArrayHasKey('data', $data); + $this->assertCount(3, $data['data']); + } +} diff --git a/tests/Unit/Controllers/Api/RoomsControllerTest.php b/tests/Unit/Controllers/Api/RoomsControllerTest.php new file mode 100644 index 00000000..6305e610 --- /dev/null +++ b/tests/Unit/Controllers/Api/RoomsControllerTest.php @@ -0,0 +1,33 @@ +initDatabase(); + Room::factory(3)->create(); + + $controller = new RoomsController(new Response()); + + $response = $controller->index(); + $this->validateApiResponse('/rooms', 'get', $response); + + $this->assertEquals(['application/json'], $response->getHeader('content-type')); + $this->assertJson($response->getContent()); + + $data = json_decode($response->getContent(), true); + $this->assertArrayHasKey('data', $data); + $this->assertCount(3, $data['data']); + } +} diff --git a/tests/Unit/Controllers/Api/ShiftsControllerTest.php b/tests/Unit/Controllers/Api/ShiftsControllerTest.php new file mode 100644 index 00000000..44876a7d --- /dev/null +++ b/tests/Unit/Controllers/Api/ShiftsControllerTest.php @@ -0,0 +1,55 @@ +initDatabase(); + + /** @var Room $room */ + $room = Room::factory()->create(); + + Shift::factory(3) + ->has(ShiftEntry::factory(2), 'shiftEntries') + ->create(['room_id' => $room->id]); + + foreach (User::all() as $user) { + // Generate user data + /** @var User $user */ + PersonalData::factory()->create(['user_id' => $user->id]); + Contact::factory()->create(['user_id' => $user->id]); + } + + $request = new Request(); + $request = $request->withAttribute('room_id', $room->id); + + $controller = new ShiftsController(new Response()); + + $response = $controller->entriesByRoom($request); + $this->validateApiResponse('/rooms', 'get', $response); + + $this->assertEquals(['application/json'], $response->getHeader('content-type')); + $this->assertJson($response->getContent()); + + $data = json_decode($response->getContent(), true); + $this->assertArrayHasKey('data', $data); + $this->assertCount(3, $data['data']); + } +}