Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SongArrangement #121

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- Request Ajax-Api ([AjaxApi-Trait](src/Requests/Traits/AjaxApi.php))
- Update Song-Arrangement ([PR121](https://github.com/5pm-HDH/churchtools-api/pull/121))

### Changed

Expand Down
19 changes: 19 additions & 0 deletions docs/out/SongAPI.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# SongAPI

## Request Song

```php
use CTApi\Models\File;
use CTApi\Requests\SongArrangementRequest;
use CTApi\Requests\SongRequest;

$allSongs = SongRequest::all();
Expand Down Expand Up @@ -128,4 +131,20 @@
// Output: "https://multitracks.com/path/to/song?id=2912&login_token=notnullapikey"


```

## Update Song-Arrangement

```php
use CTApi\Models\File;
use CTApi\Requests\SongArrangementRequest;
use CTApi\Requests\SongRequest;

$song = SongRequest::findOrFail(21);
$arrangements = $song->getArrangements();
$arrangement = end($arrangements);

$arrangement->setName("New Arrangement Title");
SongArrangementRequest::update($arrangement);

```
8 changes: 7 additions & 1 deletion docs/src/ressources/SongAPI.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# SongAPI

{{ \Tests\Unit\Docs\SongRequestTest.testExampleCode }}
## Request Song

{{ \Tests\Unit\Docs\SongRequestTest.testExampleCode }}

## Update Song-Arrangement

{{ \Tests\Unit\Docs\SongRequestTest.testUpdateArrangement }}
7 changes: 6 additions & 1 deletion src/Exceptions/CTRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public static function ofErrorResponse(ResponseInterface $response): self
$errorDescriptions = [];

foreach ($contents['errors'] as $error) {
if (!is_array($error) || !isset($error['message']) || !isset($error['messageKey'])) {
if (!is_array($error) || !isset($error['message'])) {
continue;
}

if (!isset($error['messageKey'])) {
$errorDescriptions[] = $error['message'];
continue;
}

Expand Down
18 changes: 16 additions & 2 deletions src/Models/SongArrangement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
namespace CTApi\Models;


use CTApi\Models\Interfaces\UpdatableModel;
use CTApi\Models\Traits\ExtractData;
use CTApi\Models\Traits\FillWithData;
use CTApi\Models\Traits\MetaAttribute;

class SongArrangement extends AbstractModel
class SongArrangement extends AbstractModel implements UpdatableModel
{
use FillWithData, MetaAttribute;
use FillWithData, MetaAttribute, ExtractData;

protected ?string $name = null;
protected ?bool $isDefault = null;
Expand All @@ -21,6 +23,18 @@ class SongArrangement extends AbstractModel
protected array $links = [];
protected array $files = [];

static function getModifiableAttributes(): array
{
return [
"name",
"keyOfArrangement",
"bpm",
"beat",
"duration",
"note",
];
}

protected function fillArrayType(string $key, array $data): void
{
switch ($key) {
Expand Down
15 changes: 15 additions & 0 deletions src/Requests/SongArrangementRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace CTApi\Requests;


use CTApi\Models\SongArrangement;

class SongArrangementRequest
{
public static function update(SongArrangement $songArrangement): void
{
(new SongArrangementUpdateRequest())->update($songArrangement);
}
}
31 changes: 31 additions & 0 deletions src/Requests/SongArrangementUpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace CTApi\Requests;


use CTApi\Models\SongArrangement;
use CTApi\Requests\Traits\AjaxApi;
use CTApi\Utils\CTResponseUtil;

class SongArrangementUpdateRequest
{
use AjaxApi;

public function update(SongArrangement $songArrangement): void
{
$updateAttributes = $songArrangement->getModifiableAttributes();
$allData = $songArrangement->extractData();
$updateAttributes = array_intersect_key($allData, array_flip($updateAttributes));

$this->setAjaxKeyTranslation("name", "bezeichnung");
$this->setAjaxKeyTranslation("keyOfArrangement", "tonality");
$this->setAjaxKeyTranslation("duration", "length_sec");
$updateAttributes["length_min"] = 0;
$updateAttributes["id"] = $songArrangement->getIdOrFail();

$response = $this->requestAjax("churchservice/ajax", "editArrangement", $updateAttributes);

$data = CTResponseUtil::jsonToArray($response);
}
}
45 changes: 45 additions & 0 deletions src/Requests/Traits/AjaxApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php


namespace CTApi\Requests\Traits;


use CTApi\CTClient;
use Psr\Http\Message\ResponseInterface;

trait AjaxApi
{

private array $keyTranslation = [];

protected function setAjaxKeyTranslation($modelKey, $ajaxKey)
{
$this->keyTranslation[$modelKey] = $ajaxKey;
}

/**
* @param string $ajaxQuery e.q. "churchservice/ajax"
* @param string $ajaxFunction e.q. "editArrangement"
* @param array $data model-data
* @return ResponseInterface
*/
protected function requestAjax(string $ajaxQuery, string $ajaxFunction, array $data): ResponseInterface
{
$translatedData = [];
foreach ($data as $key => $value) {
if (array_key_exists($key, $this->keyTranslation)) {
$key = $this->keyTranslation[$key];
}
$translatedData[$key] = $value;
}

$translatedData["func"] = "editArrangement";
$client = CTClient::getClient();
return $client->post('/index.php', [
"query" => [
"q" => $ajaxQuery
],
"json" => $translatedData
]);
}
}
89 changes: 89 additions & 0 deletions tests/integration/Requests/SongArrangementUpdateRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php


namespace Tests\Integration\Requests;


use CTApi\Models\SongArrangement;
use CTApi\Requests\SongArrangementRequest;
use CTApi\Requests\SongRequest;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class SongArrangementUpdateRequestTest extends TestCaseAuthenticated
{
private ?string $initName;
private ?string $initKey;
private ?string $initBpm;
private ?string $initBeat;
private ?string $initDuration;
private ?string $initNote;

private SongArrangement $arrangement;

protected function setUp(): void
{
parent::setUp();
$this->checkIfTestSuiteIsEnabled("SONG_ARRANGEMENT_UPDATE");
$this->arrangement = $this->requestSongArrangement();

$this->initName = $this->arrangement->getName();
$this->initKey = $this->arrangement->getKeyOfArrangement();
$this->initBpm = $this->arrangement->getBpm();
$this->initBeat = $this->arrangement->getBeat();
$this->initDuration = $this->arrangement->getDuration();
$this->initNote = $this->arrangement->getNote();
}

protected function tearDown(): void
{
$arrangement = $this->requestSongArrangement();
$arrangement->setName($this->initName)
->setKeyOfArrangement($this->initKey)
->setBpm($this->initBpm)
->setBeat($this->initBeat)
->setDuration($this->initDuration)
->setNote($this->initNote);
SongArrangementRequest::update($arrangement);

$arrangementReloaded = $this->requestSongArrangement();
$this->assertEquals($this->initName, $arrangementReloaded->getName());
$this->assertEquals($this->initKey, $arrangementReloaded->getKeyOfArrangement());
$this->assertEquals($this->initBpm, $arrangementReloaded->getBpm());
$this->assertEquals($this->initBeat, $arrangementReloaded->getBeat());
$this->assertEquals($this->initDuration, $arrangementReloaded->getDuration());
$this->assertEquals($this->initNote, $arrangementReloaded->getNote());
}

public function testUpdateArrangement()
{
// Update Arrangement
$this->arrangement->setName("D-Dur via CT-API")
->setKeyOfArrangement("D")
->setBpm("101")
->setBeat("6/4")
->setDuration("140")
->setNote("Hello Test Note");

SongArrangementRequest::update($this->arrangement);

$this->arrangement = $this->requestSongArrangement();

$this->assertEquals("D-Dur via CT-API", $this->arrangement->getName());
$this->assertEquals("D", $this->arrangement->getKeyOfArrangement());
$this->assertEquals("101", $this->arrangement->getBpm());
$this->assertEquals("6/4", $this->arrangement->getBeat());
$this->assertEquals("140", $this->arrangement->getDuration());
$this->assertEquals("Hello Test Note", $this->arrangement->getNote());
}

private function requestSongArrangement(): SongArrangement
{
$songId = TestData::getValueAsInteger("SONG_ARRANGEMENT_UPDATE_SONG_ID");
$song = SongRequest::findOrFail($songId);
$arrangements = $song->getArrangements();
$this->assertNotEmpty($arrangements);
$arrangement = end($arrangements);
return $arrangement;
}
}
6 changes: 5 additions & 1 deletion tests/integration/testdata.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,8 @@ FILE_EVENT_ID = 40
# enable SEARCH_PERSON testsuite with YES and siable with NO
SEARCH_PERSON = YES
SEARCH_PERSON_QUERY = john
SEARCH_PERSON_LAST_NAME = Doe
SEARCH_PERSON_LAST_NAME = Doe

# enable SONG_ARRANGEMENT_UPDATE testsuite with YES and siable with NO
SONG_ARRANGEMENT_UPDATE = YES
SONG_ARRANGEMENT_UPDATE_SONG_ID = 24
14 changes: 14 additions & 0 deletions tests/unit/Docs/SongRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use CTApi\Models\File;
use CTApi\Requests\SongArrangementRequest;
use CTApi\Requests\SongRequest;
use Tests\Unit\TestCaseHttpMocked;

Expand Down Expand Up @@ -83,4 +84,17 @@ public function testExampleCode()
$this->assertEquals(["id" => "2912"], $customFile->getFileUrlQueryParameters());
$this->assertEquals("https://multitracks.com/path/to/song?id=2912&login_token=notnullapikey", $customFile->getFileUrlAuthenticated());
}

/**
* @doesNotPerformAssertions
*/
public function testUpdateArrangement()
{
$song = SongRequest::findOrFail(21);
$arrangements = $song->getArrangements();
$arrangement = end($arrangements);

$arrangement->setName("New Arrangement Title");
SongArrangementRequest::update($arrangement);
}
}