tissue/app/Http/Controllers/UserController.php

100 lines
3.1 KiB
PHP
Raw Normal View History

2017-08-27 04:44:53 +09:00
<?php
namespace App\Http\Controllers;
use App\Ejaculation;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
//
public function profile($name)
{
$user = User::where('name', $name)->first();
if (empty($user)) {
abort(404);
}
// チェックインの取得
$query = Ejaculation::select(DB::raw(<<<'SQL'
id,
ejaculated_date,
2017-08-27 04:44:53 +09:00
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', $user->id);
if (!Auth::check() || $user->id !== Auth::id()) {
$query = $query->where('is_private', false);
}
$ejaculations = $query->orderBy('ejaculated_date', 'desc')
->paginate(20);
return view('user.profile')->with(compact('user', 'ejaculations'));
}
public function stats($name)
{
$user = User::where('name', $name)->first();
if (empty($user)) {
abort(404);
2017-08-27 04:44:53 +09:00
}
$groupByDay = Ejaculation::select(DB::raw(<<<'SQL'
to_char(ejaculated_date, 'YYYY/MM/DD') AS "date",
count(*) AS "count"
SQL
))
->where('user_id', $user->id)
->where('ejaculated_date', '>=', Carbon::now()->addMonths(-9)->firstOfMonth())
->groupBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM/DD')"))
->orderBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM/DD')"))
->get();
$calendarData = [];
foreach ($groupByDay as $data) {
$timestamp = Carbon::createFromFormat('Y/m/d', $data->date)->getTimestamp();
$calendarData[$timestamp] = $data->count;
}
$groupByMonth = Ejaculation::select(DB::raw(<<<'SQL'
to_char(ejaculated_date, 'YYYY/MM') AS "date",
count(*) AS "count"
SQL
))
->where('user_id', $user->id)
->where('ejaculated_date', '>=', Carbon::now()->addMonths(-11)->firstOfMonth())
->groupBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM')"))
->orderBy(DB::raw("to_char(ejaculated_date, 'YYYY/MM')"))
->get();
$monthlyCounts = [];
$month = (new Carbon())->subMonth(11);
while ($month->format('Y/m') <= date('Y/m')) {
if ($groupByMonth->first()['date'] === $month->format('Y/m')) {
$monthlyCounts[] = $groupByMonth->shift()['count'];
} else {
$monthlyCounts[] = 0;
}
$month = $month->addMonth(1);
}
return view('user.stats')->with(compact('user', 'calendarData', 'monthlyCounts'));
}
public function okazu($name)
{
$user = User::where('name', $name)->first();
if (empty($user)) {
abort(404);
}
2017-08-27 04:44:53 +09:00
return view('user.okazu')->with(compact('user'));
2017-08-27 04:44:53 +09:00
}
}