Last active
May 14, 2016 17:12
-
-
Save leemason/858f4034eeaab6e59aacf9608d12505c to your computer and use it in GitHub Desktop.
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 | |
| namespace App\Http\Controllers\Api\V1; | |
| use App\Http\Controllers\Controller; | |
| use App\Jobs\Domains\Screenshot; | |
| use App\Models\Domain; | |
| use Illuminate\Http\Request; | |
| class DomainsController extends Controller | |
| { | |
| // Other methods removed for brevity | |
| /** | |
| * Creates a new domain record | |
| */ | |
| public function postDomains(Request $request){ | |
| $this->validate($request, [ | |
| 'host' => 'string|url|unique:domains,host', | |
| ]); | |
| $user = $request->user(); | |
| // Notice how we provide the default/dummy screenshot here | |
| $domain = Domain::create([ | |
| 'host' => $request->get('host'), | |
| 'user_id' => $user->id, | |
| 'screenshot' => url('images/screenshot-in-progress.png') | |
| ]); | |
| $domain->users()->sync([$user->id]); | |
| // And here we create a new queued job to generate the "real" screenshot | |
| $this->dispatch(new Screenshot($domain)); | |
| // Finally return the domain | |
| return response()->json([ | |
| 'status' => 'success', | |
| 'domain' => $domain | |
| ]); | |
| } | |
| /** | |
| * Retrieves a domain record | |
| */ | |
| public function getDomain(Request $request, $domain){ | |
| $user = $request->user(); | |
| $domains = $user->domains()->where('host', $domain)->get(); | |
| if($domains->count() < 1){ | |
| abort(404, 'Not Found'); | |
| } | |
| $domain = $domains->first(); | |
| return response()->json([ | |
| 'status' => 'success', | |
| 'domain' => $domain | |
| ]); | |
| } | |
| } |
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 | |
| Route::group(['middleware' => ['api'], 'prefix' => 'api', 'as' => 'api.', 'namespace' => '\Api'], function () { | |
| Route::group(['prefix' => 'v1', 'as' => 'v1.', 'namespace' => '\V1'], function () { | |
| Route::post('domains', ['as' => 'domains.post', 'middleware' => 'auth:api', 'uses' => 'DomainsController@postDomains']); | |
| Route::get('domains/{domain}', ['as' => 'domain', 'middleware' => 'auth:api', 'uses' => 'DomainsController@getDomain']); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment