Skip to content

Commit

Permalink
Added missing api methods for content jobs (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandraAugustyn authored and AdrianSkierniewski committed Dec 15, 2017
1 parent 89eee94 commit 97842f9
Show file tree
Hide file tree
Showing 16 changed files with 736 additions and 574 deletions.
6 changes: 5 additions & 1 deletion database/factories/ContentTranslationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
'body' => $faker->text(),
'seo_title' => $faker->realText(60, 1),
'seo_description' => $faker->realText(160, 1),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'is_active' => true
];
});
Expand All @@ -39,6 +41,8 @@
'body' => $faker->text(),
'seo_title' => $faker->realText(60, 1),
'seo_description' => $faker->realText(160, 1),
'is_active' => false,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'is_active' => false
];
});
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ function ($router) {
$router->get('contents/{id}', 'ContentController@show');
$router->get('contents/{id}/children', 'NestedContentController@index');

$router->get('contents/{id}/translations', 'ContentTranslationController@index');
$router->post('contents/{id}/translations', 'ContentTranslationController@store');
$router->delete('contents/{id}/translations/{translationId}', 'ContentTranslationController@destroy');

$router->get('deleted-contents', 'DeletedContentController@index');
$router->delete('deleted-contents/{id}', 'DeletedContentController@destroy');
}
);

Expand Down
258 changes: 178 additions & 80 deletions src/Gzero/Cms/Http/Controllers/Api/ContentTranslationController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php namespace Gzero\Cms\Http\Controllers\Api;

use Gzero\Cms\Http\Resources\ContentTranslationCollection;
use Gzero\Cms\Http\Resources\ContentTranslation as ContentTranslationResource;
use Gzero\Cms\Jobs\AddContentTranslation;
use Gzero\Cms\Jobs\DeleteContentTranslation;
use Gzero\Cms\Models\Content;
use Gzero\Cms\Repositories\ContentReadRepository;
use Gzero\Cms\Validators\ContentTranslationValidator;
use Gzero\Core\Http\Controllers\ApiController;
use Gzero\Core\Parsers\BoolParser;
use Gzero\Core\Parsers\DateRangeParser;
use Gzero\Core\Parsers\NumericParser;
use Gzero\Core\Parsers\StringParser;
use Gzero\Core\UrlParamsProcessor;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -36,120 +43,211 @@ public function __construct(ContentReadRepository $repository, ContentTranslatio
/**
* Display a listing of the resource.
*
* @SWG\Get(
* path="/contents/{id}/translations",
* tags={"content"},
* summary="List of all content translations",
* description="List of all available content translations",
* produces={"application/json"},
* security={{"AdminAccess": {}}},
* @SWG\Parameter(
* name="author_id",
* in="query",
* description="Author id to filter by",
* required=false,
* type="integer",
* default="1"
* ),
* @SWG\Parameter(
* name="is_active",
* in="query",
* description="Active translation filter",
* required=false,
* type="boolean",
* default="true"
* ),
* @SWG\Parameter(
* name="created_at",
* in="query",
* description="Date range to filter by",
* required=false,
* type="array",
* minItems=2,
* maxItems=2,
* default={"2017-10-01","2017-10-07"},
* @SWG\Items(type="string")
* ),
* @SWG\Parameter(
* name="updated_at",
* in="query",
* description="Date range to filter by",
* required=false,
* type="array",
* minItems=2,
* maxItems=2,
* default={"2017-10-01","2017-10-07"},
* @SWG\Items(type="string")
* ),
* @SWG\Response(
* response=200,
* description="Successful operation",
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/ContentTranslation")),
* ),
* @SWG\Response(
* response=422,
* description="Validation Error",
* @SWG\Schema(ref="#/definitions/ValidationErrors")
* ),
* @SWG\Response(response=404, description="Content not found")
* )
*
* @param UrlParamsProcessor $processor Params processor
* @param int|null $id Id used for nested resources
*
* @return \Illuminate\Http\JsonResponse
* @return ContentTranslationCollection
*/
public function index(UrlParamsProcessor $processor, $id)
{
$content = $this->repository->getById($id);

if (!$content) {
return $this->errorNotFound();
}

$this->authorize('readList', Content::class);

$processor
->addFilter(new StringParser('lang'))
->addFilter(new StringParser('type'))
->addFilter(new StringParser('parent_id'))
->addFilter(new StringParser('is_active'))
->addFilter(new StringParser('level'))
->addFilter(new StringParser('level'))
->addFilter(new StringParser('trashed'))
->addFilter(new StringParser('language_code'), 'in:pl,en,de,fr')
->addFilter(new NumericParser('author_id'))
->addFilter(new BoolParser('is_active'))
->addFilter(new DateRangeParser('created_at'))
->addFilter(new DateRangeParser('updated_at'))
->process($this->request);

$content = $this->repository->getById($id);
if (!empty($content)) {

$results = $this->repository->getManyTranslations($processor->buildQueryBuilder());

return $this->respondWithSuccess($results, new ContentTranslationTransformer);
} else {
return $this->respondNotFound();
}
}
$results = $this->repository->getManyTranslations($content, $processor->buildQueryBuilder());
$results->setPath(apiUrl('contents/{id}/translations', ['id' => $id]));

/**
* Display a specified resource.
*
* @param int $id Id of the content
* @param int $translationId Id of the content translation
*
* @return \Illuminate\Http\JsonResponse
*/
public function show($id, $translationId)
{
$content = $this->getContent($id);
if (!empty($content)) {
$this->authorize('read', $content);
$translation = $this->repository->getContentTranslationById($content, $translationId);
if (!empty($translation)) {
return $this->respondWithSuccess($translation, new ContentTranslationTransformer);
}
}
return $this->respondNotFound();
return new ContentTranslationCollection($results);
}

/**
* Stores newly created translation for specified content entity in database.
*
* @SWG\Post(path="/contents/{id}/translations",
* tags={"content"},
* summary="Stores newly created content translation",
* description="Stores newly created content translation",
* produces={"application/json"},
* security={{"AdminAccess": {}}},
* @SWG\Parameter(
* in="body",
* name="body",
* description="Fields to create.",
* required=true,
* @SWG\Schema(
* type="object",
* required={"title, language_code"},
* @SWG\Property(property="language_code", type="string", example="en"),
* @SWG\Property(property="title", type="string", example="Example title"),
* @SWG\Property(property="teaser", type="string", example="Example Teaser"),
* @SWG\Property(property="body", type="string", example="Example Body"),
* @SWG\Property(property="seo_title", type="string", example="Example SEO Title"),
* @SWG\Property(property="seo_description", type="string", example="Example SEO Description"),
* @SWG\Property(property="is_active", type="boolean", example="true"),
* )
* ),
* @SWG\Response(
* response=201,
* description="Successful operation",
* @SWG\Schema(type="object", ref="#/definitions/ContentTranslation"),
* ),
* @SWG\Response(
* response=422,
* description="Validation Error",
* @SWG\Schema(ref="#/definitions/ValidationErrors")
* ),
* @SWG\Response(response=404, description="Content not found")
* )
*
* @param int $id Id of the content
*
* @return \Illuminate\Http\JsonResponse
* @return ContentTranslationResource
*/
public function store($id)
{
$content = $this->getContent($id);
if (!empty($content)) {
$this->authorize('create', $content);
$this->authorize('update', $content);
$input = $this->validator->validate('create');
$translation = $this->repository->createTranslation($content, $input);
return $this->respondWithSuccess($translation, new ContentTranslationTransformer);
$content = $this->repository->getById($id);

if (!$content) {
return $this->errorNotFound();
}
return $this->respondNotFound();
}

/**
* Each translations update always creates new record in database, for history revision
*
* @param int $id Id of the content
*
* @return \Illuminate\Http\JsonResponse
*/
public function update($id)
{
return $this->store($id);
$this->authorize('create', $content);
$this->authorize('update', $content);

$input = $this->validator->validate('create');

$author = auth()->user();
$title = array_get($input, 'title');
$language = language(array_get($input, 'language_code'));
$data = array_except($input, ['title', 'language_code']);

$translation = dispatch_now(new AddContentTranslation($content, $title, $language, $author, $data));

return new ContentTranslationResource($translation);
}

/**
* Remove the specified resource from database.
* Removes the specified resource from database.
*
* @SWG\Delete(path="/contents/{id}/translations/{id}",
* tags={"content"},
* summary="Deletes specified content translation",
* description="Deletes specified content translation.",
* produces={"application/json"},
* security={{"AdminAccess": {}}},
* @SWG\Parameter(
* name="id",
* in="path",
* description="Id of content that holds translation.",
* required=true,
* type="integer"
* ),
* @SWG\Parameter(
* name="translationId",
* in="path",
* description="Id of content translation that needs to be deleted.",
* required=true,
* type="integer"
* ),
* @SWG\Response(
* response=204,
* description="Successful operation"
* ),
* @SWG\Response(response=404, description="Content or content translation not found"),
* @SWG\Response(response=400, description="Cannot delete active translation")
* )
*
* @param int $id Id of the content
* @param int $translationId Id of the content translation
* @param int $id Content id
* @param int $translationId Translation id
*
* @return \Illuminate\Http\JsonResponse
*/
public function destroy($id, $translationId)
{
$content = $this->getContent($id);
if (!empty($content)) {
$this->authorize('delete', $content);
$translation = $this->repository->getContentTranslationById($content, $translationId);
if (!empty($translation)) {
$this->repository->deleteTranslation($translation);
return $this->respondWithSimpleSuccess(['success' => true]);
}
$content = $this->repository->getById($id);

if (!$content) {
return $this->errorNotFound();
}
return $this->respondNotFound();
}

/**
* Gets Content entity by id
*
* @param int $id content id
*
* @return Content
*/
protected function getContent($id)
{
return $this->repository->getById($id);
$translation = $content->translations(false)->find($translationId);

if (!$translation) {
return $this->errorNotFound();
}

dispatch_now(new DeleteContentTranslation($translation));

return $this->successNoContent();
}
}
41 changes: 41 additions & 0 deletions src/Gzero/Cms/Http/Controllers/Api/DeletedContentController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Gzero\Cms\Http\Controllers\Api;

use Gzero\Cms\Http\Resources\ContentCollection;
use Gzero\Cms\Jobs\ForceDeleteContent;
use Gzero\Cms\Models\Content;
use Gzero\Cms\Repositories\ContentReadRepository;
use Gzero\Cms\Validators\ContentValidator;
Expand Down Expand Up @@ -92,4 +93,44 @@ public function index(UrlParamsProcessor $processor)

return new ContentCollection($results);
}

/**
* Removes the specified resource from database.
*
* @SWG\Delete(path="/deleted-contents/{id}",
* tags={"content"},
* summary="Deletes soft deleted content",
* description="Deletes soft deleted content.",
* produces={"application/json"},
* security={{"AdminAccess": {}}},
* @SWG\Parameter(
* name="id",
* in="path",
* description="Id of soft deleted content that needs to be deleted.",
* required=true,
* type="integer"
* ),
* @SWG\Response(
* response=204,
* description="Successful operation"
* ),
* @SWG\Response(response=404, description="Content not found")
* )
*
* @param int $id Content id
*
* @return \Illuminate\Http\JsonResponse
*/
public function destroy($id)
{
$content = $this->repository->getDeletedById($id);

if (!$content) {
return $this->errorNotFound();
}

dispatch_now(new ForceDeleteContent($content));

return $this->successNoContent();
}
}
Loading

0 comments on commit 97842f9

Please sign in to comment.