Created
February 26, 2022 16:43
-
-
Save vishwac09/eb655d9e766bceae6026b45c6efb0c36 to your computer and use it in GitHub Desktop.
RestResourceHandlers
This file contains hidden or 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
<?php | |
// pseudo code | |
class VehicleFetchRestResource { | |
// GET /vehicle | |
public function get(Request $request) { | |
try { | |
// #1 Authentication layer | |
// Validate if the request is coming from trusted source. | |
Authenticate($request); | |
// #2 Authorisation layer | |
// Does the current user have permission to fetch the resource. | |
Authorise($request); | |
// Validate the request, applicable to GET/POST/PUT/PATCH. | |
$query = $request->query->getAll(); | |
Validator::validate($query); | |
// Get the Request filters, applicable for GET requests. | |
// Filters have the following values sent from client. | |
// e.g. /vehicles?limit=10&page=1&featured=true | |
$filters = $this->getRequestFilters($query); | |
// Create a service/utility File to get the data. | |
$data = Service::fetchVehicles($filters); | |
// Massage the data if needed. | |
$data = Massage::alterData($data); | |
// Send the response. | |
return ResourceResponse(200, $data); | |
} catch (\Exception $e) { | |
throw new \Exception('Error', 400); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment