Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Allowed access to items before attaching/detaching/syncing
  • Loading branch information
ezra-obiwale committed Jan 17, 2018
1 parent 80cb27e commit 6a31d3d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Controllers/Traits/Attachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Response;
use Log;
use Illuminate\Database\Eloquent\Model;

/**
* Shortcuts for many-to-many attachments
Expand Down Expand Up @@ -47,6 +48,16 @@ private function treatRelation($model, &$relation) {
}
}

protected function prepareAttachItems($items, Model $model, $relation) {
return $items;
}
protected function prepareDetachItems($items, Model $model, $relation) {
return $items;
}
protected function prepareSyncItems($items, Model $model, $relation) {
return $items;
}

/**
* Fetches a paginated list of related items
*
Expand Down Expand Up @@ -87,14 +98,14 @@ public function attach($id, $relation, $paramKey = null)
try {
$items = request()->input($paramKey);
$this->treatRelation($model, $relation);
$model->$relation()->syncWithoutDetaching($items);
$model->$relation()->syncWithoutDetaching($this->prepareAttachItems($items, $model, $relation));
return response()->json([
'status' => 'ok'
]);
}
catch (\Exception $e) {
Log::error($e->getMessage());
return $this->error('Something went wrong. Are you sure the items exists?');
return $this->error('Something went wrong. Are you sure the ' . str_replace('_', ' ', $paramKey) . ' exists?');
}
}

Expand All @@ -121,14 +132,14 @@ public function detach($id, $relation, $paramKey = null)
try {
$items = request()->input($paramKey);
$this->treatRelation($model, $relation);
$model->$relation()->detach($items);
$model->$relation()->detach($this->prepareDetachItems($items, $model, $relation));
return response()->json([
'status' => 'ok'
]);
}
catch (\Exception $e) {
Log::error($e->getMessage());
return $this->error('Something went wrong. Are you sure the items exists?');
return $this->error('Something went wrong. Are you sure the ' . str_replace('_', ' ', $paramKey) . ' exists?');
}
}

Expand All @@ -155,7 +166,7 @@ public function sync($id, $relation, $paramKey = null)
try {
$items = request()->input($paramKey);
$this->treatRelation($model, $relation);
$resp = $model->$relation()->sync($items);
$resp = $model->$relation()->sync($this->prepareSyncItems($items, $model, $relation));
$resp['added'] = $resp['attached'];
$resp['removed'] = $resp['detached'];
unset($resp['attached']);
Expand All @@ -166,7 +177,7 @@ public function sync($id, $relation, $paramKey = null)
}
catch (\Exception $e) {
Log::error($e->getMessage());
return $this->error('Something went wrong. Are you sure the items exists?');
return $this->error('Something went wrong. Are you sure the ' . str_replace('_', ' ', $paramKey) . ' exists?');
}
}

Expand Down

0 comments on commit 6a31d3d

Please sign in to comment.