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\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Carbon;
class TodoController extends Controller
{
private string $timezone = "Asia/Singapore";
/**
* Display a listing of the resource.
*/
@ -24,7 +27,10 @@ class TodoController extends Controller
->whereNull('completed_at')
->orWhere('completed_at', '')
// 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')
->get([
'id',
@ -60,10 +66,16 @@ class TodoController extends Controller
]);
$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,
]);
// 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);
return redirect()->route('todo.index');
@ -93,36 +105,41 @@ class TodoController extends Controller
/**
* 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
{
// Get Data
$data = $request->post();
$data = $request->only(['title', 'description', 'due_start', 'due_end', 'completed_at']);
// Check if the request contains only token and method
$emptyPost = count($data) === 2;
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);
if (Request::filled('completed_at')) {
$todo->completed_at = Request::input('completed_at') === 'on' ? strtotime(now($this->timezone)) : null;
$todo->save();
return redirect()->route('todo.index');
} else {
// Validate Data
$data = $request->validate([
'title' => 'nullable|max:255',
'description' => 'nullable|max:255',
'due_start' => 'nullable|date',
'due_end' => 'nullable|after:due_start',
]);
// Update todo
$todo->update($data);
// If 'completed_at' is not provided, toggle its value (only if the request is empty)
if (empty($data))
$todo->completed_at = $todo->completed_at ? null : strtotime(now($this->timezone));
else
// Continue to update other fields
unset($data['completed_at']);
}
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');
}

View File

@ -10,6 +10,8 @@ class Todo extends Model
{
use HasFactory, UuidTrait;
protected $dateFormat = 'U';
protected $fillable = [
'title',
'description',
@ -18,4 +20,13 @@ class Todo extends Model
'user_id',
'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>
<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"
value="{{ old('due_start', $todo->due_start) }}">
value="{{ old('due_start', date('Y-m-d\TH:i', $todo->due_start)) }}">
@error('due_start')
<div class="text-red-500 mt-2 text-sm">
{{ $message }}
@ -52,7 +52,7 @@
<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"
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')
<div class="text-red-900 mt-2 text-sm">
{{ $message }}

View File

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

View File

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