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

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

@ -13,4 +13,8 @@ class Ejaculation extends Model
'note', 'geo_latitude', 'geo_longitude', 'note', 'geo_latitude', 'geo_longitude',
'is_private' 'is_private'
]; ];
protected $dates = [
'ejaculated_date'
];
} }

View File

@ -28,50 +28,6 @@ class HomeController extends Controller
public function index() public function index()
{ {
if (Auth::check()) { 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() $informations = Information::query()
->select('id', 'category', 'pinned', 'title', 'created_at') ->select('id', 'category', 'pinned', 'title', 'created_at')
->orderBy('pinned') ->orderBy('pinned')
@ -79,7 +35,7 @@ SQL
->paginate(3); ->paginate(3);
$categories = Information::CATEGORIES; $categories = Information::CATEGORIES;
return view('home')->with(compact('ejaculations', 'currentSession', 'summary', 'informations', 'categories')); return view('home')->with(compact('informations', 'categories'));
} else { } else {
return view('guest'); return view('guest');
} }

View File

@ -23,7 +23,7 @@ class UserController extends Controller
// チェックインの取得 // チェックインの取得
$query = Ejaculation::select(DB::raw(<<<'SQL' $query = Ejaculation::select(DB::raw(<<<'SQL'
id, id,
to_char(ejaculated_date, 'YYYY/MM/DD HH24:MI') AS ejaculated_date, ejaculated_date,
note, note,
is_private, 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(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') $ejaculations = $query->orderBy('ejaculated_date', 'desc')
->paginate(20); ->paginate(20);
// 現在のオナ禁セッションの経過時間 return view('user.profile')->with(compact('user', 'ejaculations'));
if (count($ejaculations) > 0) { }
$currentSession = Carbon::parse($ejaculations[0]['ejaculated_date'])
->diff(Carbon::now()) public function stats($name)
->format('%a日 %h時間 %i分'); {
} else { $user = User::where('name', $name)->first();
$currentSession = null; if (empty($user)) {
abort(404);
} }
// 概況欄のデータ取得 return view('user.stats')->with(compact('user'));
$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.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'));
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Providers;
use App\Http\ViewComposers\ProfileComposer;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
View::composer('components.profile', ProfileComposer::class);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}

View File

@ -176,6 +176,7 @@ return [
// App\Providers\BroadcastServiceProvider::class, // App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
App\Providers\ViewComposerServiceProvider::class,
], ],

View File

@ -0,0 +1,23 @@
<div class="card mb-4">
<div class="card-body">
<img src="{{ $user->getProfileImageUrl(64) }}" class="rounded mb-1">
<h4 class="card-title"><a class="text-dark" href="{{ route('user.profile', ['name' => $user->name]) }}">{{ $user->display_name }}</a></h4>
<h6 class="card-subtitle mb-4"><a class="text-muted" href="{{ route('user.profile', ['name' => $user->name]) }}">&commat;{{ $user->name }}</a></h6>
<h6 class="font-weight-bold"><span class="oi oi-timer"></span> 現在のセッション</h6>
@if (isset($currentSession))
<p class="card-text mb-0">{{ $currentSession }}経過</p>
<p class="card-text">({{ $latestEjaculation->ejaculated_date->format('Y/m/d H:i') }} にリセット)</p>
@else
<p class="card-text mb-0">計測がまだ始まっていません</p>
<p class="card-text">(一度チェックインすると始まります)</p>
@endif
<h6 class="font-weight-bold"><span class="oi oi-graph"></span> 概況</h6>
<p class="card-text mb-0">平均記録: {{ Formatter::formatInterval($summary[0]->average) }}</p>
<p class="card-text mb-0">最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}</p>
<p class="card-text mb-0">最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}</p>
<p class="card-text mb-0">合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}</p>
<p class="card-text">通算回数: {{ $summary[0]->total_checkins }}</p>
</div>
</div>

View File

@ -7,29 +7,8 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-lg-4"> <div class="col-lg-4">
<div class="card mb-4"> @component('components.profile', ['user' => Auth::user()])
<div class="card-body"> @endcomponent
<img src="{{ Auth::user()->getProfileImageUrl(64) }}" class="rounded mb-1">
<h4 class="card-title"><a class="text-dark" href="{{ route('user.profile') }}">{{ Auth::user()->display_name }}</a></h4>
<h6 class="card-subtitle mb-4"><a class="text-muted" href="{{ route('user.profile') }}">&commat;{{ Auth::user()->name }}</a></h6>
<h6 class="font-weight-bold"><span class="oi oi-timer"></span> 現在のセッション</h6>
@if (isset($currentSession))
<p class="card-text mb-0">{{ $currentSession }}経過</p>
<p class="card-text">({{ $ejaculations[0]['ejaculated_date'] }} にリセット)</p>
@else
<p class="card-text mb-0">計測がまだ始まっていません</p>
<p class="card-text">(一度チェックインすると始まります)</p>
@endif
<h6 class="font-weight-bold"><span class="oi oi-graph"></span> 概況</h6>
<p class="card-text mb-0">平均記録: {{ Formatter::formatInterval($summary[0]->average) }}</p>
<p class="card-text mb-0">最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}</p>
<p class="card-text mb-0">最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}</p>
<p class="card-text mb-0">合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}</p>
<p class="card-text">通算回数: {{ $summary[0]->total_checkins }}</p>
</div>
</div>
</div> </div>
<div class="col-lg-8"> <div class="col-lg-8">
<div class="card mb-4"> <div class="card mb-4">

View File

@ -4,29 +4,8 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-lg-4"> <div class="col-lg-4">
<div class="card mb-4"> @component('components.profile', ['user' => $user])
<div class="card-body"> @endcomponent
<img src="{{ $user->getProfileImageUrl(64) }}" class="rounded mb-1">
<h4 class="card-title"><a class="text-dark" href="{{ route('user.profile', ['name' => $user->name]) }}">{{ $user->display_name }}</a></h4>
<h6 class="card-subtitle mb-4"><a class="text-muted" href="{{ route('user.profile', ['name' => $user->name]) }}">&commat;{{ $user->name }}</a></h6>
<h6 class="font-weight-bold"><span class="oi oi-timer"></span> 現在のセッション</h6>
@if (isset($currentSession))
<p class="card-text mb-0">{{ $currentSession }}経過</p>
<p class="card-text">({{ $ejaculations[0]['ejaculated_date'] }} にリセット)</p>
@else
<p class="card-text mb-0">計測がまだ始まっていません</p>
<p class="card-text">(一度チェックインすると始まります)</p>
@endif
<h6 class="font-weight-bold"><span class="oi oi-graph"></span> 概況</h6>
<p class="card-text mb-0">平均記録: {{ Formatter::formatInterval($summary[0]->average) }}</p>
<p class="card-text mb-0">最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}</p>
<p class="card-text mb-0">最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}</p>
<p class="card-text mb-0">合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}</p>
<p class="card-text">通算回数: {{ $summary[0]->total_checkins }}</p>
</div>
</div>
</div> </div>
<div class="col-lg-8"> <div class="col-lg-8">
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">

View File

@ -0,0 +1,4 @@
@extends('user.base')
@section('tab-content')
@endsection

View File

@ -6,18 +6,18 @@
<li class="list-group-item border-bottom-only pt-3 pb-3"> <li class="list-group-item border-bottom-only pt-3 pb-3">
<!-- span --> <!-- span -->
<div class="d-flex justify-content-between"> <div class="d-flex justify-content-between">
<h5>{{ $ejaculation['ejaculated_span'] ?? '精通' }} <small class="text-muted">{{ $ejaculation['before_date'] }}{{ !empty($ejaculation['before_date']) ? ' ' : '' }}{{ $ejaculation['ejaculated_date'] }}</small></h5> <h5>{{ $ejaculation->ejaculated_span ?? '精通' }} <small class="text-muted">{{ $ejaculation->before_date }}{{ !empty($ejaculation->before_date) ? ' ' : '' }}{{ $ejaculation->ejaculated_date->format('Y/m/d H:i') }}</small></h5>
@if ($user->id === Auth::user()->id) @if ($user->id === Auth::user()->id)
<div> <div>
<a class="text-secondary timeline-action-item" href="#"><span class="oi oi-pencil" data-toggle="tooltip" data-placement="bottom" title="修正"></span></a> <a class="text-secondary timeline-action-item" href="#"><span class="oi oi-pencil" data-toggle="tooltip" data-placement="bottom" title="修正"></span></a>
<a class="text-secondary timeline-action-item" href="#" data-toggle="modal" data-target="#deleteCheckinModal" data-id="{{ $ejaculation['id'] }}" data-date="{{ $ejaculation['ejaculated_date'] }}"><span class="oi oi-trash" data-toggle="tooltip" data-placement="bottom" title="削除"></span></a> <a class="text-secondary timeline-action-item" href="#" data-toggle="modal" data-target="#deleteCheckinModal" data-id="{{ $ejaculation->id }}" data-date="{{ $ejaculation->ejaculated_date }}"><span class="oi oi-trash" data-toggle="tooltip" data-placement="bottom" title="削除"></span></a>
</div> </div>
@endif @endif
</div> </div>
<!-- tags --> <!-- tags -->
@if ($ejaculation['is_private']) {{-- TODO: タグを付けたら、タグが空じゃないかも判定に加える --}} @if ($ejaculation->is_private) {{-- TODO: タグを付けたら、タグが空じゃないかも判定に加える --}}
<p class="mb-2"> <p class="mb-2">
@if ($ejaculation['is_private']) @if ($ejaculation->is_private)
<span class="badge badge-warning"><span class="oi oi-lock-locked"></span> 非公開</span> <span class="badge badge-warning"><span class="oi oi-lock-locked"></span> 非公開</span>
@endif @endif
{{-- {{--
@ -39,9 +39,9 @@
</div> </div>
--}} --}}
<!-- note --> <!-- note -->
@if (!empty($ejaculation['note'])) @if (!empty($ejaculation->note))
<p class="mb-0"> <p class="mb-0">
{!! Formatter::linkify(nl2br(e($ejaculation['note']))) !!} {!! Formatter::linkify(nl2br(e($ejaculation->note))) !!}
</p> </p>
@endif @endif
</li> </li>

View File

@ -0,0 +1,4 @@
@extends('user.base')
@section('tab-content')
@endsection