Merge pull request #464 from shikorism/fix/transact-checkin

EjaculationControllerでトランザクションを使う
This commit is contained in:
shibafu 2020-08-10 11:48:23 +09:00 committed by GitHub
commit 0ca16459a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,7 @@ use App\User;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Validator; use Validator;
class EjaculationController extends Controller class EjaculationController extends Controller
@ -52,6 +53,7 @@ class EjaculationController extends Controller
return redirect()->route('checkin')->withErrors($validator)->withInput(); return redirect()->route('checkin')->withErrors($validator)->withInput();
} }
$ejaculation = DB::transaction(function () use ($request, $inputs) {
$ejaculation = Ejaculation::create([ $ejaculation = Ejaculation::create([
'user_id' => Auth::id(), 'user_id' => Auth::id(),
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']), 'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
@ -76,6 +78,9 @@ class EjaculationController extends Controller
} }
$ejaculation->tags()->sync($tagIds); $ejaculation->tags()->sync($tagIds);
return $ejaculation;
});
if (!empty($ejaculation->link)) { if (!empty($ejaculation->link)) {
event(new LinkDiscovered($ejaculation->link)); event(new LinkDiscovered($ejaculation->link));
} }
@ -144,6 +149,7 @@ class EjaculationController extends Controller
return redirect()->route('checkin.edit', ['id' => $id])->withErrors($validator)->withInput(); return redirect()->route('checkin.edit', ['id' => $id])->withErrors($validator)->withInput();
} }
DB::transaction(function () use ($ejaculation, $request, $inputs) {
$ejaculation->fill([ $ejaculation->fill([
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']), 'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
'note' => $inputs['note'] ?? '', 'note' => $inputs['note'] ?? '',
@ -165,6 +171,7 @@ class EjaculationController extends Controller
} }
} }
$ejaculation->tags()->sync($tagIds); $ejaculation->tags()->sync($tagIds);
});
if (!empty($ejaculation->link)) { if (!empty($ejaculation->link)) {
event(new LinkDiscovered($ejaculation->link)); event(new LinkDiscovered($ejaculation->link));
@ -180,8 +187,11 @@ class EjaculationController extends Controller
$this->authorize('edit', $ejaculation); $this->authorize('edit', $ejaculation);
$user = User::findOrFail($ejaculation->user_id); $user = User::findOrFail($ejaculation->user_id);
DB::transaction(function () use ($ejaculation) {
$ejaculation->tags()->detach(); $ejaculation->tags()->detach();
$ejaculation->delete(); $ejaculation->delete();
});
return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。'); return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。');
} }