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,29 +53,33 @@ class EjaculationController extends Controller
return redirect()->route('checkin')->withErrors($validator)->withInput(); return redirect()->route('checkin')->withErrors($validator)->withInput();
} }
$ejaculation = Ejaculation::create([ $ejaculation = DB::transaction(function () use ($request, $inputs) {
'user_id' => Auth::id(), $ejaculation = Ejaculation::create([
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']), 'user_id' => Auth::id(),
'note' => $inputs['note'] ?? '', 'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
'link' => $inputs['link'] ?? '', 'note' => $inputs['note'] ?? '',
'source' => Ejaculation::SOURCE_WEB, 'link' => $inputs['link'] ?? '',
'is_private' => $request->has('is_private') ?? false, 'source' => Ejaculation::SOURCE_WEB,
'is_too_sensitive' => $request->has('is_too_sensitive') ?? false 'is_private' => $request->has('is_private') ?? false,
]); 'is_too_sensitive' => $request->has('is_too_sensitive') ?? false
]);
$tagIds = []; $tagIds = [];
if (!empty($inputs['tags'])) { if (!empty($inputs['tags'])) {
$tags = explode(' ', $inputs['tags']); $tags = explode(' ', $inputs['tags']);
foreach ($tags as $tag) { foreach ($tags as $tag) {
if ($tag === '') { if ($tag === '') {
continue; continue;
}
$tag = Tag::firstOrCreate(['name' => $tag]);
$tagIds[] = $tag->id;
} }
$tag = Tag::firstOrCreate(['name' => $tag]);
$tagIds[] = $tag->id;
} }
} $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,27 +149,29 @@ class EjaculationController extends Controller
return redirect()->route('checkin.edit', ['id' => $id])->withErrors($validator)->withInput(); return redirect()->route('checkin.edit', ['id' => $id])->withErrors($validator)->withInput();
} }
$ejaculation->fill([ DB::transaction(function () use ($ejaculation, $request, $inputs) {
'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']), $ejaculation->fill([
'note' => $inputs['note'] ?? '', 'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']),
'link' => $inputs['link'] ?? '', 'note' => $inputs['note'] ?? '',
'is_private' => $request->has('is_private') ?? false, 'link' => $inputs['link'] ?? '',
'is_too_sensitive' => $request->has('is_too_sensitive') ?? false 'is_private' => $request->has('is_private') ?? false,
])->save(); 'is_too_sensitive' => $request->has('is_too_sensitive') ?? false
])->save();
$tagIds = []; $tagIds = [];
if (!empty($inputs['tags'])) { if (!empty($inputs['tags'])) {
$tags = explode(' ', $inputs['tags']); $tags = explode(' ', $inputs['tags']);
foreach ($tags as $tag) { foreach ($tags as $tag) {
if ($tag === '') { if ($tag === '') {
continue; continue;
}
$tag = Tag::firstOrCreate(['name' => $tag]);
$tagIds[] = $tag->id;
} }
$tag = Tag::firstOrCreate(['name' => $tag]);
$tagIds[] = $tag->id;
} }
} $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);
$ejaculation->tags()->detach();
$ejaculation->delete(); DB::transaction(function () use ($ejaculation) {
$ejaculation->tags()->detach();
$ejaculation->delete();
});
return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。'); return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。');
} }