fix(Time Fix):

Updated:
- Database columns to unix time
- Create and Update TODO to use unix time
- View to display with unix time
This commit is contained in:
devoalda 2023-08-06 08:26:14 +08:00
parent fccb731b12
commit ea45cb5752
7 changed files with 140 additions and 33 deletions

View File

@ -9,10 +9,13 @@ use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Carbon;
class TodoController extends Controller class TodoController extends Controller
{ {
private string $timezone = "Asia/Singapore"; private string $timezone = "Asia/Singapore";
/** /**
* Display a listing of the resource. * Display a listing of the resource.
*/ */
@ -24,7 +27,10 @@ class TodoController extends Controller
->whereNull('completed_at') ->whereNull('completed_at')
->orWhere('completed_at', '') ->orWhere('completed_at', '')
// And all completed today (Range of today) // And all completed today (Range of today)
->orWhereBetween('completed_at', [now($this->timezone)->startOfDay(), now($this->timezone)->endOfDay()]) ->orWhereBetween('completed_at', [
strtotime(Carbon::today($this->timezone)),
strtotime(Carbon::tomorrow($this->timezone))
])
->orderByDesc('created_at') ->orderByDesc('created_at')
->get([ ->get([
'id', 'id',
@ -60,10 +66,16 @@ class TodoController extends Controller
]); ]);
$validatedData = array_merge($validatedData, [ $validatedData = array_merge($validatedData, [
'due_end' => $request->due_end ?? $request->due_start, 'due_end' => strtotime($validatedData['due_end'] ?? $validatedData['due_start'] ?? null) ?? null,
'user_id' => auth()->user()->id, 'user_id' => auth()->user()->id,
]); ]);
// Modify all dates to unix timestamp
if (isset($validatedData['due_start']))
$validatedData['due_start'] = strtotime($validatedData['due_start']);
if (isset($validatedData['due_end']))
$validatedData['due_end'] = strtotime($validatedData['due_end']);
Todo::create($validatedData); Todo::create($validatedData);
return redirect()->route('todo.index'); return redirect()->route('todo.index');
@ -93,36 +105,41 @@ class TodoController extends Controller
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* Can either update completed_at or All fields * Can either update completed_at (Checkbox) or other fields
*
* @param UpdateTodoRequest $request UpdateTodoRequest
* @param Todo $todo Todo Object
* @return RedirectResponse Redirect to todo.index
*/ */
public function update(UpdateTodoRequest $request, Todo $todo): RedirectResponse public function update(UpdateTodoRequest $request, Todo $todo): RedirectResponse
{ {
// Get Data $data = $request->only(['title', 'description', 'due_start', 'due_end', 'completed_at']);
$data = $request->post();
// Check if the request contains only token and method if (Request::filled('completed_at')) {
$emptyPost = count($data) === 2; $todo->completed_at = Request::input('completed_at') === 'on' ? strtotime(now($this->timezone)) : null;
if ($emptyPost) {
// If empty post, toggle completed_at
$todo->completed_at = $todo->completed_at ? null : now($this->timezone);
$todo->save();
} elseif ((isset($data['completed_at']) && $data['completed_at'] === 'on')) {
// If completed_at=on, set completed_at to now()
$todo->completed_at = now($this->timezone);
$todo->save(); $todo->save();
return redirect()->route('todo.index');
} else { } else {
// Validate Data // If 'completed_at' is not provided, toggle its value (only if the request is empty)
$data = $request->validate([ if (empty($data))
'title' => 'nullable|max:255', $todo->completed_at = $todo->completed_at ? null : strtotime(now($this->timezone));
'description' => 'nullable|max:255', else
'due_start' => 'nullable|date', // Continue to update other fields
'due_end' => 'nullable|after:due_start', unset($data['completed_at']);
]);
// Update todo
$todo->update($data);
} }
if (Request::filled('due_start')) {
$data['due_start'] = strtotime(Request::input('due_start'));
}
if (Request::filled('due_end')) {
$data['due_end'] = strtotime(Request::input('due_end'));
} elseif (isset($data['due_start'])) {
// If 'due_end' is not provided, set it to 'due_start' value
$data['due_end'] = $data['due_start'];
}
$todo->update($data);
return redirect()->route('todo.index'); return redirect()->route('todo.index');
} }

View File

@ -10,6 +10,8 @@ class Todo extends Model
{ {
use HasFactory, UuidTrait; use HasFactory, UuidTrait;
protected $dateFormat = 'U';
protected $fillable = [ protected $fillable = [
'title', 'title',
'description', 'description',
@ -18,4 +20,13 @@ class Todo extends Model
'user_id', 'user_id',
'completed_at', 'completed_at',
]; ];
protected $casts = [
// Unix timestamp for due_start, due_end, completed_at, created_at, updated_at
"due_start" => "integer",
"due_end" => "integer",
"completed_at" => "integer",
"created_at" => "integer",
"updated_at" => "integer",
];
} }

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('todos', function (Blueprint $table) {
$table->dropColumn('created_at');
$table->dropColumn('updated_at');
$table->dropColumn('completed_at');
$table->dropColumn('due_start');
$table->dropColumn('due_end');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('todos', function (Blueprint $table) {
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
$table->timestamp('completed_at')->nullable();
$table->timestamp('due_start')->nullable();
$table->timestamp('due_end')->nullable();
});
}
};

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('todos', function (Blueprint $table) {
// Create due_start, due_end, completed_at, created_at, updated_at as unsigned integer
$table->integer('due_start')->unsigned()->nullable();
$table->integer('due_end')->unsigned()->nullable()->after('due_start');
$table->integer('completed_at')->unsigned()->nullable();
$table->integer('created_at')->unsigned()->nullable()->default(strtotime('now'));
$table->integer('updated_at')->unsigned()->nullable()->default(strtotime('now'));
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('todos', function (Blueprint $table) {
// Drop due_start, due_end, completed_at, created_at, updated_at
$table->dropColumn('due_start');
$table->dropColumn('due_end');
$table->dropColumn('completed_at');
$table->dropColumn('created_at');
$table->dropColumn('updated_at');
});
}
};

View File

@ -41,7 +41,7 @@
<label for="due_start" class="block mb-2 font-semibold">Due Start</label> <label for="due_start" class="block mb-2 font-semibold">Due Start</label>
<input type="datetime-local" name="due_start" id="due_start" placeholder="Due Start" <input type="datetime-local" name="due_start" id="due_start" placeholder="Due Start"
class="bg-gray-100 dark:bg-gray-700 border border-gray-300 dark:border-gray-700 focus:ring-2 focus:ring-blue-500 rounded-lg w-full p-4 @error('due_start') border-red-500 @enderror" class="bg-gray-100 dark:bg-gray-700 border border-gray-300 dark:border-gray-700 focus:ring-2 focus:ring-blue-500 rounded-lg w-full p-4 @error('due_start') border-red-500 @enderror"
value="{{ old('due_start', $todo->due_start) }}"> value="{{ old('due_start', date('Y-m-d\TH:i', $todo->due_start)) }}">
@error('due_start') @error('due_start')
<div class="text-red-500 mt-2 text-sm"> <div class="text-red-500 mt-2 text-sm">
{{ $message }} {{ $message }}
@ -52,7 +52,7 @@
<label for="due_end" class="block mb-2 font-semibold">Due End</label> <label for="due_end" class="block mb-2 font-semibold">Due End</label>
<input type="datetime-local" name="due_end" id="due_end" placeholder="Due End" <input type="datetime-local" name="due_end" id="due_end" placeholder="Due End"
class="bg-gray-100 dark:bg-gray-700 border border-gray-300 dark:border-gray-700 focus:ring-2 focus:ring-blue-500 rounded-lg w-full p-4 @error('due_end') border-red-500 @enderror" class="bg-gray-100 dark:bg-gray-700 border border-gray-300 dark:border-gray-700 focus:ring-2 focus:ring-blue-500 rounded-lg w-full p-4 @error('due_end') border-red-500 @enderror"
value="{{ old('due_end', $todo->due_end) }}"> value="{{ old('due_end', date('Y-m-d\TH:i', $todo->due_end)) }}">
@error('due_end') @error('due_end')
<div class="text-red-900 mt-2 text-sm"> <div class="text-red-900 mt-2 text-sm">
{{ $message }} {{ $message }}

View File

@ -76,18 +76,18 @@
@if ($due_start || $due_end) @if ($due_start || $due_end)
@if ($due_start && $due_end) @if ($due_start && $due_end)
@if ($due_start == $due_end) @if ($due_start == $due_end)
<p class="text-sm text-gray-500 mb-2">Due: {{ $due_start->format('Y-m-d H:i:s') <p class="text-sm text-gray-500 mb-2">Due: {{ $due_start->format('Y-m-d H:i')
}}</p> }}</p>
@else @else
<p class="text-sm text-gray-500 mb-2">Due: {{ $due_start->format('Y-m-d H:i:s') }} - <p class="text-sm text-gray-500 mb-2">Due: {{ $due_start->format('Y-m-d H:i') }} -
{{ $due_end->format('Y-m-d H:i:s') }}</p> {{ $due_end->format('Y-m-d H:i') }}</p>
@endif @endif
@else @else
@if ($due_start) @if ($due_start)
<p class="text-sm text-gray-500 mb-2">Due: {{ $due_start->format('Y-m-d H:i:s') <p class="text-sm text-gray-500 mb-2">Due: {{ $due_start->format('Y-m-d H:i')
}}</p> }}</p>
@elseif ($due_end) @elseif ($due_end)
<p class="text-sm text-gray-500 mb-2">Due: {{ $due_end->format('Y-m-d H:i:s') }}</p> <p class="text-sm text-gray-500 mb-2">Due: {{ $due_end->format('Y-m-d H:i') }}</p>
@endif @endif
@endif @endif

View File

@ -21,7 +21,8 @@ Route::get('/', function () {
Route::get('/dashboard', function () { Route::get('/dashboard', function () {
return view('dashboard'); return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard'); })->middleware(['auth', 'verified'])
->name('dashboard');
// Show dashboard after login, get user data from TodoController // Show dashboard after login, get user data from TodoController
//Route::get('/dashboard', [TodoController::class, 'index']) //Route::get('/dashboard', [TodoController::class, 'index'])
@ -30,7 +31,11 @@ Route::get('/dashboard', function () {
// todo resource route // todo resource route
Route::resource('todo', TodoController::class) Route::resource('todo', TodoController::class)
->middleware(['auth', 'verified']); ->middleware([
'auth',
'verified',
'web'
]);
Route::middleware('auth')->group(function () { Route::middleware('auth')->group(function () {