Add Course DTO

This commit is contained in:
philipp lang 2022-02-23 22:18:11 +01:00
parent 220187bef4
commit e400315cf6
5 changed files with 112 additions and 21 deletions

View File

@ -9,9 +9,11 @@
}
],
"require": {
"php": "8.1.*",
"guzzlehttp/guzzle": "^6.3.1|^7.0",
"laravel/framework": "^8.0",
"laravel/ui": "^3.4"
"laravel/ui": "^3.4",
"spatie/data-transfer-object": "^3.7"
},
"autoload": {
"psr-4": {

71
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c08ae38275555b5d4c8569acc8f033a1",
"content-hash": "91a283d3422069c7fa3a1b13a0850bbf",
"packages": [
{
"name": "brick/math",
@ -2509,6 +2509,69 @@
],
"time": "2021-09-25T23:10:38+00:00"
},
{
"name": "spatie/data-transfer-object",
"version": "3.7.3",
"source": {
"type": "git",
"url": "https://github.com/spatie/data-transfer-object.git",
"reference": "341f72c77e0fce40ea2e0dcb212cb54dc27bbe72"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/data-transfer-object/zipball/341f72c77e0fce40ea2e0dcb212cb54dc27bbe72",
"reference": "341f72c77e0fce40ea2e0dcb212cb54dc27bbe72",
"shasum": ""
},
"require": {
"php": "^8.0"
},
"require-dev": {
"illuminate/collections": "^8.36",
"jetbrains/phpstorm-attributes": "^1.0",
"larapack/dd": "^1.1",
"phpunit/phpunit": "^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\DataTransferObject\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Brent Roose",
"email": "brent@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Data transfer objects with batteries included",
"homepage": "https://github.com/spatie/data-transfer-object",
"keywords": [
"data-transfer-object",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/data-transfer-object/issues",
"source": "https://github.com/spatie/data-transfer-object/tree/3.7.3"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2021-12-30T20:31:10+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.3.0",
@ -9046,7 +9109,9 @@
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform": {
"php": "8.1.*"
},
"platform-dev": [],
"plugin-api-version": "2.1.0"
"plugin-api-version": "2.2.0"
}

View File

@ -14,6 +14,7 @@ use Illuminate\Support\Str;
use Log;
use Zoomyboy\LaravelNami\Authentication\Authenticator;
use Zoomyboy\LaravelNami\Concerns\IsNamiMember;
use Zoomyboy\LaravelNami\Data\Course;
use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException;
use Zoomyboy\LaravelNami\Exceptions\RightException;
use Zoomyboy\LaravelNami\NamiException;
@ -198,27 +199,26 @@ class Api {
$this->assertLoggedIn();
return $this->fetchCollection('/ica/rest/module/baustein', 'Fetch courses failed')
->map(fn ($course) => (object) ['name' => $course['descriptor'], 'id' => $course['id']]);
->map(fn ($course) => (object) ['id' => $course['id']]);
}
/**
* @return Collection<Course>
*/
public function coursesFor(int $memberId): Collection
{
$this->assertLoggedIn();
return $this->fetchCollection(
"/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/flist",
'Courses fetch failed'
)->map(function ($course) use ($memberId) {
$single = $this->fetchData("/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/{$course['id']}", "Error fetching single course");
return $this->fetchCollection("/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/flist", 'Courses fetch failed')
->map(fn ($course) => $this->course($memberId, $course['id']))
->filter(fn ($course) => $course !== null);
}
return $single ? (object) [
'id' => $single['id'],
'organizer' => $single['veranstalter'],
'course_id' => $single['bausteinId'],
'event_name' => $single['vstgName'],
'completed_at' => $single['vstgTag'],
] : null;
})->filter(fn ($course) => $course !== null);
public function course(int $memberId, int $courseId): ?Course
{
$single = $this->fetchData("/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/{$courseId}", "Error fetching single course");
return $single ? new Course($single) : null;
}
/**

24
src/Data/Course.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Zoomyboy\LaravelNami\Data;
use Spatie\DataTransferObject\Attributes\MapFrom;
use Spatie\DataTransferObject\DataTransferObject;
class Course extends DataTransferObject {
public int $id;
#[MapFrom('bausteinId')]
public string $courseId;
#[MapFrom('veranstalter')]
public string $organizer;
#[MapFrom('vstgName')]
public string $eventName;
#[MapFrom('vstgTag')]
public string $completedAt;
}

View File

@ -38,9 +38,9 @@ class CourseTest extends TestCase
$this->assertEquals(788, $course->id);
$this->assertEquals('KJA', $course->organizer);
$this->assertEquals(506, $course->course_id);
$this->assertEquals('eventname', $course->event_name);
$this->assertEquals('2021-11-12 00:00:00', $course->completed_at);
$this->assertEquals(506, $course->courseId);
$this->assertEquals('eventname', $course->eventName);
$this->assertEquals('2021-11-12 00:00:00', $course->completedAt);
app(CourseFake::class)->assertFetched(11111);
app(CourseFake::class)->assertFetchedSingle(11111, 788);