Skip to content

Commit

Permalink
feat(note): add note request and replace song-comment #215
Browse files Browse the repository at this point in the history
  • Loading branch information
DumbergerL committed Jul 31, 2024
1 parent 9a8088f commit 6cf0194
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 22 deletions.
66 changes: 66 additions & 0 deletions src/Models/Common/Note/Note.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace CTApi\Models\Common\Note;

use CTApi\Models\AbstractModel;
use CTApi\Models\Common\Domain\Meta;
use CTApi\Traits\Model\FillWithData;
use CTApi\Traits\Model\MetaAttribute;

class Note extends AbstractModel
{
use FillWithData;
use MetaAttribute;

protected ?string $domainId = null;
protected ?string $domainType = null;
protected ?string $text = null;

protected function fillArrayType(string $key, array $data): void
{
switch ($key) {
case "meta":
$this->setMeta(Meta::createModelFromData($data));
break;
}
}

public function setId(?string $id): Note
{
$this->id = $id;
return $this;
}

public function getDomainId(): ?string
{
return $this->domainId;
}

public function setDomainId(?string $domainId): Note
{
$this->domainId = $domainId;
return $this;
}

public function getDomainType(): ?string
{
return $this->domainType;
}

public function setDomainType(?string $domainType): Note
{
$this->domainType = $domainType;
return $this;
}

public function getText(): ?string
{
return $this->text;
}

public function setText(?string $text): Note
{
$this->text = $text;
return $this;
}
}
62 changes: 62 additions & 0 deletions src/Models/Common/Note/NoteRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace CTApi\Models\Common\Note;

class NoteRequest
{
public static function forAvatar(int $avatarId): NoteRequestBuilder
{
return new NoteRequestBuilder("avatar", $avatarId);
}

public static function forGroup(int $groupId): NoteRequestBuilder
{
return new NoteRequestBuilder("group", $groupId);
}

public static function forLogo(int $logoId): NoteRequestBuilder
{
return new NoteRequestBuilder("logo", $logoId);
}

public static function forAttachments(int $attachmentId): NoteRequestBuilder
{
return new NoteRequestBuilder("attachments", $attachmentId);
}

public static function forBulkletterTemplate(int $bulkletterTemplateId): NoteRequestBuilder
{
return new NoteRequestBuilder("bulkletter_template", $bulkletterTemplateId);
}

public static function forService(int $serviceId): NoteRequestBuilder
{
return new NoteRequestBuilder("service", $serviceId);
}

public static function forSongArrangement(int $songArrangementId): NoteRequestBuilder
{
return new NoteRequestBuilder("song_arrangement", $songArrangementId);
}

public static function forImportTable(int $importTableId): NoteRequestBuilder
{
return new NoteRequestBuilder("importtable", $importTableId);
}

public static function forPerson(int $personId): NoteRequestBuilder
{
return new NoteRequestBuilder("person", $personId);
}

public static function forFamilyAvatar(int $familyAvatarId): NoteRequestBuilder
{
return new NoteRequestBuilder("familyavatar", $familyAvatarId);
}

public static function forWiki(int $wikiId): NoteRequestBuilder
{
// TODO: Muss unbedingt testen. Hier kann es glaub zu unterschiedlichen Implementierung führen: wiki_??
return new NoteRequestBuilder("wiki", $wikiId);
}
}
75 changes: 75 additions & 0 deletions src/Models/Common/Note/NoteRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace CTApi\Models\Common\Note;

use CTApi\CTClient;
use CTApi\Utils\CTResponseUtil;

class NoteRequestBuilder
{

public function __construct(protected string $domainType, protected int $domainIdentifier)
{
}

protected function getApiEndpoint(): string
{
return "/api/notes/" . $this->domainType . "/" . $this->domainIdentifier;
}


/**
* @return Note[]
*/
public function get(): array
{
$ctClient = CTClient::getClient();
$response = $ctClient->get($this->getApiEndpoint());
$data = CTResponseUtil::dataAsArray($response);
if (empty($data)) {
return [];
} else {
return Note::createModelsFromArray($data);
}
}

public function delete(int $noteId): void
{
$ctClient = CTClient::getClient();
$ctClient->delete($this->getApiEndpoint() . "/" . $noteId);
}

public function create(string $text, ?int $securityLevelId = null): ?Note
{
$ctClient = CTClient::getClient();
$response = $ctClient->post($this->getApiEndpoint(), [
"domainId" => "" . $this->domainIdentifier,
"domainType" => $this->domainType,
"securityLevelId" => $securityLevelId,
"text" => $text,
]);

$data = CTResponseUtil::dataAsArray($response);
if (empty($data)) {
return null;
} else {
return Note::createModelFromData($data);
}
}

public function update(int $noteId, string $text, ?int $securityLevelId = null): ?Note
{
$ctClient = CTClient::getClient();
$response = $ctClient->put($this->getApiEndpoint() . '/' . $noteId, [
"text" => $text,
"securityLevelId" => $securityLevelId,
]);

$data = CTResponseUtil::dataAsArray($response);
if (empty($data)) {
return null;
} else {
return Note::createModelFromData($data);
}
}
}
17 changes: 16 additions & 1 deletion src/Models/Events/Song/SongComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
use CTApi\Models\Common\Domain\Meta;
use CTApi\Models\Groups\Person\Person;
use CTApi\Traits\Model\FillWithData;
use CTApi\Traits\Model\MetaAttribute;

/**
* @deprecated Use Note instead. This class will be removed in the next major-release v3.
*/
class SongComment extends AbstractModel
{
use FillWithData;
use MetaAttribute;

private ?string $domainId = null;
private ?string $domainType = null;
private ?string $text = null;
private ?Meta $meta = null;

protected function fillNonArrayType(string $key, $value): void
{
Expand Down Expand Up @@ -42,6 +46,17 @@ protected function fillNonArrayType(string $key, $value): void
}
}

protected function fillArrayType(string $key, array $data): void
{
switch ($key) {
case "meta":
$this->meta = Meta::createModelFromData($data);
break;
default:
$this->fillDefault($key, $data);
}
}

public function getDomainId(): ?string
{
return $this->domainId;
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Events/Song/SongCommentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace CTApi\Models\Events\Song;

/**
* @deprecated Use NoteRequest::forSongArrangement() instead. This class will be removed in the next major-release v3.
*/
class SongCommentRequest
{
public static function getForSongArrangement(int $arrangementId): array
Expand Down
30 changes: 14 additions & 16 deletions src/Models/Events/Song/SongCommentRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace CTApi\Models\Events\Song;

use CTApi\CTClient;
use CTApi\Models\Common\Note\NoteRequest;
use CTApi\Traits\Request\AjaxApi;
use CTApi\Utils\CTResponseUtil;

/**
* @deprecated Use NoteRequest::forSongArrangement() instead. This class will be removed in the next major-release v3.
*/
class SongCommentRequestBuilder
{
use AjaxApi;
Expand All @@ -16,29 +20,23 @@ public function __construct(

public function getComments()
{
$response = $this->requestAjax("churchservice/ajax", "getComments", [
"domain_type" => "arrangement",
"domain_id" => $this->arrangementId
]);

$ctClient = CTClient::getClient();
$response = $ctClient->get( "/api/notes/song_arrangement/" . $this->arrangementId );
$data = CTResponseUtil::dataAsArray($response);
return SongComment::createModelsFromArray(array_values($data));
if (empty($data)) {
return [];
} else {
return SongComment::createModelsFromArray($data);
}
}

public function createComment(string $text): void
{
$this->requestAjax("churchservice/ajax", "addComment", [
"domain_type" => "arrangement",
"domain_id" => $this->arrangementId,
"text" => $text
]);
NoteRequest::forSongArrangement($this->arrangementId)->create($text);
}

public function deleteComment(int $commentId): void
{
$this->requestAjax("churchservice/ajax", "delComment", [
"id" => $commentId,
]);
NoteRequest::forSongArrangement($this->arrangementId)->delete($commentId);
}

}
71 changes: 71 additions & 0 deletions tests/Integration/Requests/NoteRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace CTApi\Test\Integration\Requests;

use CTApi\CTConfig;
use CTApi\Models\Common\Auth\CSRFTokenRequest;
use CTApi\Models\Common\Note\NoteRequest;
use CTApi\Test\Integration\TestCaseAuthenticated;

class NoteRequestTest extends TestCaseAuthenticated
{

public function testNoteForGroup()
{
$data = NoteRequest::forGroup(17)->get();

CTConfig::enableDebugging();

foreach($data as $note)
{
echo "=> " . $note->getText() . "\n";
NoteRequest::forGroup(17)->delete($note->getIdAsInteger());
}
}

public function testCreateNoteOnGroup()
{
CTConfig::enableDebugging();

$data = NoteRequest::forGroup(17)->create("Test String", 1);
print_r($data);

}
public function testCreateNotOnGroup_DirectWithCURL()
{
$csrfToken = CSRFTokenRequest::getOrFail();

$ch = curl_init();

$options = [
CURLOPT_URL => CTConfig::getApiUrl() . '/api/notes/group/17',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
"domainId" => "" . 17,
"domainType" => "group",
"securityLevelId" => 1,
"text" => "AawdionAWd",
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER=>array('Content-Type:application/json',
"csrf-token:" . $csrfToken
)
];

curl_setopt_array($ch, $options);

$cookie = CTConfig::getSessionCookie();
print_r([$cookie["Name"], $cookie["Value"]]);
if ($cookie != null) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie["Name"] . '=' . $cookie["Value"]);
}

$resp = curl_exec($ch);

$curlInfo = curl_getinfo($ch);
var_dump($curlInfo);

var_dump($resp);
}

}
1 change: 1 addition & 0 deletions tests/Integration/Requests/SongCommentRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CTApi\Test\Integration\Requests;

use CTApi\Models\Events\Song\SongArrangementRequest;
use CTApi\Models\Events\Song\SongCommentRequest;
use CTApi\Test\Integration\IntegrationTestData;
use CTApi\Test\Integration\TestCaseAuthenticated;
Expand Down
Loading

0 comments on commit 6cf0194

Please sign in to comment.