-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bulk editing and deletion via API (#26)
- Loading branch information
Showing
25 changed files
with
616 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
/.tmp | ||
/.vscode | ||
/.vagrant | ||
/.phpunit.cache | ||
Homestead.json | ||
Homestead.yaml | ||
npm-debug.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\Models\Api\BulkDeleteRequest; | ||
use App\Http\Requests\Models\Api\BulkEditLinksRequest; | ||
use App\Http\Requests\Models\Api\BulkEditListsRequest; | ||
use App\Http\Requests\Models\Api\BulkEditTagsRequest; | ||
use App\Models\Link; | ||
use App\Models\LinkList; | ||
use App\Models\Tag; | ||
use App\Repositories\LinkRepository; | ||
use App\Repositories\ListRepository; | ||
use App\Repositories\TagRepository; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class BulkEditController extends Controller | ||
{ | ||
public function updateLinks(BulkEditLinksRequest $request) | ||
{ | ||
$models = $request->input('models'); | ||
|
||
$results = LinkRepository::bulkUpdate($models, $request->input()); | ||
|
||
return response()->json($results); | ||
} | ||
|
||
public function updateLists(BulkEditListsRequest $request) | ||
{ | ||
$models = $request->input('models'); | ||
|
||
$results = ListRepository::bulkUpdate($models, $request->input()); | ||
|
||
return response()->json($results); | ||
} | ||
|
||
public function updateTags(BulkEditTagsRequest $request) | ||
{ | ||
$models = $request->input('models'); | ||
|
||
$results = TagRepository::bulkUpdate($models, $request->input()); | ||
|
||
return response()->json($results); | ||
} | ||
|
||
public function delete(BulkDeleteRequest $request) | ||
{ | ||
$type = $request->input('type'); | ||
$formModels = $request->input('models'); | ||
$models = match ($type) { | ||
'links' => Link::whereIn('id', $formModels)->get(), | ||
'lists' => LinkList::whereIn('id', $formModels)->get(), | ||
'tags' => Tag::whereIn('id', $formModels)->get(), | ||
}; | ||
|
||
$results = $models->mapWithKeys(function ($model) use ($type) { | ||
if (!auth()->user()->can('delete', $model)) { | ||
Log::warning('Could not delete ' . $type . ' ' . $model->id . ' during bulk deletion: Permission denied!'); | ||
return [$model->id => false]; | ||
} | ||
return match ($type) { | ||
'links' => [$model->id => LinkRepository::delete($model) === true], | ||
'lists' => [$model->id => ListRepository::delete($model) === true], | ||
'tags' => [$model->id => TagRepository::delete($model) === true], | ||
}; | ||
}); | ||
|
||
return response()->json($results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Models\Api; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class BulkDeleteRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'type' => ['required', 'in:links,lists,tags'], | ||
'models' => ['required', 'array'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Models\Api; | ||
|
||
use App\Rules\ModelVisibility; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class BulkEditLinksRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'models' => ['required', 'array'], | ||
'tags' => ['nullable', 'array'], | ||
'tags_mode' => ['required_with:tags', 'in:append,replace'], | ||
'lists' => ['nullable', 'array'], | ||
'lists_mode' => ['required_with:lists', 'in:append,replace'], | ||
'visibility' => ['nullable', new ModelVisibility], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Models\Api; | ||
|
||
use App\Rules\ModelVisibility; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class BulkEditListsRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'models' => ['required', 'array'], | ||
'visibility' => ['nullable', new ModelVisibility], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Models\Api; | ||
|
||
use App\Rules\ModelVisibility; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class BulkEditTagsRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'models' => ['required', 'array'], | ||
'visibility' => ['nullable', new ModelVisibility], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.