refactor: Address PR review feedback for timestamp handling

- Rename format-timestamp-for-postgres to format-timestamp-iso8601 (database-agnostic)
- Reuse format-timestamp-iso8601 in authenticate-user instead of inline formatting
- Change listened_at field type from integer to timestamp for consistency
This commit is contained in:
Glenn Thompson 2025-12-29 12:16:12 +03:00 committed by Brian O'Reilly
parent debf47c9c7
commit de70fbde5a
2 changed files with 6 additions and 10 deletions

View File

@ -61,7 +61,7 @@
(db:create "user_listening_history" '((user_id :integer) (db:create "user_listening_history" '((user_id :integer)
(track_title :text) (track_title :text)
(track_artist :text) (track_artist :text)
(listened_at :integer) (listened_at :timestamp)
(duration_seconds :integer)))) (duration_seconds :integer))))
(unless (db:collection-exists-p "user_playlists") (unless (db:collection-exists-p "user_playlists")
@ -98,8 +98,8 @@
(string= (string-upcase (package-name (db:implementation))) (string= (string-upcase (package-name (db:implementation)))
"I-LAMBDALITE")) "I-LAMBDALITE"))
(defun format-timestamp-for-postgres (value) (defun format-timestamp-iso8601 (value)
"Convert a timestamp value to ISO 8601 format for PostgreSQL. "Convert a timestamp value to ISO 8601 format.
Handles: integers (Unix epoch), local-time timestamps, strings, and NIL." Handles: integers (Unix epoch), local-time timestamps, strings, and NIL."
(cond (cond
((null value) nil) ((null value) nil)
@ -124,10 +124,10 @@ Handles: integers (Unix epoch), local-time timestamps, strings, and NIL."
(last-login (dm:field data-model "last-login"))) (last-login (dm:field data-model "last-login")))
(when created-date (when created-date
(setf (dm:field data-model "created-date") (setf (dm:field data-model "created-date")
(format-timestamp-for-postgres created-date))) (format-timestamp-iso8601 created-date)))
(when last-login (when last-login
(setf (dm:field data-model "last-login") (setf (dm:field data-model "last-login")
(format-timestamp-for-postgres last-login)))))) (format-timestamp-iso8601 last-login))))))
(defun data-model-save (data-model) (defun data-model-save (data-model)
"Wrapper on data-model save method to bypass error using dm:save on lambdalite. "Wrapper on data-model save method to bypass error using dm:save on lambdalite.

View File

@ -70,14 +70,10 @@
(when (and (= 1 user-active) (when (and (= 1 user-active)
(verify-password password user-password)) (verify-password password user-password))
;; Update last login using data-model (database agnostic) ;; Update last login using data-model (database agnostic)
;; Use ISO 8601 format in UTC that PostgreSQL TIMESTAMP can parse
(handler-case (handler-case
(progn (progn
(setf (dm:field user "last-login") (setf (dm:field user "last-login")
(local-time:format-timestring nil (local-time:now) (format-timestamp-iso8601 (local-time:now)))
:format '(:year "-" (:month 2) "-" (:day 2) " "
(:hour 2) ":" (:min 2) ":" (:sec 2))
:timezone local-time:+utc-zone+))
;; Use data-model-save to normalize all timestamp fields before saving ;; Use data-model-save to normalize all timestamp fields before saving
(data-model-save user)) (data-model-save user))
(error (e) (error (e)