Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Route;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Database\Schema\Blueprint;
- /*
- |--------------------------------------------------------------------------
- | API Routes
- |--------------------------------------------------------------------------
- |
- | Here is where you can register API routes for your application. These
- | routes are loaded by the RouteServiceProvider within a group which
- | is assigned the "api" middleware group. Enjoy building your API!
- |
- */
- Route::get('/hello', function () {
- return( 'hello');
- });
- /* initialize the table for REST API service */
- Route::get('/init', function () {
- Schema::dropIfExists('staff');
- if (!Schema::hasTable('staff')) {
- Schema::create('staff', function (Blueprint $table) {
- $table->id();
- $table->string('name');
- $table->string('email')->unique();
- $table->string('dept');
- $table->string('bran');
- });
- DB::insert('insert into staff (name,email,dept,bran)
- values (?,?,?,?)',
- ['john',
- 'D1',
- 'D1B1']);
- DB::insert('insert into staff (name,email,dept,bran)
- values (?,?,?,?)',
- ['nancy',
- 'D2',
- 'D2B1']);
- }
- Schema::dropIfExists('department');
- if (!Schema::hasTable('department')) {
- Schema::create('department', function (Blueprint $table) {
- $table->id();
- $table->string('code')->unique();
- $table->string('name');
- });
- DB::insert('insert into department (code,name)
- values (?,?)',
- ['D1',
- 'Department 1']);
- DB::insert('insert into department (code,name)
- values (?,?)',
- ['D2',
- 'Department 2']);
- }
- Schema::dropIfExists('branch');
- if (!Schema::hasTable('branch')) {
- Schema::create('branch', function (Blueprint $table) {
- $table->id();
- $table->string('deptcode');
- $table->string('code')->unique();
- $table->string('name');
- });
- DB::insert('insert into branch (deptcode,code,name)
- values (?,?,?)',
- ['D1',
- 'D1B1','Branch 1 of D1']);
- DB::insert('insert into branch (deptcode,code,name)
- values (?,?,?)',
- ['D1',
- 'D1B2','Branch 2 of D1']);
- DB::insert('insert into branch (deptcode,code,name)
- values (?,?,?)',
- ['D2',
- 'D2B1','Branch 1 of D2']);
- DB::insert('insert into branch (deptcode,code,name)
- values (?,?,?)',
- ['D2',
- 'D2B2','Branch 2 of D2']);
- }
- $json_data = '{"table":["staff","department","branch"],"status":"init"}';
- $result = json_decode($json_data);
- return response()->json($result, 201);
- });
- /* TESTCHECK : the student table should appear in the list
- after the REST init command is called */
- Route::get('/show_tables',function(){
- //$tables = DB::select('SHOW TABLES');
- $tables = DB::select("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;");
- echo "tables:<br/>";
- foreach($tables as $table){
- $arry = (array) $table;
- foreach ($arry as $value) {
- echo $value."<br/>";
- }
- }
- });
- /* TESTCHECK: call getall via web browser and REST client */
- Route::match(array('GET', 'POST'),'/getall', function () {
- $student = DB::select('select * from student');
- return response()->json($student, 200);
- });
- /* TESTCHECK: call REST GET command via web browser */
- Route::get('/insert/{name}/{email}',function($name,$email){
- $timestamp = time();
- DB::insert('insert into student (id, name,email)
- values (?, ?,?)',
- [$timestamp,$name.'_'.$timestamp,$email.'_'.$timestamp]);
- echo "record inserted.<br/>";
- });
- /* */
- /* TESTCHECK: call the following REST POST commands via REST client */
- /* */
- Route::post('/insert',function(Request $request)
- {
- $payload = json_decode($request->getContent(), true);
- try {
- // Get data here, eg. make an external API request or DB query
- $response = [
- 'name' => $payload['name'],
- 'email' => $payload['email']
- ];
- $timestamp = time();
- DB::insert('insert into student (id, name,email)
- values (?, ?,?)',
- [$timestamp,$response['name'].'_'.$timestamp
- ,$response['email'].'_'.$timestamp]);
- } catch (\GuzzleHttp\Exception\BadResponseException $e) {
- $errorResJson = $e
- ->getResponse()
- ->getBody()
- ->getContents();
- $errorRes = json_decode(stripslashes($errorResJson), true);
- // Return error
- return response()->json(
- [
- 'message' => 'error',
- 'data' => '$errorRes'
- ],
- $errorRes['response']['code']
- );
- }
- // Return success
- return response()->json(
- [
- 'status' => '200',
- 'data' => '$response',
- 'message' => 'success'
- ],
- 200
- );
- }
- );
- Route::post('/update',function(Request $request)
- {
- $payload = json_decode($request->getContent(), true);
- try {
- $response = [
- 'id' => $payload['id'],
- 'name' => $payload['name'],
- 'email' => $payload['email']
- ];
- $affected = DB::update(
- 'update student set name = ?, email=? where id = ?',
- [$response['name'],$response['email'],$response['id']]);
- } catch (\GuzzleHttp\Exception\BadResponseException $e) {
- $errorResJson = $e
- ->getResponse()
- ->getBody()
- ->getContents();
- $errorRes = json_decode(stripslashes($errorResJson), true);
- // Return error
- return response()->json(
- [
- 'message' => 'error',
- 'data' => '$errorRes'
- ],
- $errorRes['response']['code']
- );
- }
- // Return success
- return response()->json(
- [
- 'status' => '200',
- 'data' => $affected ,
- 'message' => 'success'
- ],
- 200
- );
- }
- );
- Route::post('/delete',function(Request $request)
- {
- $payload = json_decode($request->getContent(), true);
- try {
- $response = [
- 'id' => $payload['id']
- ];
- $deleted = DB::delete('delete from student where id = ?',
- [$response['id']]);
- } catch (\GuzzleHttp\Exception\BadResponseException $e) {
- $errorResJson = $e
- ->getResponse()
- ->getBody()
- ->getContents();
- $errorRes = json_decode(stripslashes($errorResJson), true);
- // Return error
- return response()->json(
- [
- 'message' => 'error',
- 'data' => '$errorRes'
- ],
- $errorRes['response']['code']
- );
- }
- // Return success
- return response()->json(
- [
- 'status' => '200',
- 'data' => $deleted ,
- 'message' => 'success'
- ],
- 200
- );
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment