Skip to content

Commit

Permalink
ブログ 投稿者別記事一覧URL変更
Browse files Browse the repository at this point in the history
  • Loading branch information
seto1 committed Jun 28, 2024
1 parent 6355d26 commit 9df83fe
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 63 deletions.
8 changes: 4 additions & 4 deletions plugins/bc-blog/src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function index(
*
* ### URL例
* - カテゴリ別記事一覧: /news/archives/category/category-name
* - 作成者別記事一覧: /news/archives/author/author-name
* - 作成者別記事一覧: /news/archives/author/user-id
* - タグ別記事一覧: /news/archives/tag/tag-name
* - 年別記事一覧: /news/archives/date/2022
* - 月別記事一覧: /news/archives/date/2022/12
Expand Down Expand Up @@ -198,15 +198,15 @@ public function archives(

case 'author':
if (count($pass) > 2) $this->notFound();
$author = isset($pass[1])? $pass[1] : '';
$userId = isset($pass[1]) ? (int) $pass[1] : '';
$this->set($service->getViewVarsForArchivesByAuthor(
$this->paginate($blogPostsService->getIndexByAuthor($author, array_merge([
$this->paginate($blogPostsService->getIndexByAuthor($userId, array_merge([
'status' => 'publish',
'blog_content_id' => $blogContent->id,
'direction' => $blogContent->list_direction,
'draft' => false
], $this->getRequest()->getQueryParams())), ['limit' => $blogContent->list_count]),
$author,
$userId,
$blogContent
));
break;
Expand Down
28 changes: 3 additions & 25 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ protected function createIndexConditions(Query $query, array $params)
'site_id' => null,
'category' => null,
'keyword' => null,
'author' => null,
'tag' => null,
'year' => null,
'month' => null,
Expand Down Expand Up @@ -342,10 +341,6 @@ protected function createIndexConditions(Query $query, array $params)
if ($params['keyword']) {
$conditions = $this->createKeywordCondition($conditions, $params['keyword']);
}
// 作成者
if ($params['author']) {
$conditions = $this->createAuthorCondition($conditions, $params['author']);
}
return $query->where($conditions);
}

Expand Down Expand Up @@ -538,23 +533,6 @@ public function createYearMonthDayCondition($conditions, $year, $month, $day)
return $conditions;
}

/**
* 作成者の条件を作成する
*
* @param array $conditions
* @param string $author
* @return array
* @checked
* @noTodo
* @unitTest
*/
public function createAuthorCondition($conditions, $author)
{
$user = $this->BlogPosts->Users->find()->where(['Users.name' => $author])->first();
$conditions['BlogPosts.user_id'] = $user->id;
return $conditions;
}

/**
* 初期データ用のエンティティを取得
*
Expand Down Expand Up @@ -809,16 +787,16 @@ public function getIndexByCategory($category, array $options = [])
/**
* 著者別記事一覧を取得
*
* @param string $author
* @param int $userId
* @param array $options
* @return Query
* @checked
* @noTodo
* @unitTest
*/
public function getIndexByAuthor(string $author, array $options = [])
public function getIndexByAuthor(int $userId, array $options = [])
{
$options['author'] = $author;
$options['user_id'] = $userId;
return $this->getIndex($options);
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/bc-blog/src/Service/BlogPostsServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ public function getIndexByCategory($category, array $options = []);
/**
* 著者別記事一覧を取得
*
* @param string $author
* @param int $userId
* @param array $options
* @return Query
* @checked
* @noTodo
* @unitTest
*/
public function getIndexByAuthor(string $author, array $options = []);
public function getIndexByAuthor(int $userId, array $options = []);

/**
* タグ別記事一覧を取得
Expand Down
6 changes: 3 additions & 3 deletions plugins/bc-blog/src/Service/Front/BlogFrontService.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,17 @@ public function getCategoryCrumbs(string $baseUrl, int $categoryId, $isCategoryP
/**
* 著者別アーカイブ一覧の view 用変数を取得する
* @param ResultSet|PaginatedResultSet $posts
* @param string $author
* @param int $userId
* @param BlogContent $blogContent
* @return array
* @checked
* @noTodo
* @unitTest
*/
public function getViewVarsForArchivesByAuthor(ResultSet|PaginatedResultSet $posts, string $author, BlogContent $blogContent): array
public function getViewVarsForArchivesByAuthor(ResultSet|PaginatedResultSet $posts, int $userId, BlogContent $blogContent): array
{
$usersTable = TableRegistry::getTableLocator()->get('BaserCore.Users');
$author = $usersTable->find('available')->where(['Users.name' => $author])->first();
$author = $usersTable->find('available')->where(['Users.id' => $userId])->first();
if (!$author) {
throw new NotFoundException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public function getCategoryCrumbs(string $baseUrl, int $categoryId, $isCategoryP

/**
* 著者別アーカイブ一覧の view 用変数を取得する
* @param ResultSet $posts
* @param string $author
* @param ResultSet|PaginatedResultSet $posts
* @param int $userId
* @return array
* @checked
* @noTodo
* @unitTest
*/
public function getViewVarsForArchivesByAuthor(ResultSet $posts, string $author, BlogContent $blogContent): array;
public function getViewVarsForArchivesByAuthor(ResultSet|PaginatedResultSet $posts, int $userId, BlogContent $blogContent): array;

/**
* タグ別アーカイブ一覧の view 用変数を取得する
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function test_archives()
$this->assertEquals('release', $vars['blogCategory']->name);
$this->assertEquals('post1', $vars['posts']->toArray()[0]->name);
//type = 'author'
$this->get('/news/archives/author/name');
$this->get('/news/archives/author/1');
$this->assertResponseOk();
$vars = $this->_controller->viewBuilder()->getVars();
$this->assertEquals('author', $vars['blogArchiveType']);
Expand Down
16 changes: 2 additions & 14 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,6 @@ public function testCreateYearMonthDayCondition()
$this->assertEquals("01", $result['DAY(BlogPosts.posted)']);
}

/**
* 作成者の条件を作成する
*/
public function testCreateAuthorCondition()
{
//データ 生成
UserFactory::make(['id' => 1, 'name' => 'test name', 'email' => '[email protected]'])->persist();
//戻り値を確認
$result = $this->BlogPostsService->createAuthorCondition([], "test name");
$this->assertEquals($result["BlogPosts.user_id"], 1);
}

/**
* 並び替え設定を生成する
*/
Expand Down Expand Up @@ -965,7 +953,7 @@ public function testGetIndexByAuthor()

// サービスメソッドを呼ぶ
// test author1 の記事を取得、id昇順
$result = $this->BlogPostsService->getIndexByAuthor('test author1', [
$result = $this->BlogPostsService->getIndexByAuthor(2, [
'direction' => 'ASC',
'order' => 'id',
]);
Expand All @@ -984,7 +972,7 @@ public function testGetIndexByAuthor()

// サービスメソッドを呼ぶ
// 記事が存在しない
$result = $this->BlogPostsService->getIndexByAuthor('test author3', []);
$result = $this->BlogPostsService->getIndexByAuthor(4, []);

// 戻り値を確認
// 指定した author の記事が存在しない
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public function test_getViewVarsForArchivesByAuthor()
// サービスメソッドを呼ぶ
$result = $this->BlogFrontService->getViewVarsForArchivesByAuthor(
$blogPostsService->getIndex([])->all(),
'name',
1,
$blogContentsService->get(1)
);

Expand All @@ -533,7 +533,7 @@ public function test_getViewVarsForArchivesByAuthor()
$this->expectException("Cake\Http\Exception\NotFoundException");
$this->BlogFrontService->getViewVarsForArchivesByAuthor(
$blogPostsService->getIndex([])->all(),
'author name test',
999,
$blogContentsService->get(1)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<?php foreach ($authors as $author): ?>
<?php
$class = ['bs-widget-list__item'];
if ($this->getRequest()->getPath() === $baseCurrentUrl . $author->name) {
if ($this->getRequest()->getPath() === $baseCurrentUrl . $author->id) {
$class[] = 'current';
}
if ($view_count) {
Expand All @@ -57,14 +57,10 @@
}
?>
<li class="<?php echo implode(' ', $class) ?>">
<?php if($author->name): ?>
<?php $this->BcBaser->link($title, $baseCurrentUrl . $author->name, [
'escape' => true,
'class' => 'bs-widget-list__item-title'
]) ?>
<?php else: ?>
<?php echo $title ?>
<?php endif ?>
<?php $this->BcBaser->link($title, $baseCurrentUrl . $author->id, [
'escape' => true,
'class' => 'bs-widget-list__item-title'
]) ?>
</li>
<?php endforeach; ?>
</ul>
Expand Down

0 comments on commit 9df83fe

Please sign in to comment.