Skip to content

Commit 60952b0

Browse files
refactor(article): переделал вывод списка статей на итерации
1 parent a8e272c commit 60952b0

File tree

9 files changed

+146
-48
lines changed

9 files changed

+146
-48
lines changed

backend/src-dev/OpenApi/openapi-admin.yaml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,10 @@ components:
326326
data:
327327
type: array
328328
items:
329-
properties: { title: { type: string, description: Заголовок }, alias: { type: string, description: Алиас }, body: { type: string, description: Контент, nullable: true } }
329+
$ref: '#/components/schemas/adminArticleListItem'
330330
example:
331-
- { title: 'Первая статья', alias: first-article, body: 'Контент первой статьи' }
332-
- { title: 'Вторая статья', alias: second-article, body: 'Контент второй статьи' }
331+
- { id: 0196a05e-f2bc-7ee0-b394-642978a036ec, title: 'Первая статья', alias: first-article, createdAt: 2025-05-05T15:15:12+00:00 }
332+
- { id: 0196a05e-da4a-7209-821a-d860ec58f714, title: 'Вторая статья', alias: second-article, createdAt: 2025-05-04T15:15:12+00:00 }
333333
headers:
334334
X-Request-TraceId:
335335
$ref: '#/components/headers/requestTraceId'
@@ -461,8 +461,10 @@ components:
461461
required:
462462
- id
463463
- title
464+
- body
464465
- alias
465466
- createdAt
467+
- updatedAt
466468
properties:
467469
id:
468470
type: string
@@ -484,6 +486,25 @@ components:
484486
$ref: '#/components/schemas/updatedAt'
485487
status:
486488
$ref: '#/components/schemas/succeedStatus'
489+
adminArticleListItem:
490+
type: object
491+
required:
492+
- id
493+
- title
494+
- alias
495+
- createdAt
496+
properties:
497+
id:
498+
type: string
499+
format: uuid
500+
description: ID
501+
example: dc8dbada-b154-4ffb-b21c-da70162e2704
502+
title:
503+
$ref: '#/components/schemas/adminArticleTitle'
504+
alias:
505+
$ref: '#/components/schemas/adminArticleAlias'
506+
createdAt:
507+
$ref: '#/components/schemas/createdAt'
487508
adminArticleTitle:
488509
type: string
489510
description: Заголовок

backend/src-dev/OpenApi/openapi.yaml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,10 @@ components:
10931093
data:
10941094
type: array
10951095
items:
1096-
properties: { title: { type: string, description: Заголовок }, alias: { type: string, description: Алиас }, body: { type: string, description: Контент, nullable: true } }
1096+
$ref: '#/components/schemas/adminArticleListItem'
10971097
example:
1098-
- { title: 'Первая статья', alias: first-article, body: 'Контент первой статьи' }
1099-
- { title: 'Вторая статья', alias: second-article, body: 'Контент второй статьи' }
1098+
- { id: 0196a05e-f2bc-7ee0-b394-642978a036ec, title: 'Первая статья', alias: first-article, createdAt: 2025-05-05T15:15:12+00:00 }
1099+
- { id: 0196a05e-da4a-7209-821a-d860ec58f714, title: 'Вторая статья', alias: second-article, createdAt: 2025-05-04T15:15:12+00:00 }
11001100
headers:
11011101
X-Request-TraceId:
11021102
$ref: '#/components/headers/requestTraceId'
@@ -1294,8 +1294,10 @@ components:
12941294
required:
12951295
- id
12961296
- title
1297+
- body
12971298
- alias
12981299
- createdAt
1300+
- updatedAt
12991301
properties:
13001302
id:
13011303
type: string
@@ -1317,6 +1319,25 @@ components:
13171319
$ref: '#/components/schemas/updatedAt'
13181320
status:
13191321
$ref: '#/components/schemas/succeedStatus'
1322+
adminArticleListItem:
1323+
type: object
1324+
required:
1325+
- id
1326+
- title
1327+
- alias
1328+
- createdAt
1329+
properties:
1330+
id:
1331+
type: string
1332+
format: uuid
1333+
description: ID
1334+
example: dc8dbada-b154-4ffb-b21c-da70162e2704
1335+
title:
1336+
$ref: '#/components/schemas/adminArticleTitle'
1337+
alias:
1338+
$ref: '#/components/schemas/adminArticleAlias'
1339+
createdAt:
1340+
$ref: '#/components/schemas/createdAt'
13201341
adminArticleTitle:
13211342
type: string
13221343
description: Заголовок

backend/src-dev/OpenApi/resources/admin/article.yaml

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,16 @@ components:
193193
data:
194194
type: array
195195
items:
196-
properties:
197-
title:
198-
type: string
199-
description: Заголовок
200-
alias:
201-
type: string
202-
description: Алиас
203-
body:
204-
type: string
205-
description: Контент
206-
nullable: true
196+
$ref: '#/components/schemas/adminArticleListItem'
207197
example:
208-
- title: Первая статья
198+
- id: 0196a05e-f2bc-7ee0-b394-642978a036ec
199+
title: Первая статья
209200
alias: first-article
210-
body: Контент первой статьи
211-
- title: Вторая статья
201+
createdAt: 2025-05-05T15:15:12+00:00
202+
- id: 0196a05e-da4a-7209-821a-d860ec58f714
203+
title: Вторая статья
212204
alias: second-article
213-
body: Контент второй статьи
205+
createdAt: 2025-05-04T15:15:12+00:00
214206
headers:
215207
X-Request-TraceId:
216208
$ref: '../common.yaml#/components/headers/requestTraceId'
@@ -256,8 +248,10 @@ components:
256248
required:
257249
- id
258250
- title
251+
- body
259252
- alias
260253
- createdAt
254+
- updatedAt
261255
properties:
262256
id:
263257
type: string
@@ -279,6 +273,25 @@ components:
279273
$ref: '../common.yaml#/components/schemas/updatedAt'
280274
status:
281275
$ref: '../common.yaml#/components/schemas/succeedStatus'
276+
adminArticleListItem:
277+
type: object
278+
required:
279+
- id
280+
- title
281+
- alias
282+
- createdAt
283+
properties:
284+
id:
285+
type: string
286+
format: uuid
287+
description: ID
288+
example: dc8dbada-b154-4ffb-b21c-da70162e2704
289+
title:
290+
$ref: '#/components/schemas/adminArticleTitle'
291+
alias:
292+
$ref: '#/components/schemas/adminArticleAlias'
293+
createdAt:
294+
$ref: '../common.yaml#/components/schemas/createdAt'
282295
adminArticleTitle:
283296
type: string
284297
description: Заголовок

backend/src-dev/Tests/Functional/Article/Admin/ArticleListByIdsTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ public function testSuccess(): void
5555
* id: string,
5656
* title: string,
5757
* alias: string,
58-
* body: string,
59-
* createdAt: string|null,
60-
* updatedAt: string|null
58+
* createdAt: string|null
6159
* }>,
6260
* pagination: array{total: int},
6361
* } $articles
@@ -69,16 +67,12 @@ public function testSuccess(): void
6967
self::assertSame($articleId2, $articles['data'][0]['id']);
7068
self::assertSame($title2, $articles['data'][0]['title']);
7169
self::assertSame($alias2, $articles['data'][0]['alias']);
72-
self::assertSame($content2, $articles['data'][0]['body']);
7370
self::assertNotEmpty($articles['data'][0]['createdAt']);
74-
self::assertNull($articles['data'][0]['updatedAt']);
7571

7672
self::assertSame($articleId1, $articles['data'][1]['id']);
7773
self::assertSame($title1, $articles['data'][1]['title']);
7874
self::assertSame($alias1, $articles['data'][1]['alias']);
79-
self::assertSame($content1, $articles['data'][1]['body']);
8075
self::assertNotEmpty($articles['data'][1]['createdAt']);
81-
self::assertNull($articles['data'][1]['updatedAt']);
8276

8377
self::assertSame(2, $articles['pagination']['total']);
8478
}
@@ -115,9 +109,7 @@ public function testArticleNotFound(): void
115109
* id: string,
116110
* title: string,
117111
* alias: string,
118-
* body: string,
119-
* createdAt: string|null,
120-
* updatedAt: string|null
112+
* createdAt: string|null
121113
* }>,
122114
* pagination: array{total: int},
123115
* } $articles
@@ -129,9 +121,7 @@ public function testArticleNotFound(): void
129121
self::assertSame($articleId, $articles['data'][0]['id']);
130122
self::assertSame($title, $articles['data'][0]['title']);
131123
self::assertSame($alias, $articles['data'][0]['alias']);
132-
self::assertSame($content, $articles['data'][0]['body']);
133124
self::assertNotEmpty($articles['data'][0]['createdAt']);
134-
self::assertNull($articles['data'][0]['updatedAt']);
135125

136126
self::assertSame(1, $articles['pagination']['total']);
137127
}

backend/src-dev/Tests/Functional/Article/Admin/ArticleListTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ public function testSuccess(): void
4444
self::assertContains($article['id'], [$articleId1, $articleId2]);
4545
self::assertContains($article['title'], [$title1, $title2]);
4646
self::assertNotEmpty($article['alias']);
47-
self::assertNotEmpty($article['body']);
4847
self::assertNotEmpty($article['createdAt']);
49-
self::assertNull($article['updatedAt']);
5048
}
5149
}
5250

backend/src/Article/Http/Admin/ArticleListAction.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Article\Http\Admin;
66

7+
use App\Article\Domain\Article;
78
use App\Article\Domain\ArticleRepository;
89
use App\Infrastructure\Response\ApiListObjectResponse;
910
use App\Infrastructure\Response\PaginationResponse;
@@ -21,7 +22,9 @@
2122
#[AsController]
2223
final readonly class ArticleListAction
2324
{
24-
public function __construct(private ArticleRepository $articleRepository) {}
25+
public function __construct(
26+
private ArticleRepository $articleRepository,
27+
) {}
2528

2629
public function __invoke(): ApiListObjectResponse
2730
{
@@ -31,8 +34,25 @@ public function __invoke(): ApiListObjectResponse
3134
$pagination = new PaginationResponse($articlesCount);
3235

3336
return new ApiListObjectResponse(
34-
data: $articles,
37+
data: $this->buildResponseData($articles),
3538
pagination: $pagination,
3639
);
3740
}
41+
42+
/**
43+
* @param list<Article> $articleList
44+
*
45+
* @return iterable<ArticleListData>
46+
*/
47+
private function buildResponseData(array $articleList): iterable
48+
{
49+
foreach ($articleList as $article) {
50+
yield new ArticleListData(
51+
id: $article->getId(),
52+
title: $article->getTitle(),
53+
alias: $article->getAlias(),
54+
createdAt: $article->getCreatedAt(),
55+
);
56+
}
57+
}
3858
}

backend/src/Article/Http/Admin/ArticleListByIdsAction.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __invoke(
5555
* @param non-empty-list<Uuid> $ids
5656
* @param list<Article> $articles
5757
*
58-
* @return iterable<Article>
58+
* @return iterable<ArticleListData>
5959
*/
6060
private function buildResponseData(array $ids, array $articles): iterable
6161
{
@@ -71,7 +71,12 @@ private function buildResponseData(array $ids, array $articles): iterable
7171
continue;
7272
}
7373

74-
yield $article;
74+
yield new ArticleListData(
75+
id: $article->getId(),
76+
title: $article->getTitle(),
77+
alias: $article->getAlias(),
78+
createdAt: $article->getCreatedAt(),
79+
);
7580
}
7681
}
7782
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Article\Http\Admin;
6+
7+
use DateTimeImmutable;
8+
use Symfony\Component\Uid\Uuid;
9+
10+
/**
11+
* Данные статьи для списка
12+
*/
13+
final readonly class ArticleListData
14+
{
15+
public function __construct(
16+
public Uuid $id,
17+
public string $title,
18+
public string $alias,
19+
public DateTimeImmutable $createdAt,
20+
) {}
21+
}

backend/src/Article/Http/Site/ArticleListAction.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Article\Http\Site;
66

7+
use App\Article\Domain\Article;
78
use App\Article\Domain\ArticleRepository;
89
use App\Infrastructure\Request\Pagination\PaginationRequest;
910
use App\Infrastructure\Request\Pagination\PaginationRequestArgumentResolver;
@@ -31,22 +32,30 @@ public function __invoke(
3132
limit: $paginationRequest->limit,
3233
offset: $paginationRequest->offset,
3334
);
35+
3436
$articlesCount = $this->articleRepository->countAll();
3537

36-
$data = [];
38+
$pagination = new PaginationResponse($articlesCount);
39+
40+
return new ApiListObjectResponse(
41+
data: $this->buildResponseData($articles),
42+
pagination: $pagination,
43+
);
44+
}
45+
46+
/**
47+
* @param list<Article> $articles
48+
*
49+
* @return iterable<ArticleListData>
50+
*/
51+
private function buildResponseData(array $articles): iterable
52+
{
3753
foreach ($articles as $article) {
38-
$data[] = new ArticleListData(
54+
yield new ArticleListData(
3955
title: $article->getTitle(),
4056
alias: $article->getAlias(),
4157
body: $article->getBody(),
4258
);
4359
}
44-
45-
$pagination = new PaginationResponse($articlesCount);
46-
47-
return new ApiListObjectResponse(
48-
data: $data,
49-
pagination: $pagination,
50-
);
5160
}
5261
}

0 commit comments

Comments
 (0)