API: Rename rooms to locations and start/end to starts_at/ends_at

This commit is contained in:
Igor Scheller 2023-11-03 19:03:31 +01:00 committed by Michael Weimann
parent 2e38b55167
commit 497c1772f7
6 changed files with 70 additions and 68 deletions

View File

@ -121,8 +121,8 @@ $route->addGroup(
$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->get('/locations', 'Api\LocationsController@index');
$route->get('/locations/{location_id:\d+}/shifts', 'Api\ShiftsController@entriesByLocation');
$route->addRoute(
['POST', 'PUT', 'DELETE', 'PATCH'],

View File

@ -140,7 +140,7 @@ components:
- created_at
- updated_at
- url
Room:
Location:
type: object
properties:
id:
@ -151,8 +151,8 @@ components:
example: Heaven
url:
type: string
example: https://example.com/rooms/42
description: Link of the room start page
example: https://example.com/location/42
description: Link of the location page
required:
- id
- name
@ -172,18 +172,18 @@ components:
description: |
Shift description, should be added to the shift type description as it might be empty.
Normally contains additional information for a specific shift / task.
start:
starts_at:
type: string
format: date-time
description: DateTime in ISO-8601 format
example: 2023-05-13T14:00:00.000000Z
end:
ends_at:
type: string
format: date-time
description: DateTime in ISO-8601 format
example: 2023-05-13T16:00:00.000000Z23
room:
$ref: '#/components/schemas/Room'
location:
$ref: '#/components/schemas/Location'
shift_type:
$ref: '#/components/schemas/ShiftType'
created_at:
@ -211,9 +211,9 @@ components:
- id
- title
- description
- start
- end
- room
- starts_at
- ends_at
- location
- shift_type
- created_at
- updated_at
@ -349,11 +349,11 @@ paths:
'403':
$ref: '#/components/responses/ForbiddenError'
/rooms:
/locations:
get:
tags:
- shift
summary: Get a list of rooms
summary: Get a list of locations
responses:
'200':
description: Ok
@ -365,25 +365,25 @@ paths:
data:
type: array
items:
$ref: '#/components/schemas/Room'
$ref: '#/components/schemas/Location'
'401':
$ref: '#/components/responses/UnauthorizedError'
'403':
$ref: '#/components/responses/ForbiddenError'
/rooms/{id}/shifts:
/locations/{id}/shifts:
parameters:
- name: id
in: path
required: true
description: The rooms identifier
description: The locations identifier
example: 42
schema:
type: integer
get:
tags:
- shift
summary: Get all shifts in the requested room
summary: Get all shifts in the requested location
responses:
'200':
description: Ok

View File

@ -5,18 +5,18 @@ declare(strict_types=1);
namespace Engelsystem\Controllers\Api;
use Engelsystem\Http\Response;
use Engelsystem\Models\Room;
use Engelsystem\Models\Location;
class RoomsController extends ApiController
class LocationsController extends ApiController
{
public function index(): Response
{
$models = Room::query()
$models = Location::query()
->orderBy('name')
->get(['id', 'name']);
$models->map(function (Room $model): void {
$model->url = $this->url->to('/rooms', ['action' => 'view', 'room_id' => $model->id]);
$models->map(function (Location $model): void {
$model->url = $this->url->to('/locations', ['action' => 'view', 'location_id' => $model->id]);
});
$data = ['data' => $models];

View File

@ -6,23 +6,23 @@ namespace Engelsystem\Controllers\Api;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Models\Room;
use Engelsystem\Models\Location;
use Engelsystem\Models\Shifts\NeededAngelType;
use Engelsystem\Models\Shifts\Shift;
use Illuminate\Database\Eloquent\Collection;
class ShiftsController extends ApiController
{
public function entriesByRoom(Request $request): Response
public function entriesByLocation(Request $request): Response
{
$roomId = (int) $request->getAttribute('room_id');
/** @var Room $room */
$room = Room::findOrFail($roomId);
$locationId = (int) $request->getAttribute('location_id');
/** @var Location $location */
$location = Location::findOrFail($locationId);
/** @var Shift[]|Collection $shifts */
$shifts = $room->shifts()
$shifts = $location->shifts()
->with([
'neededAngelTypes.angelType',
'room',
'location.neededAngelTypes.angelType',
'shiftEntries.angelType',
'shiftEntries.user.contact',
'shiftEntries.user.personalData',
@ -70,16 +70,16 @@ class ShiftsController extends ApiController
];
}
$roomData = $room->only(['id', 'name']);
$roomData['url'] = $this->url->to('/rooms', ['action' => 'view', 'room_id' => $room->id]);
$locationData = $location->only(['id', 'name']);
$locationData['url'] = $this->url->to('/locations', ['action' => 'view', 'location_id' => $location->id]);
$shiftEntries[] = [
'id' => $shift->id,
'title' => $shift->title,
'description' => $shift->description,
'start' => $shift->start,
'end' => $shift->end,
'room' => $roomData,
'starts_at' => $shift->start,
'ends_at' => $shift->end,
'location' => $locationData,
'shift_type' => $shift->shiftType->only(['id', 'name', 'description']),
'created_at' => $shift->created_at,
'updated_at' => $shift->updated_at,
@ -101,8 +101,8 @@ class ShiftsController extends ApiController
// From shift
$neededAngelTypes = $shift->neededAngelTypes;
// Add from room
foreach ($shift->room->neededAngelTypes as $neededAngelType) {
// Add from location
foreach ($shift->location->neededAngelTypes as $neededAngelType) {
/** @var NeededAngelType $existingNeededAngelType */
$existingNeededAngelType = $neededAngelTypes
->where('angel_type_id', $neededAngelType->angel_type_id)
@ -112,7 +112,7 @@ class ShiftsController extends ApiController
continue;
}
$existingNeededAngelType->room_id = $shift->room->id;
$existingNeededAngelType->location_id = $shift->location->id;
$existingNeededAngelType->count += $neededAngelType->count;
}

View File

@ -4,24 +4,23 @@ declare(strict_types=1);
namespace Engelsystem\Test\Unit\Controllers\Api;
use Engelsystem\Controllers\Api\RoomsController;
use Engelsystem\Controllers\Api\LocationsController;
use Engelsystem\Http\Response;
use Engelsystem\Models\Room;
use Engelsystem\Models\Location;
class RoomsControllerTest extends ApiBaseControllerTest
class LocationsControllerTest extends ApiBaseControllerTest
{
/**
* @covers \Engelsystem\Controllers\Api\RoomsController::index
* @covers \Engelsystem\Controllers\Api\LocationsController::index
*/
public function testIndex(): void
{
$this->initDatabase();
$items = Room::factory(3)->create();
$items = Location::factory(3)->create();
$controller = new RoomsController(new Response(), $this->url);
$controller = new LocationsController(new Response(), $this->url);
$response = $controller->index();
$this->validateApiResponse('/rooms', 'get', $response);
$this->validateApiResponse('/locations', 'get', $response);
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
$this->assertJson($response->getContent());

View File

@ -8,7 +8,7 @@ use Engelsystem\Controllers\Api\ShiftsController;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Models\Room;
use Engelsystem\Models\Location;
use Engelsystem\Models\Shifts\NeededAngelType;
use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftEntry;
@ -19,55 +19,58 @@ use Engelsystem\Models\User\User;
class ShiftsControllerTest extends ApiBaseControllerTest
{
/**
* @covers \Engelsystem\Controllers\Api\ShiftsController::entriesByRoom
* @covers \Engelsystem\Controllers\Api\ShiftsController::entriesByLocation
* @covers \Engelsystem\Controllers\Api\ShiftsController::getNeededAngelTypes
*/
public function testEntriesByRoom(): void
public function testEntriesByLocation(): void
{
$this->initDatabase();
/** @var Room $room */
$room = Room::factory()->create();
/** @var Location $location */
$location = Location::factory()->create();
// Shifts
/** @var Shift $shiftA */
$shiftA = Shift::factory(1)
->create(['room_id' => $room->id, 'start' => Carbon::now()->subHour()])
->create(['location_id' => $location->id, 'start' => Carbon::now()->subHour()])
->first();
/** @var Shift $shiftB */
$shiftB = Shift::factory(1)
->create(['room_id' => $room->id, 'start' => Carbon::now()->addHour()])
->create(['location_id' => $location->id, 'start' => Carbon::now()->addHour()])
->first();
// "Empty" entry to be skipped
NeededAngelType::factory(1)->create(['room_id' => null, 'shift_id' => $shiftA->id, 'count' => 0]);
NeededAngelType::factory(1)->create(['location_id' => null, 'shift_id' => $shiftA->id, 'count' => 0]);
// Needed entry by shift
/** @var NeededAngelType $byShift */
$byShift = NeededAngelType::factory(2)
->create(['room_id' => null, 'shift_id' => $shiftA->id, 'count' => 2])
->create(['location_id' => null, 'shift_id' => $shiftA->id, 'count' => 2])
->first();
// Needed entry by room
/** @var NeededAngelType $byRoom */
$byRoom = NeededAngelType::factory(1)
->create(['room_id' => $room->id, 'shift_id' => null, 'count' => 3])
// Needed entry by location
/** @var NeededAngelType $byLocation */
$byLocation = NeededAngelType::factory(1)
->create(['location_id' => $location->id, 'shift_id' => null, 'count' => 3])
->first();
// Added by both
NeededAngelType::factory(1)
->create([
'room_id' => $room->id, 'shift_id' => null, 'angel_type_id' => $byShift->angel_type_id, 'count' => 3,
'location_id' => $location->id,
'shift_id' => null,
'angel_type_id' => $byShift->angel_type_id,
'count' => 3,
])
->first();
// By shift
ShiftEntry::factory(2)->create(['shift_id' => $shiftA->id, 'angel_type_id' => $byShift->angel_type_id]);
// By room
ShiftEntry::factory(1)->create(['shift_id' => $shiftA->id, 'angel_type_id' => $byRoom->angel_type_id]);
// By location
ShiftEntry::factory(1)->create(['shift_id' => $shiftA->id, 'angel_type_id' => $byLocation->angel_type_id]);
// Additional (not required by shift nor room)
// Additional (not required by shift nor location)
ShiftEntry::factory(1)->create(['shift_id' => $shiftA->id]);
foreach (User::all() as $user) {
@ -78,12 +81,12 @@ class ShiftsControllerTest extends ApiBaseControllerTest
}
$request = new Request();
$request = $request->withAttribute('room_id', $room->id);
$request = $request->withAttribute('location_id', $location->id);
$controller = new ShiftsController(new Response(), $this->url);
$response = $controller->entriesByRoom($request);
$this->validateApiResponse('/rooms/{id}/shifts', 'get', $response);
$response = $controller->entriesByLocation($request);
$this->validateApiResponse('/locations/{id}/shifts', 'get', $response);
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
$this->assertJson($response->getContent());
@ -95,7 +98,7 @@ class ShiftsControllerTest extends ApiBaseControllerTest
// First shift
$shiftAData = $data['data'][0];
$this->assertEquals($shiftA->title, $shiftAData['title'], 'Title is equal');
$this->assertEquals($room->id, $shiftAData['room']['id'], 'Same room');
$this->assertEquals($location->id, $shiftAData['location']['id'], 'Same location');
$this->assertEquals($shiftA->shiftType->id, $shiftAData['shift_type']['id'], 'Shift type equals');
$this->assertCount(4, $shiftAData['entries']);
// Has users
@ -112,7 +115,7 @@ class ShiftsControllerTest extends ApiBaseControllerTest
// Second (empty) shift
$shiftBData = $data['data'][1];
$this->assertEquals($shiftB->title, $shiftBData['title'], 'Title is equal');
$this->assertEquals($room->id, $shiftBData['room']['id'], 'Same room');
$this->assertEquals($location->id, $shiftBData['location']['id'], 'Same location');
$this->assertEquals($shiftB->shiftType->id, $shiftBData['shift_type']['id'], 'Shift type equals');
$this->assertCount(2, $shiftBData['entries']);
// No users