Skip to content

Commit

Permalink
Published content endpoint (#132)
Browse files Browse the repository at this point in the history
* apply patch

* Review fixes.

* fix typo

* fix typo

* review fixes
  • Loading branch information
michalstrzelecki authored Dec 13, 2017
1 parent 33ed3b0 commit 89eee94
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 34 deletions.
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ function ($router) {
],
function ($router) {
/** @var \Illuminate\Routing\Router $router */
$router->get('public-contents', 'PublicContentController@index');
}
);
57 changes: 57 additions & 0 deletions src/Gzero/Cms/Http/Controllers/Api/PublicContentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace Gzero\Cms\Http\Controllers\Api;

use Gzero\Cms\Http\Resources\ContentCollection;
use Gzero\Cms\Repositories\ContentReadRepository;
use Gzero\Core\Http\Controllers\ApiController;
use Gzero\Core\Query\QueryBuilder;
use Illuminate\Http\Request;

/**
* Class PublicContentController
*/
class PublicContentController extends ApiController {

/** @var ContentReadRepository */
protected $repository;

/** @var Request */
protected $request;

/**
* ContentController constructor.
*
* @param ContentReadRepository $repository Content repository
* @param Request $request Request object
*/
public function __construct(ContentReadRepository $repository, Request $request)
{
$this->repository = $repository;
$this->request = $request;
}

/**
* Display list of public contents
*
* @SWG\Get(
* path="/public-contents",
* tags={"content, public"},
* summary="List of public contents",
* description="List of all publicly accessible contents",
* produces={"application/json"},
* @SWG\Response(
* response=200,
* description="Successful operation",
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Content")),
* ),
* )
*
* @return ContentCollection
*/
public function index()
{
$results = $this->repository->getManyPublished(new QueryBuilder());
$results->setPath(apiUrl('public-contents'));

return new ContentCollection($results);
}
}
53 changes: 53 additions & 0 deletions src/Gzero/Cms/Repositories/ContentReadRepository.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Gzero\Cms\Repositories;

use Carbon\Carbon;
use Gzero\Cms\Models\Content;
use Gzero\Cms\Models\ContentTranslation;
use Gzero\Core\Models\Language;
Expand Down Expand Up @@ -231,4 +232,56 @@ protected function getManyFrom(Builder $query, QueryBuilder $builder): LengthAwa
$builder->getPage()
);
}

/**
* Get only publicly accessible data on published content.
*
* @param QueryBuilder $builder Query builder
*
* @return LengthAwarePaginator
*/
public function getManyPublished(QueryBuilder $builder): LengthAwarePaginator
{
$query = Content::query()->where('published_at', '<=', Carbon::now())
->join('routes as r', function ($join) {
$join->on('contents.id', '=', 'r.routable_id')
->where('r.routable_type', '=', Content::class)
->where('r.is_active', true);
})
->distinct();

$count = clone $query->getQuery();

$results = $query->limit($builder->getPageSize())
->offset($builder->getPageSize() * ($builder->getPage() - 1))
->get(['contents.*']);

$results->load(
array_merge(
self::$loadRelations,
[
'routes' => function ($query) {
$query->where('is_active', true);
}
]
)
);

$results->transform(function ($content) {
$languages = $content->routes->pluck('language_code');
$filteredTranslations = $content->translations->filter(function ($translation) use ($languages) {
return $languages->contains($translation->language_code);
});
$content->setRelation('translations', $filteredTranslations);
return $content;
});

return new LengthAwarePaginator(
$results,
$count->select('contents.id')->get()->count(),
$builder->getPageSize(),
$builder->getPage()
);
}

}
38 changes: 6 additions & 32 deletions tests/_support/_generated/FunctionalTesterActions.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php //[STAMP] 9acb0c9a921a4c96b08921b6bc50b182
<?php //[STAMP] 6d4a6909eec29db46ec4490b5d3680ea
namespace Cms\_generated;

// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile

use Codeception\Module\Asserts;
use Cms\Helper\Functional;
use Codeception\Module\Laravel5;
use Codeception\Module\REST;

trait FunctionalTesterActions
{
/**
Expand Down Expand Up @@ -4090,37 +4095,6 @@ public function amNTLMAuthenticated($username, $password) {
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* Allows to send REST request using AWS Authorization
* Only works with PhpBrowser
* Example
* Config -
*
* modules:
* enabled:
* - REST:
* aws:
* key: accessKey
* secret: accessSecret
* service: awsService
* region: awsRegion
*
* ```php
* <?php
* $I->amAWSAuthenticated();
* ?>
* ```
* @param array $additionalAWSConfig
* @throws ModuleException
* @see \Codeception\Module\REST::amAWSAuthenticated()
*/
public function amAWSAuthenticated($additionalAWSConfig = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Condition('amAWSAuthenticated', func_get_args()));
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
Expand Down
6 changes: 5 additions & 1 deletion tests/_support/_generated/UnitTesterActions.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php //[STAMP] 998a2afd170f9ed8e5720f570bd4868b
<?php //[STAMP] f5c1ba132bef8d2add9deafba162cd9c
namespace Cms\_generated;

// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile

use Codeception\Module\Asserts;
use Cms\Helper\Unit;
use Codeception\Module\Laravel5;

trait UnitTesterActions
{
/**
Expand Down
90 changes: 90 additions & 0 deletions tests/functional/api/PublicContentCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php namespace Cms\api;

use Carbon\Carbon;
use Cms\FunctionalTester;
use Gzero\Cms\Jobs\CreateContent;
use Gzero\Core\Models\Language;
use Gzero\Core\Models\User;

class PublicContentCest {

public function shouldGetListOfPublicContents(FunctionalTester $I)
{
$user = factory(User::class)->create();
$language = new Language(['code' => 'en']);

dispatch_now(CreateContent::content(
'Content Title published one day ago',
$language,
$user,
[
'is_active' => true,
'published_at' => Carbon::now()->subDay()
]
));

dispatch_now(CreateContent::content(
'Content Title published now',
$language,
$user,
[
'is_active' => true,
'published_at' => Carbon::now()
]
));

dispatch_now(CreateContent::content(
'Content Title published tomorrow',
$language,
$user,
[
'is_active' => true,
'published_at' => Carbon::tomorrow()
]
));

$I->sendGET(apiUrl('public-contents'));

$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseJsonMatchesJsonPath('data[*]');
$I->dontSeeResponseContainsJson(
[
[
'type' => 'content',
'translations' => [
[
'language_code' => 'en',
'title' => 'Content Title published tomorrow',
'is_active' => true,
]
]
]
]
);
$I->seeResponseContainsJson(
[
[
'type' => 'content',
'translations' => [
[
'language_code' => 'en',
'title' => 'Content Title published one day ago',
'is_active' => true,
]
]
],
[
'type' => 'content',
'translations' => [
[
'language_code' => 'en',
'title' => 'Content Title published now',
'is_active' => true,
]
]
]
]
);
}
}
Loading

0 comments on commit 89eee94

Please sign in to comment.