date/timeをchecked_in_atに統合

This commit is contained in:
shibafu 2020-07-21 00:29:48 +09:00
parent de07e950f2
commit e6252c49d1
2 changed files with 33 additions and 10 deletions

View File

@ -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)
]);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class EjaculationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $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,
];
}
}