aboutsummaryrefslogtreecommitdiff
path: root/song.go
blob: 3a77e5b482b1a79d816301c7f779566f8d094224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
}