...
 
Commits (7)
......@@ -2,6 +2,7 @@
namespace App\Exceptions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
......@@ -70,7 +71,10 @@ class Handler extends ExceptionHandler
'fields' => $errors,
]
])->setStatusCode(422);
} elseif ($exception instanceof NotFoundHttpException) {
} elseif (
$exception instanceof NotFoundHttpException ||
$exception instanceof ModelNotFoundException) {
return response()->json([
'error' => [
'code' => 404,
......
......@@ -5,7 +5,6 @@ namespace App\Http\Controllers;
use App\Http\Requests\CreateApplicationRequest;
use App\Http\Resources\ApplicationResource;
use App\Models\Application;
use Illuminate\Support\Str;
class CreateApplicationController extends Controller
{
......@@ -13,7 +12,6 @@ class CreateApplicationController extends Controller
{
$application = new Application();
$application->qr_token = $this->generateUniqueQrToken();
$application->device_token = $request->device_token;
$application->first_name = $request->first_name;
......@@ -21,13 +19,9 @@ class CreateApplicationController extends Controller
$application->last_name = $request->last_name;
$application->out_address = $request->out_address;
$application->out_latitude = $request->out_latitude;
$application->out_longitude = $request->out_longitude;
$application->out_datetime = $request->out_datetime;
$application->visiting_address_and_name = $request->visiting_address_and_name;
$application->visiting_latitude = $request->visiting_latitude;
$application->visiting_longitude = $request->visiting_longitude;
$application->visiting_reason = $request->visiting_reason;
$application->planned_return_datetime = $request->planned_return_datetime;
......@@ -35,18 +29,4 @@ class CreateApplicationController extends Controller
return ApplicationResource::make($application);
}
private function generateUniqueQrToken(): string
{
do {
$qr_token = Str::random(32);
} while ($this->applicationWithGivenQrTokenExist($qr_token));
return $qr_token;
}
private function applicationWithGivenQrTokenExist(string $qr_token): bool
{
return Application::qrToken($qr_token)->count() > 0;
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Requests\DeleteAllApplicationsRequest;
use App\Models\Application;
use Illuminate\Http\Resources\Json\JsonResource;
class DeleteAllApplicationsController extends Controller
{
public function __invoke(DeleteAllApplicationsRequest $request)
{
Application::deviceToken($request->device_token)->delete();
return JsonResource::make([
'deleted' => true,
]);
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Requests\DeleteSingleApplicationRequest;
use App\Models\Application;
use Illuminate\Http\Resources\Json\JsonResource;
class DeleteApplicationByIdController extends Controller
{
public function __invoke(DeleteSingleApplicationRequest $request, Application $application)
{
abort_unless($request->device_token === $application->device_token, 404);
$this->deleteApplication($application);
return JsonResource::make([
'deleted' => true,
]);
}
private function deleteApplication(Application $application)
{
if (!$application->is_finished) {
return;
}
try {
$application->delete();
} catch (\Exception $exception) {
}
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Resources\ApplicationResource;
use App\Models\Application;
class QrScannedApplicationsController extends Controller
{
public function __invoke(string $qr_token)
{
$application = Application::qrToken($qr_token)->first();
return ApplicationResource::make($application);
}
}
......@@ -13,12 +13,8 @@ use Illuminate\Foundation\Http\FormRequest;
*
* @property string out_datetime
* @property string out_address
* @property string out_latitude
* @property string out_longitude
*
* @property string visiting_address_and_name
* @property string visiting_latitude
* @property string visiting_longitude
* @property string visiting_reason
* @property string planned_return_datetime
*/
......@@ -55,24 +51,11 @@ class CreateApplicationRequest extends FormRequest
'out_address' => [
'required',
],
'out_latitude' => [
'nullable',
],
'out_longitude' => [
'nullable',
],
'visiting_address_and_name' => [
'required',
],
'visiting_latitude' => [
'nullable',
],
'visiting_longitude' => [
'nullable',
],
'visiting_reason' => [
'required',
],
......
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class DeleteAllApplicationsRequest
*
* @property string device_token
*
* @package App\Http\Requests
*/
class DeleteAllApplicationsRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'device_token' => [
'required',
],
];
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class DeleteSingleApplicationRequest
*
* @property string device_token
*
* @package App\Http\Requests
*/
class DeleteSingleApplicationRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'device_token' => [
'required',
],
];
}
}
......@@ -2,7 +2,6 @@
namespace App\Http\Resources;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
class ApplicationResource extends JsonResource
......@@ -11,19 +10,19 @@ class ApplicationResource extends JsonResource
{
return [
"id" => $this->id,
"qr_token" => $this->qr_token,
"first_name" => $this->first_name,
"middle_name" => $this->middle_name,
"last_name" => $this->last_name,
"out_address" => $this->out_address,
"out_datetime" => $this->outDatetime(),
"out_latitude" => $this->out_latitude,
"out_longitude" => $this->out_longitude,
"visiting_address_and_name" => $this->visiting_address_and_name,
"visiting_latitude" => $this->visiting_latitude,
"visiting_longitude" => $this->visiting_longitude,
"visiting_reason" => $this->visiting_reason,
"planned_return_datetime" => $this->plannedReturnDatetime(),
"finished_at" => $this->finishedAtDatetime(),
"created_at" => $this->createdAtDatetime(),
];
......
......@@ -17,12 +17,8 @@ use Illuminate\Database\Eloquent\SoftDeletes;
*
* @property Carbon out_datetime
* @property string out_address
* @property double out_latitude
* @property double out_longitude
*
* @property string visiting_address_and_name
* @property double visiting_latitude
* @property double visiting_longitude
* @property string visiting_reason
* @property Carbon planned_return_datetime
*
......@@ -30,8 +26,6 @@ use Illuminate\Database\Eloquent\SoftDeletes;
*/
class Application extends Model
{
use SoftDeletes;
protected $table = 'applications';
protected $fillable = [
......
......@@ -2636,16 +2636,16 @@
},
{
"name": "symfony/http-foundation",
"version": "v5.0.5",
"version": "v5.0.7",
"source": {
"type": "git",
"url": "https://github.com/symfony/http-foundation.git",
"reference": "6f9c2ba72f4295d7ce6cf9f79dbb18036291d335"
"reference": "26fb006a2c7b6cdd23d52157b05f8414ffa417b6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/http-foundation/zipball/6f9c2ba72f4295d7ce6cf9f79dbb18036291d335",
"reference": "6f9c2ba72f4295d7ce6cf9f79dbb18036291d335",
"url": "https://api.github.com/repos/symfony/http-foundation/zipball/26fb006a2c7b6cdd23d52157b05f8414ffa417b6",
"reference": "26fb006a2c7b6cdd23d52157b05f8414ffa417b6",
"shasum": ""
},
"require": {
......@@ -2687,7 +2687,7 @@
],
"description": "Symfony HttpFoundation Component",
"homepage": "https://symfony.com",
"time": "2020-02-14T07:43:07+00:00"
"time": "2020-03-30T14:14:32+00:00"
},
{
"name": "symfony/http-kernel",
......@@ -2845,16 +2845,16 @@
},
{
"name": "symfony/mime",
"version": "v5.0.5",
"version": "v5.0.7",
"source": {
"type": "git",
"url": "https://github.com/symfony/mime.git",
"reference": "9b3e5b5e58c56bbd76628c952d2b78556d305f3c"
"reference": "481b7d6da88922fb1e0d86a943987722b08f3955"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/mime/zipball/9b3e5b5e58c56bbd76628c952d2b78556d305f3c",
"reference": "9b3e5b5e58c56bbd76628c952d2b78556d305f3c",
"url": "https://api.github.com/repos/symfony/mime/zipball/481b7d6da88922fb1e0d86a943987722b08f3955",
"reference": "481b7d6da88922fb1e0d86a943987722b08f3955",
"shasum": ""
},
"require": {
......@@ -2903,7 +2903,7 @@
"mime",
"mime-type"
],
"time": "2020-02-04T09:41:09+00:00"
"time": "2020-03-27T16:56:45+00:00"
},
{
"name": "symfony/options-resolver",
......@@ -3078,16 +3078,16 @@
},
{
"name": "symfony/polyfill-intl-idn",
"version": "v1.14.0",
"version": "v1.15.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-intl-idn.git",
"reference": "6842f1a39cf7d580655688069a03dd7cd83d244a"
"reference": "47bd6aa45beb1cd7c6a16b7d1810133b728bdfcf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/6842f1a39cf7d580655688069a03dd7cd83d244a",
"reference": "6842f1a39cf7d580655688069a03dd7cd83d244a",
"url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/47bd6aa45beb1cd7c6a16b7d1810133b728bdfcf",
"reference": "47bd6aa45beb1cd7c6a16b7d1810133b728bdfcf",
"shasum": ""
},
"require": {
......@@ -3101,7 +3101,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.14-dev"
"dev-master": "1.15-dev"
}
},
"autoload": {
......@@ -3136,20 +3136,20 @@
"portable",
"shim"
],
"time": "2020-01-17T12:01:36+00:00"
"time": "2020-03-09T19:04:49+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.14.0",
"version": "v1.15.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "34094cfa9abe1f0f14f48f490772db7a775559f2"
"reference": "81ffd3a9c6d707be22e3012b827de1c9775fc5ac"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/34094cfa9abe1f0f14f48f490772db7a775559f2",
"reference": "34094cfa9abe1f0f14f48f490772db7a775559f2",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/81ffd3a9c6d707be22e3012b827de1c9775fc5ac",
"reference": "81ffd3a9c6d707be22e3012b827de1c9775fc5ac",
"shasum": ""
},
"require": {
......@@ -3161,7 +3161,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.14-dev"
"dev-master": "1.15-dev"
}
},
"autoload": {
......@@ -3195,20 +3195,20 @@
"portable",
"shim"
],
"time": "2020-01-13T11:15:53+00:00"
"time": "2020-03-09T19:04:49+00:00"
},
{
"name": "symfony/polyfill-php72",
"version": "v1.14.0",
"version": "v1.15.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php72.git",
"reference": "46ecacf4751dd0dc81e4f6bf01dbf9da1dc1dadf"
"reference": "37b0976c78b94856543260ce09b460a7bc852747"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php72/zipball/46ecacf4751dd0dc81e4f6bf01dbf9da1dc1dadf",
"reference": "46ecacf4751dd0dc81e4f6bf01dbf9da1dc1dadf",
"url": "https://api.github.com/repos/symfony/polyfill-php72/zipball/37b0976c78b94856543260ce09b460a7bc852747",
"reference": "37b0976c78b94856543260ce09b460a7bc852747",
"shasum": ""
},
"require": {
......@@ -3217,7 +3217,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.14-dev"
"dev-master": "1.15-dev"
}
},
"autoload": {
......@@ -3250,7 +3250,7 @@
"portable",
"shim"
],
"time": "2020-01-13T11:15:53+00:00"
"time": "2020-02-27T09:26:54+00:00"
},
{
"name": "symfony/polyfill-php73",
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RemoveUnnecessaryFieldsFromApplicationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropColumn('qr_token');
$table->dropColumn('out_latitude');
$table->dropColumn('out_longitude');
$table->dropColumn('visiting_latitude');
$table->dropColumn('visiting_longitude');
$table->dropColumn('deleted_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->string('qr_token')->after('id');
$table->double('out_latitude')->nullable()->after('out_datetime');
$table->double('out_longitude')->nullable()->after('out_latitude');
$table->double('visiting_latitude')->nullable()->after('visiting_address_and_name');
$table->double('visiting_longitude')->nullable()->after('visiting_latitude');
$table->softDeletes()->after('updated_at');
});
}
}
......@@ -3,9 +3,10 @@
use App\Http\Controllers\CreateApplicationController;
use App\Http\Controllers\CurrentApplicationController;
use App\Http\Controllers\CurrentApplicationQrCodeController;
use App\Http\Controllers\DeleteAllApplicationsController;
use App\Http\Controllers\DeleteApplicationByIdController;
use App\Http\Controllers\FinishApplicationController;
use App\Http\Controllers\ListApplicationsController;
use App\Http\Controllers\QrScannedApplicationsController;
use Illuminate\Support\Facades\Route;
Route::post('/applications', CreateApplicationController::class);
......@@ -13,4 +14,5 @@ Route::post('/applications/finish', FinishApplicationController::class);
Route::get('/applications/current', CurrentApplicationController::class);
Route::get('/applications', ListApplicationsController::class);
Route::get('/applications/qr_code', CurrentApplicationQrCodeController::class);
Route::get('/applications/{qr_token}', QrScannedApplicationsController::class);
Route::delete('/applications', DeleteAllApplicationsController::class);
Route::delete('/applications/{application}', DeleteApplicationByIdController::class);