Set controllers to use Group service & API resources

This commit is contained in:
Bubka 2021-09-21 22:46:35 +02:00
parent c7e273a580
commit 6761b9e40b
4 changed files with 142 additions and 100 deletions

View File

@ -4,10 +4,32 @@
use App\Group;
use App\TwoFAccount;
use App\Services\GroupService;
use App\Http\Requests\GroupStoreRequest;
use App\Http\Requests\GroupAssignRequest;
use App\Http\Resources\GroupResource;
use Illuminate\Http\Request;
class GroupController extends Controller
{
/**
* The TwoFAccount Service instance.
*/
protected $groupService;
/**
* Create a new controller instance.
*
* @param GroupService $groupService
* @return void
*/
public function __construct(GroupService $groupService)
{
$this->groupService = $groupService;
}
/**
* Display a listing of the resource.
*
@ -15,45 +37,27 @@ class GroupController extends Controller
*/
public function index()
{
// The index method has to return the complete collection of groups
// stored in db plus a pseudo group corresponding to 'all'
$groups = $this->groupService->getAll();
// Get the stored groups
$groups = Group::withCount('twofaccounts')->get();
// Create the pseudo group
$allGroup = new Group([
'name' => __('commons.all')
]);
$allGroup->id = 0;
$allGroup->twofaccounts_count = TwoFAccount::count();
// Merge them all
$groups->prepend($allGroup);
return response()->json($groups->toArray());
return GroupResource::collection($groups);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @param \App\Http\Requests\GroupRequest $request
* @return \App\Http\Resources\GroupResource
*/
public function store(Request $request)
public function store(GroupStoreRequest $request)
{
$validated = $request->validated();
$this->validate($request, [
'name' => 'required|string|max:32|unique:groups',
]);
$group = $this->groupService->Create($validated);
$group = Group::create([
'name' => $request->name,
]);
return response()->json($group, 201);
return (new GroupResource($group))
->response()
->setStatusCode(201);
}
@ -65,40 +69,24 @@ public function store(Request $request)
*/
public function show(Group $group)
{
return response()->json($group, 200);
return new GroupResource($group);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Group $twofaccount
* @return \Illuminate\Http\Response
* @param \App\Http\Requests\GroupRequest $request
* @param \App\Group $group
* @return \App\Http\Resources\GroupResource
*/
public function update(Request $request, $id)
public function update(GroupStoreRequest $request, Group $group)
{
$validated = $request->validated();
$this->validate($request, [
'name' => 'required|string|max:32|unique:groups',
]);
$this->groupService->update($group, $validated);
// Here we catch a possible missing model exception in order to
// delete orphan submited icon
try {
$group = Group::FindOrFail($id);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
throw $e;
}
$group->update([
'name' => $request->name,
]);
return response()->json($group, 200);
return new GroupResource($group);
}
@ -106,28 +94,17 @@ public function update(Request $request, $id)
/**
* Associate the specified accounts with the group
*
* @param \App\Http\Requests\GroupAssignRequest $request
* @param \App\Group $group
* @return \Illuminate\Http\Response
*/
public function associateAccounts(Request $request)
public function assignAccounts(GroupAssignRequest $request, Group $group)
{
if( $request->input('groupId') > 0 ) {
$validated = $request->validated();
$twofaccounts = TwoFAccount::find($request->input('accountsIds'));
$group = Group::FindOrFail($request->input('groupId'));
$group->twofaccounts()->saveMany($twofaccounts);
return response()->json($group, 200);
}
else {
TwoFAccount::whereIn('id', $request->input('accountsIds'))
->update(['group_id' => NULL]);
return response()->json(['message' => 'moved to null'], 200);
}
$this->groupService->assign($validated['ids'], $group);
return response()->json($group, 200);
}
@ -140,7 +117,7 @@ public function associateAccounts(Request $request)
*/
public function destroy(Group $group)
{
$group->delete();
$this->groupService->delete($group->id);
return response()->json(null, 204);
}

View File

@ -10,9 +10,10 @@
use App\Http\Requests\TwoFAccountUpdateRequest;
use App\Http\Resources\TwoFAccountReadResource;
use App\Http\Resources\TwoFAccountStoreResource;
use App\Http\Requests\TwoFAccountBatchDestroyRequest;
use App\Http\Requests\TwoFAccountBatchRequest;
use App\Http\Requests\TwoFAccountUriRequest;
use App\Http\Requests\TwoFAccountDynamicRequest;
use App\Services\GroupService;
use App\Services\TwoFAccountService;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
@ -24,6 +25,11 @@ class TwoFAccountController extends Controller
*/
protected $twofaccountService;
/**
* The Group Service instance.
*/
protected $groupService;
/**
* Create a new controller instance.
@ -31,9 +37,10 @@ class TwoFAccountController extends Controller
* @param TwoFAccountService $twofaccountService
* @return void
*/
public function __construct(TwoFAccountService $twofaccountService)
public function __construct(TwoFAccountService $twofaccountService, GroupService $groupService)
{
$this->twofaccountService = $twofaccountService;
$this->groupService = $groupService;
}
@ -85,16 +92,7 @@ public function store(TwoFAccountDynamicRequest $request)
: $this->twofaccountService->createFromParameters($validated);
// Possible group association
$groupId = Options::get('defaultGroup') === '-1' ? (int) Options::get('activeGroup') : (int) Options::get('defaultGroup');
// 0 is the pseudo group 'All', only groups with id > 0 are true user groups
if( $groupId > 0 ) {
$group = Group::find($groupId);
if($group) {
$group->twofaccounts()->save($twofaccount);
}
}
$this->groupService->assign($twofaccount->id);
return (new TwoFAccountReadResource($twofaccount))
->response()
@ -131,7 +129,7 @@ public function reorder(TwoFAccountReorderRequest $request)
{
$validated = $request->validated();
$this->twofaccountService->saveOrder($validated['orderedIds']);
TwoFAccount::setNewOrder($validated['orderedIds']);
return response()->json(['message' => 'order saved'], 200);
}
@ -146,7 +144,7 @@ public function reorder(TwoFAccountReorderRequest $request)
*/
public function preview(TwoFAccountUriRequest $request)
{
$twofaccount = $this->twofaccountService->createFromUri($request->uri, $saveToDB = false);
$twofaccount = $this->twofaccountService->createFromUri($request->uri, false);
return new TwoFAccountStoreResource($twofaccount);
}
@ -202,6 +200,31 @@ public function count(Request $request)
}
/**
*
* Withdraw one or more accounts from their group
*
* @param \App\Http\Requests\TwoFAccountBatchRequest $request
* @param array $ids accounts ids to unassign
* @return \Illuminate\Http\Response
*/
public function withdraw(TwoFAccountBatchRequest $request)
{
$validated = $request->validated();
if ($this->tooManyIds($validated['ids'])) {
return response()->json([
'message' => 'bad request',
'reason' => [__('errors.too_many_ids')]
], 400);
}
$this->twofaccountService->withdraw($validated['ids']);
return response()->json([ 'message' => 'accounts withdrawn' ], 200);
}
/**
* Remove the specified resource from storage.
*
@ -219,25 +242,35 @@ public function destroy(TwoFAccount $twofaccount)
/**
* Remove the specified resources from storage.
*
* @param \App\Http\Requests\TwoFAccountBatchDestroyRequest $request
* @param \App\Http\Requests\TwoFAccountBatchRequest $request
* @return \Illuminate\Http\Response
*/
public function batchDestroy(TwoFAccountBatchDestroyRequest $request)
public function batchDestroy(TwoFAccountBatchRequest $request)
{
$validated = $request->validated();
$ids = explode(',', $validated['ids'], 100);
$nb = count($ids);
if ($nb > 99) {
if ($this->tooManyIds($validated['ids'])) {
return response()->json([
'message' => 'bad request',
'reason' => [__('errors.too_many_ids')]
], 400);
}
$this->twofaccountService->delete($ids);
$this->twofaccountService->delete($validated['ids']);
return response()->json(null, 204);
}
/**
* Checks ids length
*/
private function tooManyIds(string $ids) : bool
{
$arIds = explode(',', $ids, 100);
$nb = count($arIds);
return $nb > 99 ? true : false;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class GroupResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'twofaccounts_count' => $this->twofaccounts()->count(),
];
}
}

View File

@ -95,7 +95,7 @@ public function createFromParameters(array $data, bool $saveToDB = true) : TwoFA
* @param TwoFAccount $twofaccount The account
* @param array $data The parameters
*
* @return TwoFAccount The created account
* @return TwoFAccount The updated account
*/
public function update(TwoFAccount $twofaccount, array $data) : TwoFAccount
{
@ -166,10 +166,28 @@ public function getURI($data) : string
}
/**
* Withdraw one or more twofaccounts from their group
*
* @param int|array $ids twofaccount ids to free
*/
public function withdraw($ids) : void
{
$arIds = explode(',', $ids);
if ($arIds) {
TwoFAccount::whereIn('id', $arIds)
->update(
['group_id' => NULL]
);
}
}
/**
* Delete one or more twofaccounts
*
* @param int|array $ids twofaccount's ids to delete
* @param int|array $ids twofaccount ids to delete
*
* @return int The number of deleted
*/
@ -181,15 +199,6 @@ public function delete($ids) : int
}
/**
* Save TwoFAccounts order
*/
public function saveOrder(array $ids)
{
TwoFAccount::setNewOrder($ids);
}
// ########################################################################################################################
// ########################################################################################################################
// ########################################################################################################################