*
* Gib "irgendöpis" mit dem {@code echo} command aus.
*
*
* Ändere die Antwort nun zu einem gültigen json objekt {"hello" => "world"}. Übergebe dafür ein array der
* Methode {@see $this->sendJson()}.
*
*
* Implementiere und nutze die Methode {@see ArticleDao::getArticleById()}, um das entsprechende Artikel-Objekt
* aus der Datenbank zu lesen und gebe diese anschliessend im JSON Format zurück.
*
* Das Article-Objekt kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
* generieren.
*
*
* Kann der Artikel nicht gefunden werden, werfe eine {@link PageNotFoundException}.
*
*
*
* @param int $articleId
* @return void
* @throws PageNotFoundException if Article could not be found.
*/
function getDoArticle(int $articleId): void {
$article = $this->articleDao->getArticleById($articleId);
if($article === null) {
throw new PageNotFoundException('Article not found with ID:' . $articleId);
}
if($article->getId() === 2) {
throw new PageNotFoundException('Article is Secret:' . $articleId);
}
$this->sendJson($article);
}
/**
* Diese Methode kannst du im Browser testen. Pfad: localhost/[ordner name vom projekt]/src-php/public/api/articles
*
*
* -
* Implementiere und nutze die Methode {@see ArticleDao::getArticles()}, um Artikel aus der Datenbank zu lesen
* und gebe diese anschliessend im JSON Format zurück.
*
* Article-Objekte kannst du einfach {@see $this->sendJson()} übergeben, um einen validen JSON-Response zu
* generieren.
*
* -
* Wird ein $categoryName übergeben, gebe nur die Artikel aus, die über diesen Kategorienamen verfügen.
*
*
*
* @param string|null $categoryName
* @return void
*/
function getDoArticles(?string $categoryName = null): void {
if ($categoryName === null) {
$articles = $this->articleDao->getArticles();
if($articles === []) {
throw new PageNotFoundException('No articles found');
}
} else {
$articles = $this->articleDao->getArticlesByCategoryName($categoryName);
if($articles === []) {
throw new PageNotFoundException('Category ' . $categoryName . ' not found');
}
}
$this->sendJson($articles);
}
/**
* Speichere einen Artikel.
*
*
* -
* Der Kategoriename darf nur 'international','national' oder 'sport' sein.
* Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
*
* -
* Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
*
* -
* Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
*
*
*
* Nenne die Methode {@see saveArticle(Aŋ@rticle $article)}
*
* @throws StatusException
*/
function badRequestExceptionIfs(ParamBody $body, ?Article $article = null): Article {
$bodyData = $body->parseJson();
if($body->getValue() === "") {
throw new BadRequestException('Body cant be empty');
}
$bracketStart = '/^{/';
$bracketEnd = '/}$/';
$spaceStart = '/^\s/';
$spaceEnd = '/\s$/';
if (preg_match($bracketEnd, $body) == 0 || preg_match($bracketStart, $body) == 0) {
throw new BadRequestException('Json is invalid');
}
if ($article === null) {
if (!isset($bodyData['title'])) {
throw new BadRequestException('Title must be set');
}
if (!isset($bodyData['categoryName'])) {
throw new BadRequestException('The Category must be international, national or sport');
}
if(!isset($bodyData['text'])) {
throw new BadRequestException('Article must contain a text');
}
}
if (isset($bodyData['text'])) {
if (strlen($bodyData['text']) > 12000) {
throw new BadRequestException('Text is too long');
}
if (strlen($bodyData['text']) == 0) {
throw new BadRequestException('Text is too short');
}
if (preg_match($spaceStart, $bodyData['text']) == 1 || preg_match($spaceEnd, $bodyData['text']) == 1) {
throw new BadRequestException('Text cant start or end with a whitespace character');
}
}
if (isset($bodyData['title'])) {
if (strlen($bodyData['title']) > 255) {
throw new BadRequestException('Title is too long');
}
if (strlen($bodyData['title']) == 0) {
throw new BadRequestException('Title is too short');
}
if (preg_match($spaceStart, $bodyData['title']) == 1 || preg_match($spaceEnd, $bodyData['title']) == 1) {
throw new BadRequestException('Title cant start or end with a whitespace character');
}
}
if (isset($bodyData['categoryName']) && !in_array($bodyData['categoryName'], ['sport','international','national'])) {
throw new BadRequestException('The Category must be international, national or sport');
}
if ($article === null) {
$article = new Article();
}
if (isset($bodyData['categoryName'])) {
$article->setCategoryName($bodyData['categoryName']);
}
if (isset($bodyData['text'])) {
$article->setText($bodyData['text']);
}
if (isset($bodyData['title'])) {
$article->setTitle($bodyData['title']);
}
return $article;
}
/**
* @throws StatusException
*/
function postDoArticle(Parambody $body): void {
$this->beginTransaction();
$article = $this->badRequestExceptionIfs($body, null);
$this->articleDao->saveArticle($article);
$this->commit();
$this->sendJson($article);
}
/**
* @throws PageNotFoundException
* @throws StatusException
*/
function putDoArticle(int $articleId, Parambody $body): void {
$this->beginTransaction();
$article = $this->articleDao->getArticleById($articleId);
if ($article === null) {
throw new PageNotFoundException('Article not found');
}
$this->badRequestExceptionIfs($body, $article);
$this->commit();
$this->sendJson($article);
}
/**
* Editiere einen Artikel.
*
*
* -
* Der Kategoriename darf nur 'international','national' oder 'sport' sein.
* Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
*
* -
* Finde den dazugehörigen {@see Article} und passe die Daten mit denjenigen Daten von gestern ab.
*
* -
* Implementiere eine neue Methode im {@see ArticleDao} und nenne die Methode {@see saveArticle(Article $article)}.
*
*
*
* @return void
*/
/**
* Löscht den {@see Article} mit der dazugehörigen Id.
*
*
* - Mache eine neue Methode im {@see ArticleDao} und benenne sie "removeArticle(int articleId)
*
*
* @return void
*/
function deleteDoArticle(int $articleId): void {
$this->beginTransaction();
$this->articleDao->removeArticle($articleId);
$this->commit();
}
}