Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add Fields for Instagram and Threads #354

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions internal/api/response/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
50 changes: 30 additions & 20 deletions internal/api/tickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
53 changes: 49 additions & 4 deletions internal/api/tickers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -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()
Expand All @@ -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: "[email protected]",
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: "[email protected]",
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)

Expand Down
18 changes: 10 additions & 8 deletions internal/storage/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading