Skip to content

Commit

Permalink
Merge pull request #18 from jarischaefer/development
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
jarischaefer committed Mar 24, 2016
2 parents f3ccfed + ef7ad54 commit 8a4a9ae
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 46 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ class PostTransformer extends HalApiTransformer
/** @var Post $model */

return [
'id' => (int)$this->model->id,
'title' => (string)$this->model->title,
'text' => (string)$this->model->text,
'user_id' => (int)$this->model->user_id,
'id' => (int)$model->id,
'title' => (string)$model->title,
'text' => (string)$model->text,
'user_id' => (int)$model->user_id,
];
}

Expand Down Expand Up @@ -187,10 +187,10 @@ class PostTransformer extends HalApiTransformer
/** @var Post $model */

return [
'id' => (int)$this->model->id,
'title' => (string)$this->model->title,
'text' => (string)$this->model->text,
'user_id' => (int)$this->model->user_id,
'id' => (int)$model->id,
'title' => (string)$model->title,
'text' => (string)$model->text,
'user_id' => (int)$model->user_id,
];
}

Expand Down Expand Up @@ -270,10 +270,10 @@ class PostTransformer extends HalApiTransformer
/** @var Post $model */

return [
'id' => (int)$this->model->id,
'title' => (string)$this->model->title,
'text' => (string)$this->model->text,
'user_id' => (int)$this->model->user_id,
'id' => (int)$model->id,
'title' => (string)$model->title,
'text' => (string)$model->text,
'user_id' => (int)$model->user_id,
];
}

Expand Down Expand Up @@ -406,7 +406,7 @@ class UsersController extends HalApiResourceController
{
$posts = $user->posts()->paginate($this->perPage);
/** @var PostsController $postsController */
$postsController = $this->application->make(PostsController::class);
$postsController = $this->app->make(PostsController::class);

return $this->responseFactory->json($postsController->paginate($posts)->build());
}
Expand All @@ -415,7 +415,7 @@ class UsersController extends HalApiResourceController
{
$comments = $user->comments()->paginate($this->perPage);
/** @var CommentsController $commentsController */
$commentsController = $this->application->make(CommentsController::class);
$commentsController = $this->app->make(CommentsController::class);

return $this->responseFactory->json($commentsController->paginate($comments)->build());
}
Expand Down
65 changes: 33 additions & 32 deletions src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,42 @@ public function show($model)
}

/**
* @inheritdoc
* POST and PUT requests must contain all attributes.
* This method returns all fillable attributes which are missing.
*
* @param Model $model
* @return array
*/
public function store()
protected function getMissingUpdateAttributes(Model $model)
{
/** @var Model $model */
$model = new $this->model;
$keys = array_keys($this->body->getArray());
$columnNames = $this->schemaBuilder->getColumnListing($model->getTable());
$attributes = [];

foreach ($columnNames as $column) {
if (!$model->isFillable($column)) {
continue; // only check columns that can actually be filled into the database
if ($model->isFillable($column) && !in_array($column, $keys)) {
$attributes[] = $column;
}
}

if (!in_array($column, $keys)) {
throw new BadPostRequestException('POST requests must contain all attributes. Failed for: ' . $column);
}
return $attributes;
}

/**
* @inheritdoc
*/
public function store()
{
/** @var Model $model */
$model = new $this->model;
$missingAttributes = $this->getMissingUpdateAttributes($model);

if (!empty($missingAttributes)) {
throw new BadPostRequestException('POST requests must contain all attributes. Failed for: ' . join(',', $missingAttributes));
}

try {
$model->setRawAttributes($this->body->getArray());
$model->save();
$model->fill($this->body->getArray())->save();
} catch (Exception $e) {
throw new DatabaseSaveException('Model could not be created.', 0, $e);
}
Expand All @@ -226,44 +240,31 @@ public function update($model = null)

switch ($this->request->getMethod()) {
case Request::METHOD_PUT:
$existed = $model->exists;
$keys = array_keys($this->body->getArray());
$columnNames = $this->schemaBuilder->getColumnListing($model->getTable());

foreach ($columnNames as $column) {
if (!$model->isFillable($column)) {
continue; // only check columns that can actually be filled into the database
}
$missingAttributes = $this->getMissingUpdateAttributes($model);

if (!in_array($column, $keys)) {
throw new BadPutRequestException('PUT requests must contain all attributes. Failed for: ' . $column);
}
if (!empty($missingAttributes)) {
throw new BadPutRequestException('PUT requests must contain all attributes. Failed for: ' . join(',', $missingAttributes));
}

$existed = $model->exists;

try {
if ($model->exists) {
$model->update($this->body->getArray());
$model->syncOriginal();
} else {
$model->setRawAttributes($this->body->getArray(), true);
$model->save();
}
$model->fill($this->body->getArray())->save();
} catch (Exception $e) {
throw new DatabaseSaveException('Model could not be saved.', 0, $e);
}

return $existed ? $this->show($model) : $this->show($model)->setStatusCode(Response::HTTP_CREATED);
case Request::METHOD_PATCH:
try {
$model->update($this->body->getArray());
$model->syncOriginal();
$model->fill($this->body->getArray())->save();
} catch (Exception $e) {
throw new DatabaseSaveException('Model could not be updated.', 0, $e);
}

return $this->show($model);
default:
return $this->responseFactory->json('', Response::HTTP_METHOD_NOT_ALLOWED);
return $this->responseFactory->make('', Response::HTTP_METHOD_NOT_ALLOWED);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Jarischaefer/HalApi/Providers/HalApiServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Jarischaefer\HalApi\Providers;

use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Schema\Builder;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
Expand Down Expand Up @@ -108,6 +110,12 @@ public function register()
$this->app->bind(HalApiRepresentation::class, HalApiRepresentationImpl::class);
$this->app->bind(HalApiCache::class, HalApiCacheImpl::class);

$this->app->singleton(Builder::class, function (Application $application) {
/** @var DatabaseManager $databaseManager */
$databaseManager = $application->make(DatabaseManager::class);

return $databaseManager->connection()->getSchemaBuilder();
});
$this->app->singleton(CacheFactory::class, CacheFactoryImpl::class);
$this->app->singleton(TransformerFactory::class, TransformerFactoryImpl::class);
$this->app->singleton(RepresentationFactory::class, RepresentationFactoryImpl::class);
Expand Down

0 comments on commit 8a4a9ae

Please sign in to comment.