API: Add /angeltypes, /rooms, /rooms/{id}/shifts
This commit is contained in:
parent
72649c9522
commit
a5cebc8535
|
@ -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'],
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Controllers\Api;
|
||||
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\AngelType;
|
||||
|
||||
class AngelTypeController extends ApiController
|
||||
{
|
||||
public function index(): Response
|
||||
{
|
||||
$news = AngelType::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$data = ['data' => $news];
|
||||
return $this->response
|
||||
->withContent(json_encode($data));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Controllers\Api;
|
||||
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\Room;
|
||||
|
||||
class RoomsController extends ApiController
|
||||
{
|
||||
public function index(): Response
|
||||
{
|
||||
$news = Room::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$data = ['data' => $news];
|
||||
return $this->response
|
||||
->withContent(json_encode($data));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Controllers\Api;
|
||||
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\Room;
|
||||
|
||||
class ShiftsController extends ApiController
|
||||
{
|
||||
public function entriesByRoom(Request $request): Response
|
||||
{
|
||||
$roomId = (int) $request->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));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers\Api;
|
||||
|
||||
use Engelsystem\Controllers\Api\AngelTypeController;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\AngelType;
|
||||
|
||||
class AngelTypeControllerTest extends ApiBaseControllerTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Controllers\Api\AngelTypeController::index
|
||||
*/
|
||||
public function testIndex(): void
|
||||
{
|
||||
$this->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']);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers\Api;
|
||||
|
||||
use Engelsystem\Controllers\Api\RoomsController;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\Room;
|
||||
|
||||
class RoomsControllerTest extends ApiBaseControllerTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Controllers\Api\RoomsController::index
|
||||
*/
|
||||
public function testIndex(): void
|
||||
{
|
||||
$this->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']);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers\Api;
|
||||
|
||||
use Engelsystem\Controllers\Api\ShiftsController;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Models\Room;
|
||||
use Engelsystem\Models\Shifts\Shift;
|
||||
use Engelsystem\Models\Shifts\ShiftEntry;
|
||||
use Engelsystem\Models\User\Contact;
|
||||
use Engelsystem\Models\User\PersonalData;
|
||||
use Engelsystem\Models\User\User;
|
||||
|
||||
class ShiftsControllerTest extends ApiBaseControllerTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Controllers\Api\ShiftsController::entriesByRoom
|
||||
*/
|
||||
public function testEntriesByRoom(): void
|
||||
{
|
||||
$this->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']);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue