diff --git a/internal/api/response/ticker.go b/internal/api/response/ticker.go index 9e4d9df..b4a1080 100644 --- a/internal/api/response/ticker.go +++ b/internal/api/response/ticker.go @@ -23,14 +23,16 @@ type Ticker struct { } type Information struct { - Author string `json:"author"` - URL string `json:"url"` - Email string `json:"email"` - Twitter string `json:"twitter"` - Facebook string `json:"facebook"` - Telegram string `json:"telegram"` - Mastodon string `json:"mastodon"` - Bluesky string `json:"bluesky"` + Author string `json:"author"` + URL string `json:"url"` + Email string `json:"email"` + Twitter string `json:"twitter"` + Facebook string `json:"facebook"` + Instagram string `json:"instagram"` + Threads string `json:"threads"` + Telegram string `json:"telegram"` + Mastodon string `json:"mastodon"` + Bluesky string `json:"bluesky"` } type Website struct { @@ -91,14 +93,16 @@ func TickerResponse(t storage.Ticker, config config.Config) Ticker { Description: t.Description, Active: t.Active, Information: Information{ - Author: t.Information.Author, - URL: t.Information.URL, - Email: t.Information.Email, - Twitter: t.Information.Twitter, - Facebook: t.Information.Facebook, - Telegram: t.Information.Telegram, - Mastodon: t.Information.Mastodon, - Bluesky: t.Information.Bluesky, + Author: t.Information.Author, + URL: t.Information.URL, + Email: t.Information.Email, + Twitter: t.Information.Twitter, + Facebook: t.Information.Facebook, + Instagram: t.Information.Instagram, + Threads: t.Information.Threads, + Telegram: t.Information.Telegram, + Mastodon: t.Information.Mastodon, + Bluesky: t.Information.Bluesky, }, Websites: websites, Telegram: Telegram{ diff --git a/internal/api/tickers.go b/internal/api/tickers.go index 3c82d2f..14b01da 100644 --- a/internal/api/tickers.go +++ b/internal/api/tickers.go @@ -15,6 +15,33 @@ import ( "github.com/systemli/ticker/internal/storage" ) +type TickerParam struct { + Title string `json:"title" binding:"required"` + Description string `json:"description"` + Active bool `json:"active"` + + Information TickerInformationParam `json:"information"` + Location TickerLocationParam `json:"location"` +} + +type TickerInformationParam struct { + Author string `json:"author"` + URL string `json:"url"` + Email string `json:"email"` + Twitter string `json:"twitter"` + Facebook string `json:"facebook"` + Instagram string `json:"instagram"` + Threads string `json:"threads"` + Telegram string `json:"telegram"` + Mastodon string `json:"mastodon"` + Bluesky string `json:"bluesky"` +} + +type TickerLocationParam struct { + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` +} + func (h *handler) GetTickers(c *gin.Context) { me, err := helper.Me(c) if err != nil { @@ -515,26 +542,7 @@ func (h *handler) ClearTickerCache(ticker *storage.Ticker) { } func updateTicker(t *storage.Ticker, c *gin.Context) error { - var body struct { - Title string `json:"title" binding:"required"` - Description string `json:"description"` - Active bool `json:"active"` - Information struct { - Author string `json:"author"` - URL string `json:"url"` - Email string `json:"email"` - Twitter string `json:"twitter"` - Facebook string `json:"facebook"` - Telegram string `json:"telegram"` - Mastodon string `json:"mastodon"` - Bluesky string `json:"bluesky"` - } `json:"information"` - Location struct { - Lat float64 `json:"lat"` - Lon float64 `json:"lon"` - } - } - + var body TickerParam err := c.Bind(&body) if err != nil { return err @@ -548,6 +556,8 @@ func updateTicker(t *storage.Ticker, c *gin.Context) error { t.Information.Email = body.Information.Email t.Information.Twitter = body.Information.Twitter t.Information.Facebook = body.Information.Facebook + t.Information.Instagram = body.Information.Instagram + t.Information.Threads = body.Information.Threads t.Information.Telegram = body.Information.Telegram t.Information.Mastodon = body.Information.Mastodon t.Information.Bluesky = body.Information.Bluesky diff --git a/internal/api/tickers_test.go b/internal/api/tickers_test.go index 2c7c04e..31c8e2d 100644 --- a/internal/api/tickers_test.go +++ b/internal/api/tickers_test.go @@ -1,6 +1,7 @@ package api import ( + "bytes" "encoding/json" "errors" "fmt" @@ -131,7 +132,7 @@ func (s *TickerTestSuite) TestPostTicker() { }) s.Run("when storage returns error", func() { - body := `{"domain":"localhost","title":"title","description":"description"}` + body := `{"title":"title","description":"description"}` s.ctx.Request = httptest.NewRequest(http.MethodPost, "/v1/admin/tickers", strings.NewReader(body)) s.ctx.Request.Header.Add("Content-Type", "application/json") s.store.On("SaveTicker", mock.Anything).Return(errors.New("storage error")).Once() @@ -143,10 +144,54 @@ func (s *TickerTestSuite) TestPostTicker() { }) s.Run("when storage returns ticker", func() { - body := `{"domain":"localhost","title":"title","description":"description"}` - s.ctx.Request = httptest.NewRequest(http.MethodPost, "/v1/admin/tickers", strings.NewReader(body)) + param := TickerParam{ + Title: "title", + Description: "description", + Active: true, + Information: TickerInformationParam{ + Author: "author", + URL: "https://example.org", + Email: "author@example.org", + Twitter: "author", + Facebook: "author", + Instagram: "author", + Threads: "author", + Telegram: "author", + Mastodon: "https://example.org/@author", + Bluesky: "https://author.bsky.social", + }, + Location: TickerLocationParam{ + Lat: 1, + Lon: 1, + }, + } + b, err := json.Marshal(param) + s.NoError(err) + + s.ctx.Request = httptest.NewRequest(http.MethodPost, "/v1/admin/tickers", bytes.NewReader(b)) s.ctx.Request.Header.Add("Content-Type", "application/json") - s.store.On("SaveTicker", mock.Anything).Return(nil).Once() + ticker := storage.Ticker{ + Title: "title", + Description: "description", + Active: true, + Information: storage.TickerInformation{ + Author: "author", + URL: "https://example.org", + Email: "author@example.org", + Twitter: "author", + Facebook: "author", + Instagram: "author", + Threads: "author", + Telegram: "author", + Mastodon: "https://example.org/@author", + Bluesky: "https://author.bsky.social", + }, + Location: storage.TickerLocation{ + Lat: 1, + Lon: 1, + }, + } + s.store.On("SaveTicker", &ticker).Return(nil).Once() h := s.handler() h.PostTicker(s.ctx) diff --git a/internal/storage/ticker.go b/internal/storage/ticker.go index 294a342..429b4cd 100644 --- a/internal/storage/ticker.go +++ b/internal/storage/ticker.go @@ -51,14 +51,16 @@ func (t *Ticker) AsMap() map[string]interface{} { } type TickerInformation struct { - Author string - URL string - Email string - Twitter string - Facebook string - Telegram string - Mastodon string - Bluesky string + Author string + URL string + Email string + Twitter string + Facebook string + Instagram string + Threads string + Telegram string + Mastodon string + Bluesky string } type TickerWebsite struct {