Skip to content

Restructure blog-api #572

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

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions blog-api/composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"symbol-whitelist": [
"Composer\\Script\\Event"
]
}
6 changes: 3 additions & 3 deletions blog-api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@
"scripts": {
"serve": "./yii serve",
"post-update-cmd": [
"App\\Installer::postUpdate",
"App\\Installer::copyEnvFile"
"App\\Infrastructure\\Installer::postUpdate",
"App\\Infrastructure\\Installer::copyEnvFile"
],
"post-create-project-cmd": [
"App\\Installer::copyEnvFile"
"App\\Infrastructure\\Installer::copyEnvFile"
],
"test": "codecept run",
"test-watch": "phpunit-watcher watch"
Expand Down
7 changes: 7 additions & 0 deletions blog-api/config/common/di/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
'__construct()' => [
'targets' => ReferencesArray::from([
FileTarget::class,
'logger.target.stdout',
]),
],
],
'logger.target.stdout' => [
'class' => FileTarget::class,
'__construct()' => [
'php://stdout',
],
],
];
2 changes: 1 addition & 1 deletion blog-api/config/common/di/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use App\Middleware\ExceptionMiddleware;
use App\Infrastructure\Http\Middleware\ExceptionMiddleware;
use Yiisoft\Config\Config;
use Yiisoft\DataResponse\Middleware\FormatDataResponse;
use Yiisoft\Request\Body\RequestBodyParser;
Expand Down
2 changes: 1 addition & 1 deletion blog-api/config/common/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use App\Queue\LoggingAuthorizationHandler;
use App\Infrastructure\Queue\LoggingAuthorizationHandler;
use Cycle\Database\Config\SQLite\FileConnectionConfig;
use Cycle\Database\Config\SQLiteDriverConfig;
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
Expand Down
35 changes: 19 additions & 16 deletions blog-api/config/common/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

declare(strict_types=1);

use App\Auth\AuthController;
use App\Blog\BlogController;
use App\Factory\RestGroupFactory;
use App\InfoController;
use App\User\UserController;
use App\Infrastructure\IO\Http;
use Yiisoft\Auth\Middleware\Authentication;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsHtml;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
Expand All @@ -19,37 +15,44 @@

return [
Route::get('/')
->action([InfoController::class, 'index'])
->action([Http\Home\GetIndex\Action::class, '__invoke'])
->name('api/info'),

Route::get('/blog/')
->action([BlogController::class, 'index'])
->action([Http\Blog\GetIndex\Action::class, '__invoke'])
->name('blog/index'),

Route::get('/blog/{id:\d+}')
->action([BlogController::class, 'view'])
->action([Http\Blog\GetView\Action::class, '__invoke'])
->name('blog/view'),

Route::post('/blog/')
->middleware(Authentication::class)
->action([BlogController::class, 'create'])
->action([Http\Blog\PostCreate\Action::class, '__invoke'])
->name('blog/create'),

Route::put('/blog/{id:\d+}')
->middleware(Authentication::class)
->action([BlogController::class, 'update'])
->action([Http\Blog\PutUpdate\Action::class, '__invoke'])
->name('blog/update'),

RestGroupFactory::create('/users/', UserController::class)
->prependMiddleware(Authentication::class),
Route::get('/users/')
->middleware(Authentication::class)
->action([Http\User\GetIndex\Action::class, '__invoke'])
->name('users/index'),

Route::get('/users/{id:\d+}')
->middleware(Authentication::class)
->action([Http\User\GetView\Action::class, '__invoke'])
->name('users/view'),

Route::post('/auth/')
->action([AuthController::class, 'login'])
Route::post('/auth/login')
->action([Http\Auth\PostLogin\Action::class, '__invoke'])
->name('auth'),

Route::post('/logout/')
Route::post('/auth/logout')
->middleware(Authentication::class)
->action([AuthController::class, 'logout'])
->action([Http\Auth\PostLogout\Action::class, '__invoke'])
->name('logout'),

// Swagger routes
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion blog-api/config/web/di/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use App\Handler\NotFoundHandler;
use App\Infrastructure\Http\Handler\NotFoundHandler;
use Yiisoft\Definitions\DynamicReference;
use Yiisoft\Definitions\Reference;
use Yiisoft\Injector\Injector;
Expand Down
2 changes: 1 addition & 1 deletion blog-api/config/web/di/data-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use App\Formatter\ApiResponseFormatter;
use App\Infrastructure\Http\Formatter\ApiResponseFormatter;
use Yiisoft\DataResponse\DataResponseFactory;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
use Yiisoft\DataResponse\DataResponseFormatterInterface;
Expand Down
4 changes: 2 additions & 2 deletions blog-api/config/web/di/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

declare(strict_types=1);

use App\Auth\AuthRequestErrorHandler;
use App\User\UserRepository;
use App\Application\User\Entity\UserRepository;
use App\Infrastructure\Http\Handler\AuthRequestErrorHandler;
use Yiisoft\Auth\AuthenticationMethodInterface;
use Yiisoft\Auth\IdentityRepositoryInterface;
use Yiisoft\Auth\IdentityWithTokenRepositoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace App\Blog;
namespace App\Application\Blog\Entity\Post;

use App\User\User;
use App\Application\User\Entity\User;
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\BelongsTo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Blog;
namespace App\Application\Blog\Entity\Post;

use Cycle\ORM\ORMInterface;
use Cycle\ORM\Select;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Blog;
namespace App\Application\Blog\Entity\Post;

use MyCLabs\Enum\Enum;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types=1);

namespace App\Blog;
namespace App\Application\Blog\Service;

use App\Exception\NotFoundException;
use App\Application\Blog\Entity\Post\Post;
use App\Application\Blog\Entity\Post\PostRepository;
use App\Application\Exception\NotFoundException;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Paginator\PaginatorInterface;

Expand Down
39 changes: 39 additions & 0 deletions blog-api/src/Application/Blog/UseCase/Post/Create/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Application\Blog\UseCase\Post\Create;

use App\Application\Blog\Entity\Post\PostStatus;
use App\Application\User\Entity\User;

final class Command
{
public function __construct(
private string $title,
private string $content,
private PostStatus $status,
private User $user,
) {
}

public function getTitle(): string
{
return $this->title;
}

public function getContent(): string
{
return $this->content;
}

public function getUser(): User
{
return $this->user;
}

public function getStatus(): PostStatus
{
return $this->status;
}
}
27 changes: 27 additions & 0 deletions blog-api/src/Application/Blog/UseCase/Post/Create/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Application\Blog\UseCase\Post\Create;

use App\Application\Blog\Entity\Post\Post;
use App\Application\Blog\Entity\Post\PostRepository;

final class Handler
{
public function __construct(
private PostRepository $postRepository,
) {
}

public function handle(Command $command)
{
$post = new Post();
$post->setTitle($command->getTitle());
$post->setContent($command->getContent());
$post->setStatus($command->getStatus());
$post->setUser($command->getUser());

$this->postRepository->save($post);
}
}
18 changes: 18 additions & 0 deletions blog-api/src/Application/Blog/UseCase/Post/GetById/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Application\Blog\UseCase\Post\GetById;

final class Command
{
public function __construct(
private int $id,
) {
}

public function getId(): int
{
return $this->id;
}
}
20 changes: 20 additions & 0 deletions blog-api/src/Application/Blog/UseCase/Post/GetById/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Application\Blog\UseCase\Post\GetById;

use App\Application\Blog\Service\BlogService;

final class Handler
{
public function __construct(
private BlogService $blogService,
) {
}

public function handle(Command $command)
{
return $this->blogService->getPost($command->getId());
}
}
18 changes: 18 additions & 0 deletions blog-api/src/Application/Blog/UseCase/Post/GetPageList/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Application\Blog\UseCase\Post\GetPageList;

final class Command
{
public function __construct(
private int $page,
) {
}

public function getPage(): int
{
return $this->page;
}
}
24 changes: 24 additions & 0 deletions blog-api/src/Application/Blog/UseCase/Post/GetPageList/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Application\Blog\UseCase\Post\GetPageList;

use App\Application\Blog\Service\BlogService;
use Yiisoft\Data\Paginator\PaginatorInterface;

final class Handler
{
public function __construct(
private BlogService $blogService,
) {
}

/**
* TODO: Replace paginator with something else
*/
public function handle(Command $command): PaginatorInterface
{
return $this->blogService->getPosts($command->getPage());
}
}
38 changes: 38 additions & 0 deletions blog-api/src/Application/Blog/UseCase/Post/Update/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Application\Blog\UseCase\Post\Update;

use App\Application\Blog\Entity\Post\PostStatus;

final class Command
{
public function __construct(
private int $id,
private string $title,
private string $content,
private PostStatus $status,
) {
}

public function getId(): int
{
return $this->id;
}

public function getTitle(): string
{
return $this->title;
}

public function getContent(): string
{
return $this->content;
}

public function getStatus(): PostStatus
{
return $this->status;
}
}
Loading