|
@@ -9,6 +9,7 @@ use internship\bo\Article;
|
|
|
use n2n\validation\plan\ErrorMap;
|
|
use n2n\validation\plan\ErrorMap;
|
|
|
use n2n\web\http\BadRequestException;
|
|
use n2n\web\http\BadRequestException;
|
|
|
use n2n\web\http\PageNotFoundException;
|
|
use n2n\web\http\PageNotFoundException;
|
|
|
|
|
+use n2n\web\http\StatusException;
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -102,92 +103,99 @@ class ArticleApiController extends ControllerAdapter {
|
|
|
* Speichere einen Artikel.
|
|
* Speichere einen Artikel.
|
|
|
*
|
|
*
|
|
|
* <ul>
|
|
* <ul>
|
|
|
- * <li>
|
|
|
|
|
- * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
|
|
|
|
|
- * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
|
|
|
|
|
- * </li>
|
|
|
|
|
- * <li>
|
|
|
|
|
- * Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
|
|
|
|
|
- * </li>
|
|
|
|
|
|
|
+ * <li>
|
|
|
|
|
+ * Der Kategoriename darf nur 'international','national' oder 'sport' sein.
|
|
|
|
|
+ * Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
|
|
|
|
|
+ * </li>
|
|
|
|
|
+ * <li>
|
|
|
|
|
+ * Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
|
|
|
|
|
+ * </li>
|
|
|
* <li>
|
|
* <li>
|
|
|
- * Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
|
|
|
|
|
- * </li>
|
|
|
|
|
|
|
+ * Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
|
|
|
|
|
+ * </li>
|
|
|
* </ul>
|
|
* </ul>
|
|
|
*
|
|
*
|
|
|
* Nenne die Methode {@see saveArticle(Aŋ@rticle $article)}
|
|
* Nenne die Methode {@see saveArticle(Aŋ@rticle $article)}
|
|
|
*
|
|
*
|
|
|
* @return void
|
|
* @return void
|
|
|
|
|
+ * @throws StatusException
|
|
|
*/
|
|
*/
|
|
|
- function badRequestExceptionIfs(ParamBody $body, $articleId = null) {
|
|
|
|
|
- $this->beginTransaction();
|
|
|
|
|
- $article = $this->articleDao->getArticleById($articleId);
|
|
|
|
|
|
|
+ function badRequestExceptionIfs(ParamBody $body, ?Article $article = null): Article {
|
|
|
$bodyData = $body->parseJson();
|
|
$bodyData = $body->parseJson();
|
|
|
if($body->getValue() === "") {
|
|
if($body->getValue() === "") {
|
|
|
- throw new BadRequestException('Body cant be empty');
|
|
|
|
|
- }
|
|
|
|
|
- $pattern = '/^{/';
|
|
|
|
|
- $pattern2 = '/}$/';
|
|
|
|
|
-
|
|
|
|
|
- if(preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
|
|
|
|
|
- throw new BadRequestException('Json is invalid');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(!isset($bodyData['title'])) {
|
|
|
|
|
- throw new BadRequestException('Title must be set');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (strlen($bodyData['title']) > 255){
|
|
|
|
|
- throw new BadRequestException('Title is too long');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ throw new BadRequestException('Body cant be empty');
|
|
|
|
|
+ }
|
|
|
|
|
+ $pattern = '/^{/';
|
|
|
|
|
+ $pattern2 = '/}$/';
|
|
|
|
|
|
|
|
- if (!isset($bodyData['categoryName'])) {
|
|
|
|
|
- throw new BadRequestException('The Category must be international, national or sport');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
|
|
|
|
|
+ throw new BadRequestException('Json is invalid');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
|
|
|
|
|
- throw new BadRequestException('The Category must be international, national or sport');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!isset($bodyData['title'])) {
|
|
|
|
|
+ throw new BadRequestException('Title must be set');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if(!isset($bodyData['text'])) {
|
|
|
|
|
- throw new BadRequestException('Article must contain a text');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (strlen($bodyData['title']) > 255) {
|
|
|
|
|
+ throw new BadRequestException('Title is too long');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (strlen($bodyData['text']) > 12000){
|
|
|
|
|
- throw new BadRequestException('Text is too long');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!isset($bodyData['categoryName'])) {
|
|
|
|
|
+ throw new BadRequestException('The Category must be international, national or sport');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
|
|
|
|
|
+ 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($articleId)) {
|
|
|
|
|
|
|
+ if (strlen($bodyData['text']) > 12000){
|
|
|
|
|
+ throw new BadRequestException('Text is too long');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if($article === null) {
|
|
|
|
|
- throw new PageNotFoundException('Article not found');
|
|
|
|
|
- }
|
|
|
|
|
- $article->setCategoryName($bodyData['categoryName']);
|
|
|
|
|
- $article->setText($bodyData['text']);
|
|
|
|
|
- $article->setTitle($bodyData['title']);
|
|
|
|
|
- $this->articleDao->saveArticle($article);
|
|
|
|
|
- $this->commit();
|
|
|
|
|
- $this->sendJson($article);
|
|
|
|
|
-
|
|
|
|
|
- } else {
|
|
|
|
|
|
|
+ if ($article === null) {
|
|
|
$article = new Article();
|
|
$article = new Article();
|
|
|
- $article->setTitle($bodyData['title']);
|
|
|
|
|
- $article->setText($bodyData['text']);
|
|
|
|
|
- $article->setCategoryName($bodyData['categoryName']);
|
|
|
|
|
- $this->articleDao->saveArticle($article);
|
|
|
|
|
- $this->commit();
|
|
|
|
|
- $this->sendJson($article);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ $article->setCategoryName($bodyData['categoryName']);
|
|
|
|
|
+ $article->setText($bodyData['text']);
|
|
|
|
|
+ $article->setTitle($bodyData['title']);
|
|
|
|
|
+
|
|
|
|
|
+ return $article;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function postDoArticle(Parambody $body) {
|
|
|
|
|
- $this->badRequestExceptionIfs($body);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @throws StatusException
|
|
|
|
|
+ */
|
|
|
|
|
+ function postDoArticle(Parambody $body): void {
|
|
|
|
|
+ $this->beginTransaction();
|
|
|
|
|
+
|
|
|
|
|
+ $article = $this->badRequestExceptionIfs($body, null);
|
|
|
|
|
+ $this->articleDao->saveArticle($article);
|
|
|
|
|
+ $this->commit();
|
|
|
|
|
+
|
|
|
|
|
+ $this->sendJson($article);
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function putDoArticle(Parambody $body, int $articleId) {
|
|
|
|
|
- $this->badRequestExceptionIfs($body, $articleId);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @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);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|