Skip to content

Commit

Permalink
Simplify repositories and add example event
Browse files Browse the repository at this point in the history
  • Loading branch information
hariadi committed Jun 5, 2017
1 parent d6aa663 commit b55b32b
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/Commands/stubs/repository.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class {{class}} extends BaseRepository
/**
* @return mixed
*/
public function getForDataTable() {}
public function getForDataTable()
{
//
}

{{all}} /**
* Get all instance of {{model}}.
Expand Down Expand Up @@ -45,19 +48,19 @@ class {{class}} extends BaseRepository
* Create a new instance of {{model}}.
*
* @param array $input
* @return bool
* @return object
*/
public function create($input)
{
${{variable}} = $this->create{{model}}Stub($input);

if (!${{variable}}->save()) {
if (${{variable}}->save()) {
// event(new {{model}}Created(${{variable}}));

throw new GeneralException('There was a problem creating this {{variable}}. Please try again.');
return ${{variable}};
}

return ${{variable}};

throw new GeneralException('There was a problem creating this {{variable}}. Please try again.');
}
{{/create}}
{{update}} /**
Expand All @@ -67,34 +70,33 @@ class {{class}} extends BaseRepository
* @param array $input
* @return bool|int
*/
public function update($id, $input)
public function update(Model ${{variable}}, array $input)
{
${{variable}} = $this->findOrThrowException($id);

if (!${{variable}}->update($input)) {
throw new GeneralException('There was a problem updating this {{variable}}. Please try again.');
}
${{variable}} = $this->createAgencyStub($input);

return ${{variable}};
if (${{variable}}->save()) {
//event(new {{model}}Updated(${{variable}}));

return ${{variable}};
}

throw new GeneralException('There was a problem updating this {{variable}}. Please try again.');
}
{{/update}}
{{delete}} /**
* @param $id
* @throws GeneralException
* @return boolean|null
*/
public function destroy($id)
public function destroy(Model ${{variable}})
{
${{variable}} = $this->findOrThrowException($id);
if (${{variable}}->delete()) {
//event(new {{model}}Deleted(${{variable}}));

if (!${{variable}}->delete()) {
throw new GeneralException('There was a problem deleting this {{variable}}. Please try again.');
return true;
}

return true;

throw new GeneralException('There was a problem deleting this {{variable}}. Please try again.');
}
{{/delete}}
/**
Expand All @@ -103,8 +105,13 @@ class {{class}} extends BaseRepository
*/
private function create{{model}}Stub($input)
{
${{variable}} = new {{model}};
${{variable}}->name = $input['name'];
${{variable}} = new self::MODEL;

foreach (${{variable}}->getFillable() as $column) {
if (array_key_exists($column, $input)) {
${{variable}}->{$column} = $input[$column];
}
}

return ${{variable}};
}
Expand Down

0 comments on commit b55b32b

Please sign in to comment.