diff --git a/internal/storage/message.go b/internal/storage/message.go index adb0dd10..d5962122 100644 --- a/internal/storage/message.go +++ b/internal/storage/message.go @@ -1,6 +1,7 @@ package storage import ( + "encoding/json" "fmt" "time" @@ -24,6 +25,23 @@ func NewMessage() Message { return Message{} } +func (m *Message) AsMap() map[string]interface{} { + geoInformation, _ := m.GeoInformation.MarshalJSON() + telegram, _ := json.Marshal(m.Telegram) + mastodon, _ := json.Marshal(m.Mastodon) + + return map[string]interface{}{ + "id": m.ID, + "created_at": m.CreatedAt, + "updated_at": m.UpdatedAt, + "ticker_id": m.TickerID, + "text": m.Text, + "geo_information": string(geoInformation), + "telegram": telegram, + "mastodon": mastodon, + } +} + type TelegramMeta struct { Messages []tgbotapi.Message } diff --git a/internal/storage/sql_storage.go b/internal/storage/sql_storage.go index 5cf27ee9..1ab310cb 100644 --- a/internal/storage/sql_storage.go +++ b/internal/storage/sql_storage.go @@ -72,7 +72,7 @@ func (s *SqlStorage) SaveUser(user *User) error { log.WithError(err).WithField("user_id", user.ID).Error("failed to replace user tickers") } - return s.DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(user).Error + return s.DB.Session(&gorm.Session{FullSaveAssociations: true}).Model(user).Updates(user.AsMap()).Error } func (s *SqlStorage) DeleteUser(user User) error { @@ -170,7 +170,7 @@ func (s *SqlStorage) SaveTicker(ticker *Ticker) error { log.WithError(err).WithField("ticker_id", ticker.ID).Error("failed to replace ticker users") } - return s.DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(ticker).Error + return s.DB.Session(&gorm.Session{FullSaveAssociations: true}).Model(ticker).Updates(ticker.AsMap()).Error } func (s *SqlStorage) DeleteTicker(ticker Ticker) error { @@ -273,7 +273,7 @@ func (s *SqlStorage) SaveMessage(message *Message) error { return s.DB.Create(message).Error } - return s.DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(message).Error + return s.DB.Session(&gorm.Session{FullSaveAssociations: true}).Model(message).Updates(message.AsMap()).Error } func (s *SqlStorage) DeleteMessage(message Message) error { diff --git a/internal/storage/sql_storage_test.go b/internal/storage/sql_storage_test.go index 0dc5347d..57d26309 100644 --- a/internal/storage/sql_storage_test.go +++ b/internal/storage/sql_storage_test.go @@ -525,6 +525,31 @@ func (s *SqlStorageTestSuite) TestSaveTicker() { s.Equal(int64(1), count) }) + s.Run("when ticker is existing and properties are updated", func() { + ticker.Active = true + ticker.Information = TickerInformation{Author: "Author"} + ticker.Location = TickerLocation{Lat: 1, Lon: 1} + + s.NoError(s.store.SaveTicker(&ticker)) + s.True(ticker.Active) + s.Equal("Author", ticker.Information.Author) + s.Equal(float64(1), ticker.Location.Lat) + s.Equal(float64(1), ticker.Location.Lon) + + ticker.Active = false + ticker.Information.Author = "" + ticker.Location.Lat = 0 + ticker.Location.Lon = 0 + + s.NoError(s.store.SaveTicker(&ticker)) + + ticker, err := s.store.FindTickerByID(ticker.ID) + s.NoError(err) + s.Equal("", ticker.Information.Author) + s.Equal(float64(0), ticker.Location.Lat) + s.Equal(float64(0), ticker.Location.Lon) + }) + s.Run("when ticker is existing with users", func() { user := User{Email: "user@systemli.org"} err := s.db.Create(&user).Error diff --git a/internal/storage/ticker.go b/internal/storage/ticker.go index 7649f80c..680bf89a 100644 --- a/internal/storage/ticker.go +++ b/internal/storage/ticker.go @@ -33,6 +33,26 @@ func (t *Ticker) Reset() { t.Mastodon.Reset() } +func (t *Ticker) AsMap() map[string]interface{} { + return map[string]interface{}{ + "id": t.ID, + "created_at": t.CreatedAt, + "updated_at": t.UpdatedAt, + "domain": t.Domain, + "title": t.Title, + "description": t.Description, + "active": t.Active, + "author": t.Information.Author, + "url": t.Information.URL, + "email": t.Information.Email, + "twitter": t.Information.Twitter, + "facebook": t.Information.Facebook, + "telegram": t.Information.Telegram, + "lat": t.Location.Lat, + "lon": t.Location.Lon, + } +} + type TickerInformation struct { Author string URL string diff --git a/internal/storage/user.go b/internal/storage/user.go index 836a2fc9..63e99a0d 100644 --- a/internal/storage/user.go +++ b/internal/storage/user.go @@ -47,6 +47,17 @@ func (u *User) UpdatePassword(password string) { u.EncryptedPassword = pw } +func (u *User) AsMap() map[string]interface{} { + return map[string]interface{}{ + "id": u.ID, + "created_at": u.CreatedAt, + "updated_at": u.UpdatedAt, + "email": u.Email, + "encrypted_password": u.EncryptedPassword, + "is_super_admin": u.IsSuperAdmin, + } +} + func hashPassword(password string) (string, error) { pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil {