tissue/app/Http/Controllers/EjaculationController.php

121 lines
4.6 KiB
PHP
Raw Normal View History

2017-08-27 04:44:53 +09:00
<?php
namespace App\Http\Controllers;
use App\User;
use Carbon\Carbon;
2017-08-27 04:44:53 +09:00
use Validator;
use App\Ejaculation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class EjaculationController extends Controller
{
public function create()
{
return view('ejaculation.checkin');
}
public function store(Request $request)
{
$inputs = $request->all();
if ($request->has('note')) {
$inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']);
}
Validator::make($inputs, [
2017-08-27 04:44:53 +09:00
'date' => 'required|date_format:Y/m/d',
'time' => 'required|date_format:H:i',
'note' => 'nullable|string|max:500',
2018-01-05 00:26:48 +09:00
'link' => 'nullable|url',
])->after(function ($validator) use ($request, $inputs) {
2017-08-27 04:44:53 +09:00
// 日時の重複チェック
if (!$validator->errors()->hasAny(['date', 'time'])) {
$dt = $inputs['date'] . ' ' . $inputs['time'];
if (Ejaculation::where(['user_id' => Auth::id(), 'ejaculated_date' => $dt])->count()) {
$validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。');
}
2017-08-27 04:44:53 +09:00
}
})->validate();
$ejaculation = Ejaculation::create([
2017-08-27 04:44:53 +09:00
'user_id' => Auth::id(),
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
'note' => $inputs['note'] ?? '',
2018-01-05 00:26:48 +09:00
'link' => $inputs['link'] ?? '',
2017-08-27 04:44:53 +09:00
'is_private' => $request->has('is_private') ?? false
]);
return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインしました!');
2017-08-27 04:44:53 +09:00
}
public function show($id)
2017-08-27 04:44:53 +09:00
{
$ejaculation = Ejaculation::findOrFail($id);
$user = User::findOrFail($ejaculation->user_id);
// 1つ前のチェックインからの経過時間を求める
$previousEjaculation = Ejaculation::select('ejaculated_date')
->where('user_id', $ejaculation->user_id)
->where('ejaculated_date', '<', $ejaculation->ejaculated_date)
->orderByDesc('ejaculated_date')
->first();
if (!empty($previousEjaculation)) {
$ejaculatedSpan = $ejaculation->ejaculated_date
->diff($previousEjaculation->ejaculated_date)
->format('%a日 %h時間 %i分');
} else {
$ejaculatedSpan = null;
}
return view('ejaculation.show')->with(compact('user', 'ejaculation', 'ejaculatedSpan'));
2017-08-27 04:44:53 +09:00
}
public function edit($id)
2017-08-27 04:44:53 +09:00
{
$ejaculation = Ejaculation::findOrFail($id);
return view('ejaculation.edit')->with(compact('ejaculation'));
2017-08-27 04:44:53 +09:00
}
public function update(Request $request, $id)
2017-08-27 04:44:53 +09:00
{
$ejaculation = Ejaculation::findOrFail($id);
$inputs = $request->all();
if ($request->has('note')) {
$inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']);
}
Validator::make($inputs, [
'date' => 'required|date_format:Y/m/d',
'time' => 'required|date_format:H:i',
'note' => 'nullable|string|max:500',
2018-01-05 00:26:48 +09:00
'link' => 'nullable|url',
])->after(function ($validator) use ($id, $request, $inputs) {
// 日時の重複チェック
if (!$validator->errors()->hasAny(['date', 'time'])) {
$dt = $inputs['date'] . ' ' . $inputs['time'];
if (Ejaculation::where(['user_id' => Auth::id(), 'ejaculated_date' => $dt])->where('id', '<>', $id)->count()) {
$validator->errors()->add('datetime', '既にこの日時にチェックインしているため、登録できません。');
}
}
})->validate();
$ejaculation->fill([
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
'note' => $inputs['note'] ?? '',
2018-01-05 00:26:48 +09:00
'link' => $inputs['link'] ?? '',
'is_private' => $request->has('is_private') ?? false
])->save();
return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインを修正しました!');
2017-08-27 04:44:53 +09:00
}
public function destroy($id)
2017-08-27 04:44:53 +09:00
{
$ejaculation = Ejaculation::findOrFail($id);
$user = User::findOrFail($ejaculation->user_id);
$ejaculation->delete();
2017-11-03 18:10:50 +09:00
return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。');
2017-08-27 04:44:53 +09:00
}
}