From e6252c49d1e0635d8bbbaa7f3bcee478f26e4535 Mon Sep 17 00:00:00 2001 From: shibafu Date: Tue, 21 Jul 2020 00:29:48 +0900 Subject: [PATCH] =?UTF-8?q?date/time=E3=82=92checked=5Fin=5Fat=E3=81=AB?= =?UTF-8?q?=E7=B5=B1=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/WebhookController.php | 15 ++++------ app/Http/Resources/EjaculationResource.php | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 app/Http/Resources/EjaculationResource.php diff --git a/app/Http/Controllers/Api/WebhookController.php b/app/Http/Controllers/Api/WebhookController.php index 734e861..112068f 100644 --- a/app/Http/Controllers/Api/WebhookController.php +++ b/app/Http/Controllers/Api/WebhookController.php @@ -6,6 +6,7 @@ use App\CheckinWebhook; use App\Ejaculation; use App\Events\LinkDiscovered; use App\Http\Controllers\Controller; +use App\Http\Resources\EjaculationResource; use App\Tag; use Carbon\Carbon; use Illuminate\Http\Request; @@ -27,8 +28,7 @@ class WebhookController extends Controller $inputs = $request->all(); $validator = Validator::make($inputs, [ - 'date' => 'nullable|date_format:Y/m/d', - 'time' => 'nullable|date_format:H:i', + 'checked_in_at' => 'nullable|date|after_or_equal:2000-01-01 00:00:00|before_or_equal:2099-12-31 23:59:59', 'note' => 'nullable|string|max:500', 'link' => 'nullable|url|max:2000', 'tags' => 'nullable|array', @@ -49,13 +49,8 @@ class WebhookController extends Controller ]); } - $ejaculatedDate = now()->startOfMinute(); - if (!empty($inputs['date'])) { - $ejaculatedDate = $ejaculatedDate->setDateFrom(Carbon::createFromFormat('Y/m/d', $inputs['date'])); - } - if (!empty($inputs['time'])) { - $ejaculatedDate = $ejaculatedDate->setTimeFrom(Carbon::createFromFormat('H:i', $inputs['time'])); - } + $ejaculatedDate = empty($inputs['checked_in_at']) ? now() : new Carbon($inputs['checked_in_at']); + $ejaculatedDate = $ejaculatedDate->setTimezone(date_default_timezone_get())->startOfMinute(); if (Ejaculation::where(['user_id' => $webhook->user_id, 'ejaculated_date' => $ejaculatedDate])->count()) { return response()->json([ 'status' => 422, @@ -95,7 +90,7 @@ class WebhookController extends Controller return response()->json([ 'status' => 200, - 'checkin' => $ejaculation + 'checkin' => new EjaculationResource($ejaculation) ]); } } diff --git a/app/Http/Resources/EjaculationResource.php b/app/Http/Resources/EjaculationResource.php new file mode 100644 index 0000000..f66ff7d --- /dev/null +++ b/app/Http/Resources/EjaculationResource.php @@ -0,0 +1,28 @@ + $this->id, + 'checked_in_at' => $this->ejaculated_date->format(\DateTime::ATOM), + 'note' => $this->note, + 'link' => $this->link, + 'tags' => $this->tags->pluck('name'), + 'source' => $this->source, + 'is_private' => $this->is_private, + 'is_too_sensitive' => $this->is_too_sensitive, + ]; + } +}