Skip to content

Commit

Permalink
fix(zess): season validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Dec 10, 2024
1 parent 071975a commit 26347ea
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 61 deletions.
12 changes: 6 additions & 6 deletions cmd/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func main() {
zap.S().Fatal("DB: Fatal error\n", err)
}

// Tap
_, _ = cmd.Tap(db)
// // Tap
// _, _ = cmd.Tap(db)

// Zess
_, _, _ = cmd.Zess(db)

// Gamification
_, _ = cmd.Gamification(db)
// // Gamification
// _, _ = cmd.Gamification(db)

// Event
_, _ = cmd.Event(db)
// // Event
// _, _ = cmd.Event(db)

// Spotify
spotify, err := cmd.Song(db)
Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/buzzer/buzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ var defaultSong = []string{

// New returns a new buzzer instance
func New() *Buzzer {
song := config.GetDefaultStringSlice("backend.buzzer.song", defaultSong)
return &Buzzer{
Song: song,
Song: config.GetDefaultStringSlice("backend.buzzer.song", defaultSong),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/db/dto/season.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Season struct {
Name string `json:"name" validate:"required"`
Start date.Date `json:"start" validate:"required"`
End date.Date `json:"end" validate:"required"`
Current bool `json:"is_current"` // FIXME: This should have `required`. However when added the validation fails even though it's present
Current bool `json:"is_current" validate:"boolean"`
}

// SeasonDTO converts a sqlc.Season to a Season
Expand Down
10 changes: 2 additions & 8 deletions internal/pkg/event/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ import (
"github.com/gocolly/colly"
"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

// TODO: Look at https://github.com/PuerkitoBio/goquery

var layout = "Monday 02 January, 15:04 2006"

var (
website = config.GetDefaultString("backend.event.website", "https://zeus.gent/events")
websitePoster = config.GetDefaultString("backend.event.website_poster", "https://git.zeus.gent/ZeusWPI/visueel/raw/branch/master")
)

func (e *Event) getEvents() ([]dto.Event, error) {
zap.S().Info("Events: Getting all events")

Expand Down Expand Up @@ -73,7 +67,7 @@ func (e *Event) getEvents() ([]dto.Event, error) {
events = append(events, event)
})

err := c.Visit(website)
err := c.Visit(e.website)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -102,7 +96,7 @@ func (e *Event) getPoster(event *dto.Event) error {

year := fmt.Sprintf("20%d-20%d", yearStart, yearEnd)

url := fmt.Sprintf("%s/%s/%s/scc.png", websitePoster, year, event.Name)
url := fmt.Sprintf("%s/%s/%s/scc.png", e.websitePoster, year, event.Name)

req := fiber.Get(url)
status, body, errs := req.Bytes()
Expand Down
11 changes: 9 additions & 2 deletions internal/pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ import (

"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/pkg/config"
)

// Event represents a event instance
type Event struct {
db *db.DB
db *db.DB
website string
websitePoster string
}

// New creates a new event instance
func New(db *db.DB) *Event {
return &Event{db: db}
return &Event{
db: db,
website: config.GetDefaultString("backend.event.website", "https://zeus.gent/events"),
websitePoster: config.GetDefaultString("backend.event.website_poster", "https://git.zeus.gent/ZeusWPI/visueel/raw/branch/master"),
}
}

// Update gets all events from the website of this academic year
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/gamification/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

var api = config.GetDefaultString("backend.gamification.api", "https://gamification.zeus.gent")

type gamificationItem struct {
ID int32 `json:"id"`
Name string `json:"github_name"`
Expand All @@ -22,7 +19,7 @@ type gamificationItem struct {
func (g *Gamification) getLeaderboard() ([]dto.Gamification, error) {
zap.S().Info("Gamification: Getting leaderboard")

req := fiber.Get(api+"/top4").Set("Accept", "application/json")
req := fiber.Get(g.api+"/top4").Set("Accept", "application/json")

res := new([]gamificationItem)
status, _, errs := req.Struct(res)
Expand Down
9 changes: 7 additions & 2 deletions internal/pkg/gamification/gamification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import (
"errors"

"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/pkg/config"
)

// Gamification represents a gamification instance
type Gamification struct {
db *db.DB
db *db.DB
api string
}

// New creates a new gamification instance
func New(db *db.DB) *Gamification {
return &Gamification{db: db}
return &Gamification{
db: db,
api: config.GetDefaultString("backend.gamification.api", "https://gamification.zeus.gent"),
}
}

// Update gets the current leaderboard from gamification
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/song/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

var apiAccount = config.GetDefaultString("backend.song.spotify_api_account", "https://accounts.spotify.com/api/token")

type accountResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Expand All @@ -23,7 +20,7 @@ func (s *Song) refreshToken() error {
form := &fiber.Args{}
form.Add("grant_type", "client_credentials")

req := fiber.Post(apiAccount).Form(form).BasicAuth(s.ClientID, s.ClientSecret)
req := fiber.Post(s.apiAccount).Form(form).BasicAuth(s.ClientID, s.ClientSecret)

res := new(accountResponse)
status, _, errs := req.Struct(res)
Expand Down
12 changes: 3 additions & 9 deletions internal/pkg/song/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

var (
api = config.GetDefaultString("backend.song.spotify_api", "https://api.spotify.com/v1")
apiLrclib = config.GetDefaultString("backend.song.lrclib_api", "https://lrclib.net/api")
)

type trackArtist struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -35,7 +29,7 @@ type trackResponse struct {
func (s *Song) getTrack(track *dto.Song) error {
zap.S().Info("Song: Getting track info for id: ", track.SpotifyID)

req := fiber.Get(fmt.Sprintf("%s/%s/%s", api, "tracks", track.SpotifyID)).
req := fiber.Get(fmt.Sprintf("%s/%s/%s", s.api, "tracks", track.SpotifyID)).
Set("Authorization", fmt.Sprintf("Bearer %s", s.AccessToken))

res := new(trackResponse)
Expand Down Expand Up @@ -76,7 +70,7 @@ type artistResponse struct {
func (s *Song) getArtist(artist *dto.SongArtist) error {
zap.S().Info("Song: Getting artists info for ", artist.ID)

req := fiber.Get(fmt.Sprintf("%s/%s/%s", api, "artists", artist.SpotifyID)).
req := fiber.Get(fmt.Sprintf("%s/%s/%s", s.api, "artists", artist.SpotifyID)).
Set("Authorization", fmt.Sprintf("Bearer %s", s.AccessToken))

res := new(artistResponse)
Expand Down Expand Up @@ -124,7 +118,7 @@ func (s *Song) getLyrics(track *dto.Song) error {
params.Set("album_name", track.Album)
params.Set("duration", fmt.Sprintf("%d", track.DurationMS/1000))

req := fiber.Get(fmt.Sprintf("%s/get?%s", apiLrclib, params.Encode()))
req := fiber.Get(fmt.Sprintf("%s/get?%s", s.apiLrclib, params.Encode()))

res := new(lyricsResponse)
status, _, errs := req.Struct(res)
Expand Down
14 changes: 13 additions & 1 deletion internal/pkg/song/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type Song struct {
ClientSecret string
AccessToken string
ExpiresTime int64

api string
apiAccount string
apiLrclib string
}

// New creates a new song instance
Expand All @@ -31,7 +35,15 @@ func New(db *db.DB) (*Song, error) {
return &Song{}, errors.New("Song: Spotify client id or secret not set")
}

return &Song{db: db, ClientID: clientID, ClientSecret: clientSecret, ExpiresTime: 0}, nil
return &Song{
db: db,
ClientID: clientID,
ClientSecret: clientSecret,
ExpiresTime: 0,
api: config.GetDefaultString("backend.song.spotify_api", "https://api.spotify.com/v1"),
apiAccount: config.GetDefaultString("backend.song.spotify_api_account", "https://accounts.spotify.com/api/token"),
apiLrclib: config.GetDefaultString("backend.song.lrclib_api", "https://lrclib.net/api"),
}, nil
}

// Track gets information about the current track and stores it in the database
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/tap/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (
"time"

"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

var api = config.GetDefaultString("backend.tap.api", "https://tap.zeus.gent")

type orderResponseItem struct {
OrderID int32 `json:"order_id"`
OrderCreatedAt time.Time `json:"order_created_at"`
Expand All @@ -25,7 +22,7 @@ type orderResponse struct {
func (t *Tap) getOrders() ([]orderResponseItem, error) {
zap.S().Info("Tap: Getting orders")

req := fiber.Get(api + "/recent")
req := fiber.Get(t.api + "/recent")

res := new(orderResponse)
status, _, errs := req.Struct(res)
Expand Down
9 changes: 6 additions & 3 deletions internal/pkg/tap/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type Tap struct {
db *db.DB
beers []string
api string
}

var defaultBeers = []string{
Expand All @@ -37,9 +38,11 @@ var defaultBeers = []string{

// New creates a new tap instance
func New(db *db.DB) *Tap {
beers := config.GetDefaultStringSlice("backend.tap.beers", defaultBeers)

return &Tap{db: db, beers: beers}
return &Tap{
db: db,
beers: config.GetDefaultStringSlice("backend.tap.beers", defaultBeers),
api: config.GetDefaultString("backend.tap.api", "https://tap.zeus.gent"),
}
}

// Update gets all new orders from tap
Expand Down
9 changes: 2 additions & 7 deletions internal/pkg/zess/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

var (
api = config.GetDefaultString("backend.zess.api", "https://zess.zeus.gent")
)

func (z *Zess) getSeasons() (*[]*dto.Season, error) {
zap.S().Info("Zess: Getting seasons")

req := fiber.Get(api + "/seasons")
req := fiber.Get(z.api + "/seasons")

res := new([]*dto.Season)
status, _, errs := req.Struct(res)
Expand All @@ -41,7 +36,7 @@ func (z *Zess) getSeasons() (*[]*dto.Season, error) {
func (z *Zess) getScans() (*[]*dto.Scan, error) {
zap.S().Info("Zess: Getting scans")

req := fiber.Get(api + "/recent_scans")
req := fiber.Get(z.api + "/recent_scans")

res := new([]*dto.Scan)
status, _, errs := req.Struct(res)
Expand Down
9 changes: 7 additions & 2 deletions internal/pkg/zess/zess.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import (
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/internal/pkg/db/sqlc"
"github.com/zeusWPI/scc/pkg/config"
"github.com/zeusWPI/scc/pkg/util"
)

// Zess represents a zess instance
type Zess struct {
db *db.DB
db *db.DB
api string
}

// New creates a new zess instance
func New(db *db.DB) *Zess {
return &Zess{db: db}
return &Zess{
db: db,
api: config.GetDefaultString("backend.zess.api", "https://zess.zeus.gent/api"),
}
}

// UpdateSeasons updates the seasons
Expand Down
4 changes: 1 addition & 3 deletions tui/screen/cammie/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/zeusWPI/scc/tui/view/zess"
)

var bottomTimeout = config.GetDefaultInt("tui.screen.cammie.interval_s", 300)

// Cammie represents the cammie screen
type Cammie struct {
db *db.DB
Expand Down Expand Up @@ -164,7 +162,7 @@ func (c *Cammie) GetSizeMsg() tea.Msg {
}

func updateBottomIndex(cammie Cammie) tea.Cmd {
timeout := time.Duration(bottomTimeout * int(time.Second))
timeout := time.Duration(config.GetDefaultInt("tui.screen.cammie.interval_s", 300) * int(time.Second))
return tea.Tick(timeout, func(_ time.Time) tea.Msg {
newIndex := (cammie.indexTop + 1) % len(cammie.top)

Expand Down
6 changes: 3 additions & 3 deletions tui/view/zess/zess.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"go.uber.org/zap"
)

var maxWeeks = config.GetDefaultInt("tui.view.zess.weeks", 10)

// yearWeek represents a yearWeek object by keeping the year and week number
type yearWeek struct {
year int
Expand All @@ -32,6 +30,7 @@ type Model struct {
db *db.DB
lastScanID int32
scans []weekScan // Scans per week
showWeeks int // Amount of weeks to show
maxWeekScans int64
currentSeason yearWeek // Start week of the season
seasonScans int64
Expand Down Expand Up @@ -59,6 +58,7 @@ func NewModel(db *db.DB) view.View {
db: db,
lastScanID: -1,
scans: make([]weekScan, 0),
showWeeks: config.GetDefaultInt("tui.view.zess.weeks", 10),
maxWeekScans: -1,
currentSeason: yearWeek{year: -1, week: -1},
seasonScans: 0,
Expand Down Expand Up @@ -122,7 +122,7 @@ func (m *Model) Update(msg tea.Msg) (view.View, tea.Cmd) {
m.maxWeekScans = newScan.amount
}
// Make sure the array doesn't get too big
if len(m.scans) > maxWeeks {
if len(m.scans) > m.showWeeks {
m.scans = m.scans[:1]
}
}
Expand Down

0 comments on commit 26347ea

Please sign in to comment.