/
source.go
41 lines (31 loc) 路 904 Bytes
/
source.go
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
package edb
import (
"github.com/jinzhu/gorm"
"time"
)
type AudioSource struct {
DBModel
Service string `gorm:"UNIQUE_INDEX:uix_uri_type;NOT NULL"`
URI string `gorm:"UNIQUE_INDEX:uix_uri_type;NOT NULL"`
TrackSources []TrackSource
}
type TrackSource struct {
DBModel
AudioSourceID uint64 `gorm:"NOT NULL"`
AudioSource *AudioSource
TrackID uint64 `gorm:"NOT NULL"`
Track Track
StartOffsetMS uint32 `gorm:"type:integer"`
EndOffsetMS uint32 `gorm:"type:integer"`
}
// Length returns the length of the track as a duration.
func (ts TrackSource) Length() time.Duration {
return time.Duration(ts.EndOffsetMS-ts.StartOffsetMS) * time.Millisecond
}
func GetTrackSource(db *gorm.DB, trackID uint64) (TrackSource, error) {
var trackSource TrackSource
err := db.Preload("AudioSource").
Take(&trackSource, "track_id = ?", trackID).
Error
return trackSource, err
}