Files
eatvio-infra/app/Http/Controllers/DiaryController.php
2023-12-28 17:07:09 +03:00

138 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Diary;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DiaryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
/* return view('diary.index'); */
$curDiary = $this->getCurrentDiary();
/* dd($curDiary); */
return redirect()->route('diary.edit', $curDiary);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request);
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function show(Diary $diary)
{
/* $curDiary = Diary::find($id); */
$daysCount = Diary::where('user_id', auth()->id())->count();
return view('diary.show', ['curDiary' => $diary, 'daysCount' => $daysCount]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$curDiary = Diary::find($id);
$daysCount = Diary::where('user_id', auth()->id())->count();
return view(
'diary.index',
[
'diaryId' => $id,
'curDiary' => $curDiary,
'daysCount' => $daysCount,
]
);
}
/**
* Update the specified resource in storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
/* dd($request, $id, $request->input()); */
$curDiary = Diary::find($id);
$curDiary->wakeup = $request->input('wakeup');
$curDiary->retire = $request->input('retire');
$curDiary->comment = $request->input('comment') ?? '';
$curDiary->closed = true;
$curDiary->save();
return redirect()->route('diary.show', $id);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
}
private function getCurrentDiary()
{
$curUserId = Auth::id();
$curDiary = Diary::userToDayItem($curUserId)->first();
if (! is_null($curDiary)) {
return $curDiary;
}
return $this->newDiary($curUserId);
}
private function newDiary($curUserId)
{
$curDiary = new Diary();
$curDiary->date = now()->setTimezone('3')->format('Y-m-d');
$curDiary->wakeup = '06:30';
$curDiary->retire = '22:30';
$curDiary->user_id = $curUserId;
$curDiary->closed = false;
$curDiary->comment = '';
$curDiary->save();
return $curDiary;
}
}