Commit 49f437c0 authored by nic's avatar nic
Browse files

feat: add getting multiple comments, articles and topics

parent 1c581e40
1 merge request!1Первая версия API
Showing with 226 additions and 1 deletion
+226 -1
......@@ -185,4 +185,24 @@ Content-Type: application/json
"body": "Very interesting!",
"created": "2024-01-22 21:30:00",
"changed": "2024-01-22 21:30:00"
}
\ No newline at end of file
}
### GET MY TOPICS
GET http://localhost:8800/topic/my
### GET USER TOPICS
GET http://localhost:8800/topic/user/2
### GET MY COMMENTS
GET http://localhost:8800/comment/my
### GET USER COMMENTS
GET http://localhost:8800/comment/user/2
### GET MY ARTICLES
GET http://localhost:8800/article/my
### GET USER ARTICLES
GET http://localhost:8800/article/user/2
......@@ -6,7 +6,9 @@ use App\Entity\Article;
use App\Entity\User;
use App\Model\ArticleModel;
use App\Model\ArticleResponseModel;
use App\Model\MultipleArticlesModel;
use App\Service\ArticleService;
use App\Service\UserProvider;
use Kaa\Component\DependencyInjectionDecorator\Inject;
use Kaa\Component\HttpMessage\Response\JsonResponse;
use Kaa\Component\HttpMessage\Response\Response;
......@@ -78,4 +80,28 @@ class ArticleController
return new ForbiddenResponse();
}
#[Get('/my')]
#[AsJsonResponse]
public function getCurrentUserArticles(
#[CurrentUser]
User $user,
#[Inject]
ArticleService $articleService
): MultipleArticlesModel
{
return $articleService->getUserArticles($user->getIdentifier());
}
#[Get('/user/{id}')]
#[AsJsonResponse]
public function getUserArticles(
#[MapRouteParameter]
int $id,
#[Inject]
ArticleService $articleService
): MultipleArticlesModel
{
return $articleService->getUserArticles((string)$id);
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ namespace App\Controller;
use App\Entity\User;
use App\Model\CommentModel;
use App\Model\MultipleCommentModel;
use App\Service\CommentService;
use Exception;
use Kaa\Component\DependencyInjectionDecorator\Inject;
......@@ -80,4 +81,29 @@ class CommentController
}
return new ForbiddenResponse();
}
#[Get('/my')]
#[AsJsonResponse]
public function getCurrentUserComments(
#[CurrentUser]
?User $user,
#[Inject]
CommentService $commentService
): MultipleCommentModel
{
return $commentService->getUserComments($user->getIdentifier());
}
#[Get('/user/{id}')]
#[AsJsonResponse]
public function getUserComments(
#[MapRouteParameter]
int $id,
#[Inject]
CommentService $commentService
): MultipleCommentModel
{
return $commentService->getUserComments((string)$id);
}
}
......@@ -2,6 +2,8 @@
namespace App\Controller;
use App\Entity\User;
use App\Model\MultipleTopicsModel;
use App\Model\TopicModel;
use App\Service\TopicService;
use Kaa\Component\DependencyInjectionDecorator\Inject;
......@@ -12,6 +14,7 @@ use Kaa\Component\RequestMapperDecorator\MapRouteParameter;
use Kaa\Component\Router\Attribute\Get;
use Kaa\Component\Router\Attribute\Post;
use Kaa\Component\Router\Attribute\Put;
use Kaa\Component\SecurityDecorator\CurrentUser;
use Throwable;
class TopicController
......@@ -57,4 +60,28 @@ class TopicController
}
return new JsonResponse("Successful changed", status: 200);
}
#[Get('/topic/my')]
#[AsJsonResponse]
public function getCurrentUserTopics(
#[CurrentUser]
?User $user,
#[Inject]
TopicService $topicService
): MultipleTopicsModel
{
return $topicService->getUserTopics($user->getIdentifier());
}
#[Get('/topic/user/{id}')]
#[AsJsonResponse]
public function getUserTopics(
#[MapRouteParameter]
int $id,
#[Inject]
TopicService $topicService
): MultipleTopicsModel
{
return $topicService->getUserTopics((string)$id);
}
}
<?php
namespace App\Model;
/** @kphp-json flatten */
class MultipleArticlesModel
{
/** @var ArticleResponseModel[] */
private array $articles;
/**
* @param ArticleResponseModel[] $articles
*/
public function __construct(array $articles)
{
$this->articles= $articles;
}
/**
* @return ArticleResponseModel[]
*/
public function getTopics(): array
{
return $this->articles;
}
}
\ No newline at end of file
<?php
namespace App\Model;
/** @kphp-json flatten */
class MultipleCommentModel
{
/** @var CommentModel[] */
private array $comments;
/**
* @param CommentModel[] $comments
*/
public function __construct(array $comments)
{
$this->comments = $comments;
}
/**
* @return CommentModel[]
*/
public function getTopics(): array
{
return $this->comments;
}
}
<?php
namespace App\Model;
/** @kphp-json flatten */
class MultipleTopicsModel
{
/** @var TopicModel[] */
private array $topics;
/**
* @param TopicModel[] $topics
*/
public function __construct(array $topics)
{
$this->topics = $topics;
}
/**
* @return TopicModel[]
*/
public function getTopics(): array
{
return $this->topics;
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ use App\Entity\User;
use App\Model\ArticleModel;
use App\Model\ArticleResponseModel;
use App\Model\CommentModel;
use App\Model\MultipleArticlesModel;
use App\Model\TagModel;
use App\Model\TopicModel;
use App\Model\UserModel;
......@@ -122,4 +123,15 @@ class ArticleService
}
$this->entityManager->flush();
}
public function getUserArticles(string $id)
{
$articles = $this->entityManager->findBy(Article::class, ['author' => $id]);
$articleModels = [];
foreach ($articles as $article){
$articleModels[] = $this->getArticle($article->getId());
}
return new MultipleArticlesModel($articleModels);
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ use App\Entity\Article;
use App\Entity\Comment;
use App\Entity\User;
use App\Model\CommentModel;
use App\Model\MultipleCommentModel;
use DateTimeImmutable;
use Exception;
use Kaa\Component\Database\EntityManager\EntityManagerInterface;
......@@ -63,4 +64,21 @@ class CommentService
$this->entityManager->flush();
}
public function getUserComments(string $id)
{
$comments = $this->entityManager->findBy(Comment::class, ['author' => $id]);
$commentsModels = [];
foreach ($comments as $comment){
$commentsModels[] = new CommentModel(
$comment->getId(),
$comment->getAuthorId(),
$comment->getArticleId(),
$comment->getBody(),
$comment->getCreationTime(),
$comment->getChangingTime()
);
}
return new MultipleCommentModel($commentsModels);
}
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ namespace App\Service;
use App\Entity\Tag;
use App\Entity\Topic;
use App\Entity\User;
use App\Model\MultipleTopicsModel;
use App\Model\TagModel;
use App\Model\TopicModel;
use DateTimeImmutable;
......@@ -63,4 +64,19 @@ class TopicService
$this->entityManager->flush();
}
public function getUserTopics(string $userId): MultipleTopicsModel
{
$topics = $this->entityManager->findBy(Topic::class, ['author' => $userId]);
$topicsModelArray = [];
foreach ($topics as $topic){
$topicsModelArray[] = new TopicModel(
$topic->getId(),
$topic->getAuthorId(),
$topic->getTitle(),
$topic->getCreationTime()
);
}
return new MultipleTopicsModel($topicsModelArray);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment