Skip to content

Commit

Permalink
Merge pull request #248 from systemli/Embed-Ticker-Information-into-t…
Browse files Browse the repository at this point in the history
…he-Ticker-Table

⚡️ Embed Ticker Information into the Ticker Table
  • Loading branch information
0x46616c6b authored Oct 21, 2023
2 parents f712eb8 + 471ace8 commit 5fbd392
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
3 changes: 1 addition & 2 deletions internal/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/systemli/ticker/internal/api/helper"
"github.com/systemli/ticker/internal/api/response"
"github.com/systemli/ticker/internal/storage"
)

// GetInit returns the basic settings for the ticker.
Expand All @@ -31,7 +30,7 @@ func (h *handler) GetInit(c *gin.Context) {
return
}

ticker, err := h.storage.FindTickerByDomain(domain, storage.WithInformation())
ticker, err := h.storage.FindTickerByDomain(domain)
if err != nil || !ticker.Active {
settings.InactiveSettings = h.storage.GetInactiveSettings()
c.JSON(http.StatusOK, response.SuccessResponse(map[string]interface{}{"ticker": nil, "settings": settings}))
Expand Down
14 changes: 6 additions & 8 deletions internal/legacy/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ func (m *Migration) Do() error {
Description: oldTicker.Description,
Active: oldTicker.Active,
Information: storage.TickerInformation{
CreatedAt: oldTicker.CreationDate,
UpdatedAt: time.Now(),
Author: oldTicker.Information.Author,
URL: oldTicker.Information.URL,
Email: oldTicker.Information.Email,
Twitter: oldTicker.Information.Twitter,
Facebook: oldTicker.Information.Facebook,
Telegram: oldTicker.Information.Telegram,
Author: oldTicker.Information.Author,
URL: oldTicker.Information.URL,
Email: oldTicker.Information.Email,
Twitter: oldTicker.Information.Twitter,
Facebook: oldTicker.Information.Facebook,
Telegram: oldTicker.Information.Telegram,
},
Telegram: storage.TickerTelegram{
CreatedAt: oldTicker.CreationDate,
Expand Down
6 changes: 0 additions & 6 deletions internal/storage/sql_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,3 @@ func WithAttachments() func(*gorm.DB) *gorm.DB {
return db.Preload("Attachments")
}
}

func WithInformation() func(*gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Preload("Information")
}
}
29 changes: 20 additions & 9 deletions internal/storage/sql_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,28 +346,39 @@ var _ = Describe("SqlStorage", func() {
Expect(err).To(HaveOccurred())
Expect(ticker).To(BeZero())

err = db.Create(&Ticker{Domain: "systemli.org"}).Error
ticker = Ticker{
Domain: "systemli.org",
Information: TickerInformation{
Author: "Author",
},
}
err = db.Create(&ticker).Error
Expect(err).ToNot(HaveOccurred())

ticker, err = store.FindTickerByDomain("systemli.org")
Expect(err).ToNot(HaveOccurred())
Expect(ticker).ToNot(BeZero())
Expect(ticker.Information.Author).To(Equal("Author"))
})

It("returns the ticker with the given domain and preload information", func() {
err = db.Create(&Ticker{
It("returns the ticker for the given domain with preload all associations", func() {
ticker = Ticker{
Domain: "systemli.org",
Information: TickerInformation{
Author: "Author",
Mastodon: TickerMastodon{
Active: true,
},
}).Error
Telegram: TickerTelegram{
Active: true,
},
}
err = db.Create(&ticker).Error
Expect(err).ToNot(HaveOccurred())

ticker, err := store.FindTickerByDomain("systemli.org", WithInformation())
ticker, err = store.FindTickerByDomain("systemli.org", WithPreload())
Expect(err).ToNot(HaveOccurred())
Expect(ticker).ToNot(BeZero())

Expect(ticker.Information.Author).To(Equal("Author"))
Expect(ticker.Mastodon.Active).To(BeTrue())
Expect(ticker.Telegram.Active).To(BeTrue())
})
})

Expand Down
18 changes: 7 additions & 11 deletions internal/storage/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Ticker struct {
Title string
Description string
Active bool
Information TickerInformation
Information TickerInformation `gorm:"embedded"`
Telegram TickerTelegram
Mastodon TickerMastodon
Location TickerLocation `gorm:"embedded"`
Expand All @@ -34,16 +34,12 @@ func (t *Ticker) Reset() {
}

type TickerInformation struct {
ID int `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
TickerID int
Author string
URL string
Email string
Twitter string
Facebook string
Telegram string
Author string
URL string
Email string
Twitter string
Facebook string
Telegram string
}

type TickerTelegram struct {
Expand Down

0 comments on commit 5fbd392

Please sign in to comment.