Refactor ListPosts logic to use time.Time for createdAt and updatedAt; convert timestamps to Unix milliseconds
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s

This commit is contained in:
2025-10-26 17:22:40 +08:00
parent df712e8715
commit 116adb7922
2 changed files with 7 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
Name: Blog Name: Blog
Host: 0.0.0.0 Host: 0.0.0.0
Port: 8888 Port: 8888
Timeout: 9999999
JWT: JWT:
Secret: "${JWT_SECRET}" # JWT signing secret key Secret: "${JWT_SECRET}" # JWT signing secret key

View File

@@ -114,13 +114,18 @@ func (l *ListPostsLogic) ListPosts(req *types.ListPostsReq) (resp *types.ListPos
for rows.Next() { for rows.Next() {
var post types.ListPostsRespPosts var post types.ListPostsRespPosts
var coverID sql.NullInt64 var coverID sql.NullInt64
var createdAt, updatedAt time.Time
err := rows.Scan(&post.PostId, &post.Title, &post.CreatedAt, &post.UpdatedAt, &coverID) err := rows.Scan(&post.PostId, &post.Title, &createdAt, &updatedAt, &coverID)
if err != nil { if err != nil {
l.Errorf("Failed to scan post: %v", err) l.Errorf("Failed to scan post: %v", err)
continue continue
} }
// Convert timestamps to Unix milliseconds
post.CreatedAt = createdAt.UnixMilli()
post.UpdatedAt = updatedAt.UnixMilli()
// If cover image exists, get its URL // If cover image exists, get its URL
if coverID.Valid { if coverID.Valid {
coverQuery := `SELECT file_key FROM files WHERE id = $1` coverQuery := `SELECT file_key FROM files WHERE id = $1`
@@ -143,10 +148,6 @@ func (l *ListPostsLogic) ListPosts(req *types.ListPostsReq) (resp *types.ListPos
} }
} }
// Convert timestamps to Unix milliseconds
post.CreatedAt = post.CreatedAt * 1000
post.UpdatedAt = post.UpdatedAt * 1000
posts = append(posts, post) posts = append(posts, post)
} }