From c162743200466b6d8d9e52d5666e1220c9cf67a2 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 31 Jul 2023 10:19:29 +0200 Subject: move song and connection code into separate files --- song.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 song.go (limited to 'song.go') diff --git a/song.go b/song.go new file mode 100644 index 0000000..3a77e5b --- /dev/null +++ b/song.go @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/json" + + "muhq.space/go/wrms/llog" + + "github.com/google/uuid" +) + +type Song struct { + Title string `json:"title"` + Artist string `json:"artist"` + Source string `json:"source"` + Uri string `json:"uri"` + Weight float64 `json:"weight"` + Album string `json:"album"` + Year int `json:"year"` + index int `json:"-"` // used by heap.Interface + Upvotes map[uuid.UUID]struct{} `json:"upvotes"` + Downvotes map[uuid.UUID]struct{} `json:"downvotes"` +} + +func NewSong(title, artist, source, uri string) *Song { + return &Song{ + Title: title, + Artist: artist, + Source: source, + Uri: uri, + Upvotes: map[uuid.UUID]struct{}{}, + Downvotes: map[uuid.UUID]struct{}{}, + } +} + +func NewDetailedSong(title, artist, source, uri, album string, year int) *Song { + s := NewSong(title, artist, source, uri) + s.Album = album + s.Year = year + return s +} + +func NewSongFromJson(data []byte) (*Song, error) { + var s Song + err := json.Unmarshal(data, &s) + if err != nil { + llog.Error("Failed to parse song data %s with %s", string(data), err) + return nil, err + } + + s.Upvotes = map[uuid.UUID]struct{}{} + s.Downvotes = map[uuid.UUID]struct{}{} + return &s, nil +} -- cgit v1.2.3