forked from eatvio/eatvio-infra
init commit
This commit is contained in:
133
app/Http/Controllers/DiaryController.php
Normal file
133
app/Http/Controllers/DiaryController.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Diary;
|
||||
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);
|
||||
}
|
||||
|
||||
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->save();
|
||||
|
||||
return $curDiary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @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 \Illuminate\Http\Request $request
|
||||
* @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->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)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user