From 497c1772f7be12e7ef4b5587237d9369b52235da Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 3 Nov 2023 19:03:31 +0100 Subject: [PATCH] API: Rename rooms to locations and start/end to starts_at/ends_at --- config/routes.php | 4 +- resources/api/openapi.yml | 32 ++++++------- ...Controller.php => LocationsController.php} | 10 ++-- src/Controllers/Api/ShiftsController.php | 30 ++++++------ ...erTest.php => LocationsControllerTest.php} | 15 +++--- .../Controllers/Api/ShiftsControllerTest.php | 47 ++++++++++--------- 6 files changed, 70 insertions(+), 68 deletions(-) rename src/Controllers/Api/{RoomsController.php => LocationsController.php} (55%) rename tests/Unit/Controllers/Api/{RoomsControllerTest.php => LocationsControllerTest.php} (63%) diff --git a/config/routes.php b/config/routes.php index 352f5bf6..451fd128 100644 --- a/config/routes.php +++ b/config/routes.php @@ -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'], diff --git a/resources/api/openapi.yml b/resources/api/openapi.yml index 4bd69998..9b9023ee 100644 --- a/resources/api/openapi.yml +++ b/resources/api/openapi.yml @@ -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 diff --git a/src/Controllers/Api/RoomsController.php b/src/Controllers/Api/LocationsController.php similarity index 55% rename from src/Controllers/Api/RoomsController.php rename to src/Controllers/Api/LocationsController.php index b19a5dc1..b27f6319 100644 --- a/src/Controllers/Api/RoomsController.php +++ b/src/Controllers/Api/LocationsController.php @@ -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]; diff --git a/src/Controllers/Api/ShiftsController.php b/src/Controllers/Api/ShiftsController.php index 91f2d87e..082e8e5a 100644 --- a/src/Controllers/Api/ShiftsController.php +++ b/src/Controllers/Api/ShiftsController.php @@ -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; } diff --git a/tests/Unit/Controllers/Api/RoomsControllerTest.php b/tests/Unit/Controllers/Api/LocationsControllerTest.php similarity index 63% rename from tests/Unit/Controllers/Api/RoomsControllerTest.php rename to tests/Unit/Controllers/Api/LocationsControllerTest.php index af0375dd..6cc6a6e7 100644 --- a/tests/Unit/Controllers/Api/RoomsControllerTest.php +++ b/tests/Unit/Controllers/Api/LocationsControllerTest.php @@ -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()); diff --git a/tests/Unit/Controllers/Api/ShiftsControllerTest.php b/tests/Unit/Controllers/Api/ShiftsControllerTest.php index 0c980eaf..05d837d3 100644 --- a/tests/Unit/Controllers/Api/ShiftsControllerTest.php +++ b/tests/Unit/Controllers/Api/ShiftsControllerTest.php @@ -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