Skip to content

Commit

Permalink
PCC-61: Changing ContentApi to ArticleApi. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
purushotamrai authored May 27, 2024
1 parent 2347dbf commit 33ef588
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 54 deletions.
29 changes: 29 additions & 0 deletions pcx_connect.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,32 @@ pcx_connect.get_all_articles:
_controller: '\Drupal\pcx_connect\Controller\DebugSiteController::getAllArticles'
requirements:
_permission: 'administer pcc configurations'

pcx_connect.get_article_by_id:
path: '/api/pantheoncloud/debug/get-article-by-id/{id}'
defaults:
_controller: '\Drupal\pcx_connect\Controller\DebugSiteController::getArticleById'
requirements:
_permission: 'administer pcc configurations'
options:
parameters:
id:
type: string

pcx_connect.get_article_by_slug:
path: '/api/pantheoncloud/debug/get-article-by-slug/{slug}'
defaults:
_controller: '\Drupal\pcx_connect\Controller\DebugSiteController::getArticleBySlug'
requirements:
_permission: 'administer pcc configurations'
options:
parameters:
slug:
type: string

pcx_connect.search_articles:
path: '/api/pantheoncloud/debug/search-articles'
defaults:
_controller: '\Drupal\pcx_connect\Controller\DebugSiteController::searchArticles'
requirements:
_permission: 'administer pcc configurations'
10 changes: 6 additions & 4 deletions pcx_connect.services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
services:
pcx_connect.pcc_api_client:
class: Drupal\pcx_connect\Service\PccApiClient
class: Drupal\pcx_connect\Pcc\Service\PccApiClient
arguments: ['@logger.factory']
pcx_connect.pcc_content_api:
class: Drupal\pcx_connect\Service\PccContentApi
arguments: ['@pcx_connect.pcc_api_client', '@logger.factory']
pcx_connect.articles_mapper:
class: Drupal\pcx_connect\Pcc\Mapper\PccArticlesMapper
pcx_connect.pcc_articles_api:
class: Drupal\pcx_connect\Pcc\Service\PccArticlesApi
arguments: ['@pcx_connect.pcc_api_client', '@pcx_connect.articles_mapper', '@logger.factory']
22 changes: 20 additions & 2 deletions pcx_connect.views.inc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ function pcx_connect_views_data() {
'query_id' => 'pcc_site_view_query',
];

$table['id'] = [
'title' => $translation->translate('Id'),
'help' => $translation->translate('Article Id'),
'field' => [
'id' => 'standard',
'click sortable' => TRUE,
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];

$table['title'] = [
'title' => $translation->translate('Title'),
'help' => $translation->translate('Article title'),
Expand Down Expand Up @@ -96,8 +114,8 @@ function pcx_connect_views_data() {
],
];

$table['publishedAt'] = [
'title' => $translation->translate('Published At'),
$table['publishedDate'] = [
'title' => $translation->translate('Published Date'),
'help' => $translation->translate('Article publish time'),
'field' => [
'id' => 'date',
Expand Down
87 changes: 82 additions & 5 deletions src/Controller/DebugSiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Drupal\pcx_connect\Controller;

use Drupal\Core\Controller\ControllerBase;
use PccPhpSdk\api\ContentApi;
use PccPhpSdk\api\SiteApi;
use PccPhpSdk\api\ArticlesApi;
use PccPhpSdk\api\Query\ArticleQueryArgs;
use PccPhpSdk\api\Query\ArticleSearchArgs;
use PccPhpSdk\api\Query\Enums\PublishStatus;
use PccPhpSdk\api\SitesApi;
use PccPhpSdk\core\PccClient;
use PccPhpSdk\core\PccClientConfig;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -68,7 +71,7 @@ public static function create(ContainerInterface $container) {
*/
public function getSite(): JsonResponse {
$siteId = $this->getSiteID();
$contentApi = new SiteApi($this->pccClient);
$contentApi = new SitesApi($this->pccClient);
$content = $contentApi->getSite($siteId);

return new JsonResponse(
Expand All @@ -86,8 +89,77 @@ public function getSite(): JsonResponse {
* JsonResponse with all articles.
*/
public function getAllArticles(): JsonResponse {
$contentApi = new ContentApi($this->pccClient);
$content = $contentApi->getAllArticles();
$contentApi = new ArticlesApi($this->pccClient);
$response = $contentApi->getAllArticles();
$content = json_encode($response);

return new JsonResponse(
$content,
200,
[],
true
);
}

/**
* Get article by id.
*
* @param string $id
* Article ID.
*
* @return JsonResponse
* Json Response containing the article.
*/
public function getArticleById(string $id): JsonResponse {
$contentApi = new ArticlesApi($this->pccClient);
$response = $contentApi->getArticleById($id);
$content = json_encode($response);

return new JsonResponse(
$content,
200,
[],
true
);
}

/**
* Get article by slug.
*
* @param string $slug
* Article Slug.
*
* @return JsonResponse
* Json Response containing the article.
*/
public function getArticleBySlug(string $slug): JsonResponse {
$contentApi = new ArticlesApi($this->pccClient);
$response = $contentApi->getArticleBySlug($slug);
$content = json_encode($response);

return new JsonResponse(
$content,
200,
[],
true
);
}

/**
* Search Articles.
*
* @return JsonResponse
* Json Response containing the articles.
*/
public function searchArticles(): JsonResponse {
$contentApi = new ArticlesApi($this->pccClient);
$response = $contentApi->searchArticles(new ArticleQueryArgs(), new ArticleSearchArgs(
$this->getQueryArg('bodyContains') ?? '',
$this->getQueryArg('tagContains') ?? '',
$this->getQueryArg('titleContains') ?? '',
$this->getQueryArg('published') ? PublishStatus::PUBLISHED : PublishStatus::UNPUBLISHED
));
$content = json_encode($response);

return new JsonResponse(
$content,
Expand Down Expand Up @@ -116,4 +188,9 @@ private function getSiteID(): ?string {
private function getSiteToken(): ?string {
return $this->requestStack->getCurrentRequest()->query->get('site-token');
}

private function getQueryArg(string $name): ?string {
return $this->requestStack->getCurrentRequest()->query->get($name);
}

}
86 changes: 86 additions & 0 deletions src/Pcc/Entity/PccSiteArticle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Drupal\pcx_connect\Pcc\Entity;

class PccSiteArticle {

/**
* Article ID.
*
* @var string $id
*/
public string $id;

/**
* Article Slug.
*
* @var string $slug
*/
public string $slug;

/**
* Article Title.
*
* @var string $title
*/
public string $title;

/**
* Site ID.
*
* @var string $siteId
*/
public string $siteId;

/**
* Article Content.
*
* @var string $content
*/
public string $content;

/**
* Article snippet.
*
* @var string $snippet
*/
public string $snippet;

/**
* Article Tags.
*
* @todo Redefine exact type of Tags, id or string.
*
* @var array $tags
*/
public array $tags;

/**
* Published Date.
*
* @var string $publishedDate
*/
public string $publishedDate;

/**
* Updated Date.
*
* @var string $updatedAt
*/
public string $updatedAt;

public static function getDefaultFields() {
return [
'id',
'slug',
'title',
'siteId',
'content',
'snippet',
'tags',
'publishedDate',
'updatedAt',
];
}

}
22 changes: 22 additions & 0 deletions src/Pcc/Mapper/PccArticlesMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Drupal\pcx_connect\Pcc\Mapper;

use PccPhpSdk\api\Response\Article;
use PccPhpSdk\api\Response\PaginatedArticles;

class PccArticlesMapper implements PccArticlesMapperInterface {

public function toArticleData(Article $article): array {
return (array) $article;
}

public function toArticlesList(PaginatedArticles $paginatedArticles): array {
$list = [];
foreach ($paginatedArticles->articles as $article) {
$list[] = $this->toArticleData($article);
}
return $list;
}

}
14 changes: 14 additions & 0 deletions src/Pcc/Mapper/PccArticlesMapperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Drupal\pcx_connect\Pcc\Mapper;

use PccPhpSdk\api\Response\Article;
use PccPhpSdk\api\Response\PaginatedArticles;

interface PccArticlesMapperInterface {

function toArticleData(Article $article): array;

function toArticlesList(PaginatedArticles $paginatedArticles): array;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\pcx_connect\Service;
namespace Drupal\pcx_connect\Pcc\Service;

use PccPhpSdk\core\PccClient;
use PccPhpSdk\core\PccClientConfig;
Expand Down
Loading

0 comments on commit 33ef588

Please sign in to comment.