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 @@ namespace App\Http\Controllers;
use App\Group; use App\Group;
use App\TwoFAccount; 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; use Illuminate\Http\Request;
class GroupController extends Controller 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. * Display a listing of the resource.
* *
@ -15,45 +37,27 @@ class GroupController extends Controller
*/ */
public function index() public function index()
{ {
// The index method has to return the complete collection of groups $groups = $this->groupService->getAll();
// stored in db plus a pseudo group corresponding to 'all'
// Get the stored groups return GroupResource::collection($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());
} }
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \App\Http\Requests\GroupRequest $request
* @return \Illuminate\Http\Response * @return \App\Http\Resources\GroupResource
*/ */
public function store(Request $request) public function store(GroupStoreRequest $request)
{ {
$validated = $request->validated();
$this->validate($request, [ $group = $this->groupService->Create($validated);
'name' => 'required|string|max:32|unique:groups',
]);
$group = Group::create([ return (new GroupResource($group))
'name' => $request->name, ->response()
]); ->setStatusCode(201);
return response()->json($group, 201);
} }
@ -65,40 +69,24 @@ class GroupController extends Controller
*/ */
public function show(Group $group) public function show(Group $group)
{ {
return response()->json($group, 200); return new GroupResource($group);
} }
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \App\Http\Requests\GroupRequest $request
* @param \App\Group $twofaccount * @param \App\Group $group
* @return \Illuminate\Http\Response * @return \App\Http\Resources\GroupResource
*/ */
public function update(Request $request, $id) public function update(GroupStoreRequest $request, Group $group)
{ {
$validated = $request->validated();
$this->validate($request, [ $this->groupService->update($group, $validated);
'name' => 'required|string|max:32|unique:groups',
]);
// Here we catch a possible missing model exception in order to return new GroupResource($group);
// 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);
} }
@ -106,28 +94,17 @@ class GroupController extends Controller
/** /**
* Associate the specified accounts with the group * Associate the specified accounts with the group
* *
* @param \App\Http\Requests\GroupAssignRequest $request
* @param \App\Group $group * @param \App\Group $group
* @return \Illuminate\Http\Response * @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')); $this->groupService->assign($validated['ids'], $group);
$group = Group::FindOrFail($request->input('groupId'));
$group->twofaccounts()->saveMany($twofaccounts);
return response()->json($group, 200); return response()->json($group, 200);
}
else {
TwoFAccount::whereIn('id', $request->input('accountsIds'))
->update(['group_id' => NULL]);
return response()->json(['message' => 'moved to null'], 200);
}
} }
@ -140,7 +117,7 @@ class GroupController extends Controller
*/ */
public function destroy(Group $group) public function destroy(Group $group)
{ {
$group->delete(); $this->groupService->delete($group->id);
return response()->json(null, 204); return response()->json(null, 204);
} }

View File

@ -10,9 +10,10 @@ use App\Http\Requests\TwoFAccountStoreRequest;
use App\Http\Requests\TwoFAccountUpdateRequest; use App\Http\Requests\TwoFAccountUpdateRequest;
use App\Http\Resources\TwoFAccountReadResource; use App\Http\Resources\TwoFAccountReadResource;
use App\Http\Resources\TwoFAccountStoreResource; use App\Http\Resources\TwoFAccountStoreResource;
use App\Http\Requests\TwoFAccountBatchDestroyRequest; use App\Http\Requests\TwoFAccountBatchRequest;
use App\Http\Requests\TwoFAccountUriRequest; use App\Http\Requests\TwoFAccountUriRequest;
use App\Http\Requests\TwoFAccountDynamicRequest; use App\Http\Requests\TwoFAccountDynamicRequest;
use App\Services\GroupService;
use App\Services\TwoFAccountService; use App\Services\TwoFAccountService;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -24,6 +25,11 @@ class TwoFAccountController extends Controller
*/ */
protected $twofaccountService; protected $twofaccountService;
/**
* The Group Service instance.
*/
protected $groupService;
/** /**
* Create a new controller instance. * Create a new controller instance.
@ -31,9 +37,10 @@ class TwoFAccountController extends Controller
* @param TwoFAccountService $twofaccountService * @param TwoFAccountService $twofaccountService
* @return void * @return void
*/ */
public function __construct(TwoFAccountService $twofaccountService) public function __construct(TwoFAccountService $twofaccountService, GroupService $groupService)
{ {
$this->twofaccountService = $twofaccountService; $this->twofaccountService = $twofaccountService;
$this->groupService = $groupService;
} }
@ -85,16 +92,7 @@ class TwoFAccountController extends Controller
: $this->twofaccountService->createFromParameters($validated); : $this->twofaccountService->createFromParameters($validated);
// Possible group association // Possible group association
$groupId = Options::get('defaultGroup') === '-1' ? (int) Options::get('activeGroup') : (int) Options::get('defaultGroup'); $this->groupService->assign($twofaccount->id);
// 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);
}
}
return (new TwoFAccountReadResource($twofaccount)) return (new TwoFAccountReadResource($twofaccount))
->response() ->response()
@ -131,7 +129,7 @@ class TwoFAccountController extends Controller
{ {
$validated = $request->validated(); $validated = $request->validated();
$this->twofaccountService->saveOrder($validated['orderedIds']); TwoFAccount::setNewOrder($validated['orderedIds']);
return response()->json(['message' => 'order saved'], 200); return response()->json(['message' => 'order saved'], 200);
} }
@ -146,7 +144,7 @@ class TwoFAccountController extends Controller
*/ */
public function preview(TwoFAccountUriRequest $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); return new TwoFAccountStoreResource($twofaccount);
} }
@ -202,6 +200,31 @@ class TwoFAccountController extends Controller
} }
/**
*
* 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. * Remove the specified resource from storage.
* *
@ -219,25 +242,35 @@ class TwoFAccountController extends Controller
/** /**
* Remove the specified resources from storage. * Remove the specified resources from storage.
* *
* @param \App\Http\Requests\TwoFAccountBatchDestroyRequest $request * @param \App\Http\Requests\TwoFAccountBatchRequest $request
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function batchDestroy(TwoFAccountBatchDestroyRequest $request) public function batchDestroy(TwoFAccountBatchRequest $request)
{ {
$validated = $request->validated(); $validated = $request->validated();
$ids = explode(',', $validated['ids'], 100); if ($this->tooManyIds($validated['ids'])) {
$nb = count($ids);
if ($nb > 99) {
return response()->json([ return response()->json([
'message' => 'bad request', 'message' => 'bad request',
'reason' => [__('errors.too_many_ids')] 'reason' => [__('errors.too_many_ids')]
], 400); ], 400);
} }
$this->twofaccountService->delete($ids); $this->twofaccountService->delete($validated['ids']);
return response()->json(null, 204); 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 @@ class TwoFAccountService
* @param TwoFAccount $twofaccount The account * @param TwoFAccount $twofaccount The account
* @param array $data The parameters * @param array $data The parameters
* *
* @return TwoFAccount The created account * @return TwoFAccount The updated account
*/ */
public function update(TwoFAccount $twofaccount, array $data) : TwoFAccount public function update(TwoFAccount $twofaccount, array $data) : TwoFAccount
{ {
@ -166,10 +166,28 @@ class TwoFAccountService
} }
/**
* 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 * 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 * @return int The number of deleted
*/ */
@ -181,15 +199,6 @@ class TwoFAccountService
} }
/**
* Save TwoFAccounts order
*/
public function saveOrder(array $ids)
{
TwoFAccount::setNewOrder($ids);
}
// ######################################################################################################################## // ########################################################################################################################
// ######################################################################################################################## // ########################################################################################################################
// ######################################################################################################################## // ########################################################################################################################