From ea45cb5752a6b9d0881d95eadeeab6687e3252e9 Mon Sep 17 00:00:00 2001 From: devoalda Date: Sun, 6 Aug 2023 08:26:14 +0800 Subject: [PATCH] fix(Time Fix): Updated: - Database columns to unix time - Create and Update TODO to use unix time - View to display with unix time --- app/Http/Controllers/TodoController.php | 65 ++++++++++++------- app/Models/Todo.php | 11 ++++ ...23_08_05_234455_timestamps_to_unix_int.php | 36 ++++++++++ .../2023_08_05_235025_time_unix_int.php | 38 +++++++++++ resources/views/todo/edit.blade.php | 4 +- resources/views/todo/index.blade.php | 10 +-- routes/web.php | 9 ++- 7 files changed, 140 insertions(+), 33 deletions(-) create mode 100644 database/migrations/2023_08_05_234455_timestamps_to_unix_int.php create mode 100644 database/migrations/2023_08_05_235025_time_unix_int.php diff --git a/app/Http/Controllers/TodoController.php b/app/Http/Controllers/TodoController.php index 7523b98..44f4f34 100644 --- a/app/Http/Controllers/TodoController.php +++ b/app/Http/Controllers/TodoController.php @@ -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'); } diff --git a/app/Models/Todo.php b/app/Models/Todo.php index 2931159..b82ea9f 100644 --- a/app/Models/Todo.php +++ b/app/Models/Todo.php @@ -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", + ]; } diff --git a/database/migrations/2023_08_05_234455_timestamps_to_unix_int.php b/database/migrations/2023_08_05_234455_timestamps_to_unix_int.php new file mode 100644 index 0000000..db46ec9 --- /dev/null +++ b/database/migrations/2023_08_05_234455_timestamps_to_unix_int.php @@ -0,0 +1,36 @@ +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(); + }); + } +}; diff --git a/database/migrations/2023_08_05_235025_time_unix_int.php b/database/migrations/2023_08_05_235025_time_unix_int.php new file mode 100644 index 0000000..301cc85 --- /dev/null +++ b/database/migrations/2023_08_05_235025_time_unix_int.php @@ -0,0 +1,38 @@ +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'); + }); + } +}; diff --git a/resources/views/todo/edit.blade.php b/resources/views/todo/edit.blade.php index 39ac358..abb5645 100644 --- a/resources/views/todo/edit.blade.php +++ b/resources/views/todo/edit.blade.php @@ -41,7 +41,7 @@ + value="{{ old('due_start', date('Y-m-d\TH:i', $todo->due_start)) }}"> @error('due_start')
{{ $message }} @@ -52,7 +52,7 @@ + value="{{ old('due_end', date('Y-m-d\TH:i', $todo->due_end)) }}"> @error('due_end')
{{ $message }} diff --git a/resources/views/todo/index.blade.php b/resources/views/todo/index.blade.php index 422991d..e7ee829 100644 --- a/resources/views/todo/index.blade.php +++ b/resources/views/todo/index.blade.php @@ -76,18 +76,18 @@ @if ($due_start || $due_end) @if ($due_start && $due_end) @if ($due_start == $due_end) -

Due: {{ $due_start->format('Y-m-d H:i:s') +

Due: {{ $due_start->format('Y-m-d H:i') }}

@else -

Due: {{ $due_start->format('Y-m-d H:i:s') }} - - {{ $due_end->format('Y-m-d H:i:s') }}

+

Due: {{ $due_start->format('Y-m-d H:i') }} - + {{ $due_end->format('Y-m-d H:i') }}

@endif @else @if ($due_start) -

Due: {{ $due_start->format('Y-m-d H:i:s') +

Due: {{ $due_start->format('Y-m-d H:i') }}

@elseif ($due_end) -

Due: {{ $due_end->format('Y-m-d H:i:s') }}

+

Due: {{ $due_end->format('Y-m-d H:i') }}

@endif @endif diff --git a/routes/web.php b/routes/web.php index 38c5514..8f721b3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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 () {