プロフィールの左カラムにある情報に関する処理をリファクタリング

This commit is contained in:
shibafu
2017-11-05 01:26:52 +09:00
parent 53f34c12cc
commit 6ed0938694
12 changed files with 157 additions and 127 deletions

View File

@@ -28,50 +28,6 @@ class HomeController extends Controller
public function index()
{
if (Auth::check()) {
$ejaculations = Ejaculation::select(DB::raw(<<<'SQL'
to_char(ejaculated_date, 'YYYY/MM/DD HH24:MI') AS ejaculated_date,
note,
is_private,
to_char(lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC), 'YYYY/MM/DD HH24:MI') AS before_date,
to_char(ejaculated_date - (lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)), 'FMDDD日 FMHH24時間 FMMI分') AS ejaculated_span
SQL
))
->where(['user_id' => Auth::id()])
->orderBy('ejaculated_date', 'desc')
->limit(9)
->get();
// 現在のオナ禁セッションの経過時間
if (count($ejaculations) > 0) {
$currentSession = Carbon::parse($ejaculations[0]['ejaculated_date'])
->diff(Carbon::now())
->format('%a日 %h時間 %i分');
} else {
$currentSession = null;
}
// 概況欄のデータ取得
$summary = DB::select(<<<'SQL'
SELECT
avg(span) AS average,
max(span) AS longest,
min(span) AS shortest,
sum(span) AS total_times,
count(*) AS total_checkins
FROM
(
SELECT
extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span
FROM
ejaculations
WHERE
user_id = :user_id
ORDER BY
ejaculated_date DESC
) AS temp
SQL
, ['user_id' => Auth::id()]);
$informations = Information::query()
->select('id', 'category', 'pinned', 'title', 'created_at')
->orderBy('pinned')
@@ -79,7 +35,7 @@ SQL
->paginate(3);
$categories = Information::CATEGORIES;
return view('home')->with(compact('ejaculations', 'currentSession', 'summary', 'informations', 'categories'));
return view('home')->with(compact('informations', 'categories'));
} else {
return view('guest');
}

View File

@@ -23,7 +23,7 @@ class UserController extends Controller
// チェックインの取得
$query = Ejaculation::select(DB::raw(<<<'SQL'
id,
to_char(ejaculated_date, 'YYYY/MM/DD HH24:MI') AS ejaculated_date,
ejaculated_date,
note,
is_private,
to_char(lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC), 'YYYY/MM/DD HH24:MI') AS before_date,
@@ -37,37 +37,26 @@ SQL
$ejaculations = $query->orderBy('ejaculated_date', 'desc')
->paginate(20);
// 現在のオナ禁セッションの経過時間
if (count($ejaculations) > 0) {
$currentSession = Carbon::parse($ejaculations[0]['ejaculated_date'])
->diff(Carbon::now())
->format('%a日 %h時間 %i分');
} else {
$currentSession = null;
return view('user.profile')->with(compact('user', 'ejaculations'));
}
public function stats($name)
{
$user = User::where('name', $name)->first();
if (empty($user)) {
abort(404);
}
// 概況欄のデータ取得
$summary = DB::select(<<<'SQL'
SELECT
avg(span) AS average,
max(span) AS longest,
min(span) AS shortest,
sum(span) AS total_times,
count(*) AS total_checkins
FROM
(
SELECT
extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span
FROM
ejaculations
WHERE
user_id = :user_id
ORDER BY
ejaculated_date DESC
) AS temp
SQL
, ['user_id' => $user->id]);
return view('user.stats')->with(compact('user'));
}
return view('user.profile')->with(compact('user', 'ejaculations', 'currentSession', 'summary'));
public function okazu($name)
{
$user = User::where('name', $name)->first();
if (empty($user)) {
abort(404);
}
return view('user.okazu')->with(compact('user'));
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Http\ViewComposers;
use App\Ejaculation;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
class ProfileComposer
{
public function __construct()
{
}
public function compose(View $view)
{
// user変数に値が設定されてない場合は落とす
if (!$view->offsetExists('user')) {
throw new \LogicException('View data "user" was not exist.');
}
$user = $view->offsetGet('user');
// 現在のオナ禁セッションの経過時間
$latestEjaculation = Ejaculation::select('ejaculated_date')
->where('user_id', $user->id)
->orderByDesc('ejaculated_date')
->first();
if (!empty($latestEjaculation)) {
$currentSession = $latestEjaculation->ejaculated_date
->diff(Carbon::now())
->format('%a日 %h時間 %i分');
} else {
$currentSession = null;
}
// 概況欄のデータ取得
$summary = DB::select(<<<'SQL'
SELECT
avg(span) AS average,
max(span) AS longest,
min(span) AS shortest,
sum(span) AS total_times,
count(*) AS total_checkins
FROM
(
SELECT
extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span
FROM
ejaculations
WHERE
user_id = :user_id
ORDER BY
ejaculated_date DESC
) AS temp
SQL
, ['user_id' => $user->id]);
$view->with(compact('latestEjaculation', 'currentSession', 'summary'));
}
}