2016-03-17 01:27:48 +01:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
2016-03-17 01:51:03 +01:00
|
|
|
"encoding/json"
|
2016-03-17 01:27:48 +01:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/deluan/gosonic/engine"
|
|
|
|
)
|
|
|
|
|
2016-03-17 01:51:03 +01:00
|
|
|
var (
|
|
|
|
nowPlayingKeyName = []byte("nowplaying")
|
|
|
|
)
|
|
|
|
|
2016-03-17 01:27:48 +01:00
|
|
|
type nowPlayingRepository struct {
|
|
|
|
ledisRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNowPlayingRepository() engine.NowPlayingRepository {
|
|
|
|
r := &nowPlayingRepository{}
|
2016-03-17 01:51:03 +01:00
|
|
|
r.init("nowplaying", &engine.NowPlayingInfo{})
|
2016-03-17 01:27:48 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-03-17 02:04:41 +01:00
|
|
|
func (r *nowPlayingRepository) Set(id string) error {
|
2016-03-17 01:27:48 +01:00
|
|
|
if id == "" {
|
|
|
|
return errors.New("Id is required")
|
|
|
|
}
|
|
|
|
m := &engine.NowPlayingInfo{TrackId: id, Start: time.Now()}
|
2016-03-17 01:51:03 +01:00
|
|
|
|
|
|
|
h, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-17 02:04:41 +01:00
|
|
|
return Db().SetEX(nowPlayingKeyName, int64(engine.NowPlayingExpire.Seconds()), []byte(h))
|
2016-03-17 01:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ engine.NowPlayingRepository = (*nowPlayingRepository)(nil)
|