Skip to content

Commit

Permalink
Rename base API controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Nov 30, 2023
1 parent 95491e3 commit d61ea6d
Show file tree
Hide file tree
Showing 17 changed files with 372 additions and 263 deletions.
67 changes: 39 additions & 28 deletions app/Http/Controllers/Admin/AddressPools/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,53 @@

namespace Convoy\Http\Controllers\Admin\AddressPools;

use Convoy\Enums\Network\AddressType;
use Convoy\Exceptions\Repository\Proxmox\ProxmoxConnectionException;
use Convoy\Http\Controllers\ApiController;
use Convoy\Http\Requests\Admin\AddressPools\Addresses\StoreAddressRequest;
use Convoy\Http\Requests\Admin\AddressPools\Addresses\UpdateAddressRequest;
use Convoy\Jobs\Server\SyncNetworkSettings;
use Convoy\Models\Address;
use Illuminate\Http\Request;
use Convoy\Models\AddressPool;
use Convoy\Enums\Network\AddressType;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;
use Convoy\Models\Filters\AllowedNullableFilter;
use Convoy\Models\Filters\FiltersAddress;
use Convoy\Repositories\Eloquent\AddressRepository;
use Convoy\Services\Servers\NetworkService;
use Convoy\Jobs\Server\SyncNetworkSettings;
use Illuminate\Database\ConnectionInterface;
use Convoy\Models\Filters\AllowedNullableFilter;
use Convoy\Transformers\Admin\AddressTransformer;
use Convoy\Repositories\Eloquent\AddressRepository;
use Convoy\Http\Controllers\ApplicationApiController;
use Convoy\Exceptions\Repository\Proxmox\ProxmoxConnectionException;
use Convoy\Http\Requests\Admin\AddressPools\Addresses\StoreAddressRequest;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Convoy\Http\Requests\Admin\AddressPools\Addresses\UpdateAddressRequest;

class AddressController extends ApplicationApiController
class AddressController extends ApiController
{
public function __construct(private NetworkService $networkService, private AddressRepository $repository, private ConnectionInterface $connection)
public function __construct(
private NetworkService $networkService, private AddressRepository $repository,
private ConnectionInterface $connection,
)
{
}

public function index(Request $request, AddressPool $addressPool)
{
$addresses = QueryBuilder::for($addressPool->addresses())
->with('server')
->defaultSort('-id')
->allowedFilters(
[AllowedFilter::exact('address'), AllowedFilter::exact('type'), AllowedFilter::custom(
'*',
new FiltersAddress,
), AllowedNullableFilter::exact('server_id')],
)
->paginate(min($request->query('per_page', 50), 100))->appends($request->query());

return fractal($addresses, new AddressTransformer)->parseIncludes($request->include)->respond();
->with('server')
->defaultSort('-id')
->allowedFilters(
[AllowedFilter::exact('address'), AllowedFilter::exact(
'type',
), AllowedFilter::custom(
'*',
new FiltersAddress(),
), AllowedNullableFilter::exact('server_id')],
)
->paginate(min($request->query('per_page', 50), 100))->appends(
$request->query(),
);

return fractal($addresses, new AddressTransformer())->parseIncludes($request->include)
->respond();
}

public function store(StoreAddressRequest $request, AddressPool $addressPool)
Expand All @@ -63,7 +71,8 @@ public function store(StoreAddressRequest $request, AddressPool $addressPool)
return $address;
});

return fractal($address, new AddressTransformer)->parseIncludes($request->include)->respond();
return fractal($address, new AddressTransformer())->parseIncludes($request->include)
->respond();
}

if ($request->boolean('is_bulk_action')) {
Expand Down Expand Up @@ -99,7 +108,8 @@ public function store(StoreAddressRequest $request, AddressPool $addressPool)
}
}

public function update(UpdateAddressRequest $request, AddressPool $addressPool, Address $address)
public function update(UpdateAddressRequest $request, AddressPool $addressPool, Address $address,
)
{
$address = $this->connection->transaction(function () use ($request, $address) {
$oldLinkedServer = $address->server;
Expand Down Expand Up @@ -135,7 +145,8 @@ public function update(UpdateAddressRequest $request, AddressPool $addressPool,
return $address;
});

return fractal($address, new AddressTransformer)->parseIncludes($request->include)->respond();
return fractal($address, new AddressTransformer())->parseIncludes($request->include)
->respond();
}

public function destroy(AddressPool $addressPool, Address $address)
Expand Down
58 changes: 36 additions & 22 deletions app/Http/Controllers/Admin/AddressPools/AddressPoolController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,57 @@

namespace Convoy\Http\Controllers\Admin\AddressPools;

use Illuminate\Http\Request;
use Convoy\Http\Controllers\ApiController;
use Convoy\Http\Requests\Admin\AddressPools\StoreAddressPoolRequest;
use Convoy\Http\Requests\Admin\AddressPools\UpdateAddressPoolRequest;
use Convoy\Models\AddressPool;
use Spatie\QueryBuilder\QueryBuilder;
use Convoy\Models\Filters\FiltersNode;
use Spatie\QueryBuilder\AllowedFilter;
use Convoy\Models\Filters\FiltersAddressPool;
use Convoy\Transformers\Admin\NodeTransformer;
use Convoy\Http\Controllers\ApplicationApiController;
use Convoy\Models\Filters\FiltersNode;
use Convoy\Transformers\Admin\AddressPoolTransformer;
use Convoy\Http\Requests\Admin\AddressPools\StoreAddressPoolRequest;
use Convoy\Http\Requests\Admin\AddressPools\UpdateAddressPoolRequest;
use Convoy\Transformers\Admin\NodeTransformer;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class AddressPoolController extends ApplicationApiController
class AddressPoolController extends ApiController
{
public function index(Request $request)
{
$addressPools = QueryBuilder::for(AddressPool::query())
->withCount(['addresses', 'nodes'])
->defaultSort('-id')
->allowedFilters(['name', AllowedFilter::custom('*', new FiltersAddressPool)])
->paginate(min($request->query('per_page', 50), 100))->appends($request->query());

return fractal($addressPools, new AddressPoolTransformer)->respond();
->withCount(['addresses', 'nodes'])
->defaultSort('-id')
->allowedFilters(
['name', AllowedFilter::custom(
'*', new FiltersAddressPool(),
)],
)
->paginate(min($request->query('per_page', 50), 100))->appends(
$request->query(),
);

return fractal($addressPools, new AddressPoolTransformer())->respond();
}

public function show(AddressPool $addressPool)
{
$addressPool->loadCount(['addresses', 'nodes']);

return fractal($addressPool, new AddressPoolTransformer)->respond();
return fractal($addressPool, new AddressPoolTransformer())->respond();
}

public function getAttachedNodes(Request $request, AddressPool $addressPool)
{
$nodes = QueryBuilder::for($addressPool->nodes())
->withCount('servers')
->allowedFilters(['name', 'fqdn', AllowedFilter::exact('location_id'), AllowedFilter::custom('*', new FiltersNode)])
->paginate(min($request->query('per_page', 50), 100))->appends($request->query());
->withCount('servers')
->allowedFilters(
['name', 'fqdn', AllowedFilter::exact(
'location_id',
), AllowedFilter::custom('*', new FiltersNode())],
)
->paginate(min($request->query('per_page', 50), 100))->appends(
$request->query(),
);

return fractal($nodes, new NodeTransformer())->respond();
}
Expand All @@ -51,7 +63,7 @@ public function store(StoreAddressPoolRequest $request)
$pool->nodes()->attach($request->node_ids);
$pool->loadCount(['addresses', 'nodes']);

return fractal($pool, new AddressPoolTransformer)->respond();
return fractal($pool, new AddressPoolTransformer())->respond();
}

public function update(UpdateAddressPoolRequest $request, AddressPool $addressPool)
Expand All @@ -60,15 +72,17 @@ public function update(UpdateAddressPoolRequest $request, AddressPool $addressPo
$addressPool->nodes()->sync($request->node_ids);
$addressPool->loadCount(['addresses', 'nodes']);

return fractal($addressPool, new AddressPoolTransformer)->respond();
return fractal($addressPool, new AddressPoolTransformer())->respond();
}

public function destroy(AddressPool $addressPool)
{
$addressPool->loadCount('nodes');

if ($addressPool->nodes_count > 0) {
throw new AccessDeniedHttpException('This address pool cannot be deleted while still allocated to nodes.');
throw new AccessDeniedHttpException(
'This address pool cannot be deleted while still allocated to nodes.',
);
}

$addressPool->delete();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Admin/CotermController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Convoy\Http\Controllers\Admin;

use Convoy\Http\Controllers\ApplicationApiController;
use Convoy\Http\Controllers\ApiController;
use Convoy\Http\Requests\Admin\Coterms\StoreCotermRequest;
use Convoy\Http\Requests\Admin\Coterms\UpdateAttachedNodesRequest;
use Convoy\Http\Requests\Admin\Coterms\UpdateCotermRequest;
Expand All @@ -16,7 +16,7 @@
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

class CotermController extends ApplicationApiController
class CotermController extends ApiController
{
public function __construct(private CotermTokenCreationService $cotermTokenCreator)
{
Expand Down
26 changes: 16 additions & 10 deletions app/Http/Controllers/Admin/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

namespace Convoy\Http\Controllers\Admin;

use Convoy\Http\Controllers\ApiController;
use Convoy\Http\Requests\Admin\LocationFormRequest;
use Convoy\Models\Filters\FiltersLocation;
use Convoy\Models\Location;
use Convoy\Transformers\Admin\LocationTransformer;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;
use Convoy\Models\Filters\FiltersLocation;
use Convoy\Transformers\Admin\LocationTransformer;
use Convoy\Http\Requests\Admin\LocationFormRequest;
use Convoy\Http\Controllers\ApplicationApiController;
use Spatie\QueryBuilder\QueryBuilder;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class LocationController extends ApplicationApiController
class LocationController extends ApiController
{
public function index(Request $request)
{
$locations = QueryBuilder::for(Location::query())
->withCount(['nodes', 'servers'])
->withCount(['nodes', 'servers'])
// @phpstan-ignore-next-line
->allowedFilters(['short_code', AllowedFilter::custom('*', new FiltersLocation)])
->paginate(min($request->query('per_page', 50), 100))->appends($request->query());
->allowedFilters(
['short_code', AllowedFilter::custom('*', new FiltersLocation())],
)
->paginate(min($request->query('per_page', 50), 100))->appends(
$request->query(),
);

return fractal($locations, new LocationTransformer())->respond();
}
Expand All @@ -45,7 +49,9 @@ public function destroy(Location $location)

// @phpstan-ignore-next-line
if ($location->nodes_count > 0) {
throw new BadRequestHttpException('The location cannot be deleted with nodes still associated.');
throw new BadRequestHttpException(
'The location cannot be deleted with nodes still associated.',
);
}

$location->delete();
Expand Down
49 changes: 28 additions & 21 deletions app/Http/Controllers/Admin/Nodes/IsoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,55 @@

namespace Convoy\Http\Controllers\Admin\Nodes;

use Convoy\Models\ISO;
use Convoy\Models\Node;
use Illuminate\Http\Request;
use Convoy\Data\Helpers\ChecksumData;
use Spatie\QueryBuilder\QueryBuilder;
use Illuminate\Support\Facades\Validator;
use Convoy\Services\Nodes\Isos\IsoService;
use Convoy\Enums\Helpers\ChecksumAlgorithm;
use Convoy\Transformers\Admin\IsoTransformer;
use Convoy\Http\Controllers\ApplicationApiController;
use Convoy\Transformers\Admin\FileMetadataTransformer;
use Convoy\Http\Controllers\ApiController;
use Convoy\Http\Requests\Admin\Nodes\Isos\StoreIsoRequest;
use Convoy\Http\Requests\Admin\Nodes\Isos\UpdateIsoRequest;
use Convoy\Models\ISO;
use Convoy\Models\Node;
use Convoy\Repositories\Proxmox\Node\ProxmoxStorageRepository;
use Convoy\Services\Nodes\Isos\IsoService;
use Convoy\Transformers\Admin\FileMetadataTransformer;
use Convoy\Transformers\Admin\IsoTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Spatie\QueryBuilder\QueryBuilder;

class IsoController extends ApplicationApiController
class IsoController extends ApiController
{
public function __construct(private IsoService $isoService, private ProxmoxStorageRepository $repository)
public function __construct(
private IsoService $isoService, private ProxmoxStorageRepository $repository,
)
{
}

public function index(Node $node, Request $request)
{
$isos = QueryBuilder::for(ISO::query())
->where('iso_library.node_id', $node->id)
->allowedFilters(['name'])
->paginate(min($request->query('per_page', 50), 100))->appends($request->query());
->where('iso_library.node_id', $node->id)
->allowedFilters(['name'])
->paginate(min($request->query('per_page', 50), 100))->appends(
$request->query(),
);

return fractal($isos, new IsoTransformer)->respond();
return fractal($isos, new IsoTransformer())->respond();
}

public function store(StoreIsoRequest $request, Node $node)
{
$shouldDownload = $request->boolean('should_download');

if($shouldDownload) {
$checksumData = (bool) $request->checksum_algorithum ? ChecksumData::from([
if ($shouldDownload) {
$checksumData = (bool)$request->checksum_algorithum ? ChecksumData::from([
'algorithm' => ChecksumAlgorithm::from($request->checksum_algorithum),
'checksum' => $request->checksum,
]) : null;

$iso = $this->isoService->download($node, $request->name, $request->file_name, $request->link, $checksumData, $request->hidden);
$iso = $this->isoService->download(
$node, $request->name, $request->file_name, $request->link, $checksumData,
$request->hidden,
);
} else {
$isoFromProxmox = $this->isoService->getIso($node, $request->file_name);

Expand All @@ -57,14 +64,14 @@ public function store(StoreIsoRequest $request, Node $node)
]);
}

return fractal($iso, new IsoTransformer)->respond();
return fractal($iso, new IsoTransformer())->respond();
}

public function update(UpdateIsoRequest $request, Node $node, ISO $iso)
{
$iso->update($request->validated());

return fractal($iso, new IsoTransformer)->respond();
return fractal($iso, new IsoTransformer())->respond();
}

public function destroy(Node $node, ISO $iso)
Expand All @@ -84,6 +91,6 @@ public function queryLink(Request $request, Node $node)

$metadata = $this->repository->setNode($node)->getFileMetadata($request->link);

return fractal($metadata, new FileMetadataTransformer)->respond();
return fractal($metadata, new FileMetadataTransformer())->respond();
}
}
Loading

0 comments on commit d61ea6d

Please sign in to comment.