Skip to content

Commit

Permalink
🐛Ensure Fields are updated correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Apr 27, 2024
1 parent aa39ed7 commit 31914b6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
18 changes: 18 additions & 0 deletions internal/storage/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"encoding/json"
"fmt"
"time"

Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/storage/sql_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions internal/storage/sql_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions internal/storage/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions internal/storage/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 31914b6

Please sign in to comment.