From 8cbac9bf1fcc1c2cec144893ebef87e60379d28d Mon Sep 17 00:00:00 2001 From: Nikolay Dimitrov Date: Mon, 11 Nov 2024 10:23:04 +0900 Subject: [PATCH] Fix created date for Moments in SQLite --- db/db.go | 7 +++++-- handlers/moment.go | 12 ++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/db/db.go b/db/db.go index 27b6e7b..4c3687e 100644 --- a/db/db.go +++ b/db/db.go @@ -10,8 +10,9 @@ import ( ) var ( - Instance *gorm.DB - TimestampFunc = "" + Instance *gorm.DB + TimestampFunc = "" + CreatedDateFunc = "" ) func Init() { @@ -26,6 +27,7 @@ func Init() { log.Fatalf("MySQL DB error: %v", err) } TimestampFunc = "unix_timestamp()" + CreatedDateFunc = "date(from_unixtime(created_at))" } else if config.SQLITE_FILE != "" { // Sqlite setup db, err = gorm.Open(sqlite.Open(config.SQLITE_FILE), &gorm.Config{}) @@ -37,6 +39,7 @@ func Init() { // sqliteDB.SetMaxOpenConns(1) // } TimestampFunc = "strftime('%s', 'now')" + CreatedDateFunc = "date(created_at, 'unixepoch')" } else { log.Fatal("No database configuration found") } diff --git a/handlers/moment.go b/handlers/moment.go index f4d2a90..d520e48 100644 --- a/handlers/moment.go +++ b/handlers/moment.go @@ -44,16 +44,16 @@ func MomentList(c *gin.Context, user *models.User) { rows, err := db.Instance.Raw(` select date, if(city = '', area, city), - group_concat(place_id separator ',') places, + group_concat(place_id) places, max(hero), min(start), max(end) from (select place_id, - from_unixtime(created_at, '%Y-%m-%d') date, - max(id) hero, - count(*) cnt, - min(created_at) start, - max(created_at) end + `+db.CreatedDateFunc+` date, + max(id) hero, + count(*) cnt, + min(created_at) start, + max(created_at) end from assets where user_id = ? and deleted = 0