Exemple #1
0
func updateCurrent() error {
	var err error

	if TrackCount() == 0 {
		return errors.New("Track is not exists")
	}

	redisClient := redis.Get()
	defer redisClient.Close()

	t := getRandam()
	err = t.Validate()

	if err != nil {
		t.Destroy()
		return err
	}

	_, err = redisClient.Do(
		"HMSET", "gokuraku:current-track",
		"track_id", t.Id,
		"started_at", strconv.FormatInt(time.Now().Unix(), 10),
	)

	return err
}
Exemple #2
0
func (ct *CurrentTrack) Destroy() {
	var err error
	redisClient := redis.Get()
	defer redisClient.Close()

	_, err = redisClient.Do("DEL", "gokuraku:current-track")

	if err != nil {
		log.Panicln(err)
	}
}
Exemple #3
0
func TrackCount() int {
	var err error
	redisClient := redis.Get()
	defer redisClient.Close()
	count, err := redis.Int(redisClient.Do("SCARD", "gokuraku:track-ids"))

	if err != nil {
		log.Panicln(err)
	}

	return count
}
Exemple #4
0
func (t *Track) Destroy() {
	var err error
	redisClient := redis.Get()
	defer redisClient.Close()
	_, err = redisClient.Do("SREM", "gokuraku:track-ids", t.Id)

	if err != nil {
		log.Panicln(err)
	}

	_, err = redisClient.Do("DEL", "gokuraku:track:"+t.Id)

	if err != nil {
		log.Panicln(err)
	}
}
Exemple #5
0
func FindTrack(id string) (Track, error) {
	redisClient := redis.Get()
	defer redisClient.Close()

	values, err := redis.Values(redisClient.Do("HGETALL", "gokuraku:track:"+id))
	if err != nil {
		log.Panicln(err)
	}

	track := Track{}
	err = redis.ScanStruct(values, &track)
	if err != nil {
		log.Panicln(err)
	}

	return track, nil
}
Exemple #6
0
func getRandam() *Track {
	redisClient := redis.Get()
	defer redisClient.Close()

	trackId, _ := redis.String(redisClient.Do("SRANDMEMBER", "gokuraku:track-ids"))
	values, err := redis.Values(redisClient.Do("HGETALL", "gokuraku:track:"+trackId))
	if err != nil {
		panic(err)
	}

	track := Track{}
	err = redis.ScanStruct(values, &track)
	if err != nil {
		panic(err)
	}

	return &track
}
Exemple #7
0
func TrackAll() ([]*Track, error) {
	redisClient := redis.Get()
	defer redisClient.Close()

	trackIds, _ := redis.Strings(redisClient.Do("SORT", "gokuraku:track-ids", "By", "gokuraku:track:*->created_at", "ALPHA", "DESC"))

	tracks := []*Track{}
	for _, trackId := range trackIds {
		track, err := FindTrack(trackId)

		if err != nil {
			log.Panicln(err)
		}

		tracks = append(tracks, &track)
	}

	return tracks, nil
}
Exemple #8
0
func GetCurrent() (*CurrentTrack, error) {
	var err error
	redisClient := redis.Get()
	defer redisClient.Close()

	exists, _ := redis.Bool(redisClient.Do("EXISTS", "gokuraku:current-track"))
	if exists == false {
		err := updateCurrent()

		if err != nil {
			return nil, err
		}

		return GetCurrent()
	}

	values, err := redis.Values(redisClient.Do("HGETALL", "gokuraku:current-track"))
	currentTrack := CurrentTrack{}
	err = redis.ScanStruct(values, &currentTrack)

	if err != nil {
		log.Panicln(err)
	}

	err = currentTrack.Validate()

	if err != nil {
		currentTrack.Destroy()
		return nil, err
	}

	track, err := FindTrack(currentTrack.TrackId)

	if err != nil {
		log.Panicln(err)
	}

	return &CurrentTrack{currentTrack.TrackId, currentTrack.StartedAt, &track}, nil
}
Exemple #9
0
func (t *Track) Save() error {
	var err error
	redisClient := redis.Get()
	defer redisClient.Close()

	_, err = redisClient.Do("SADD", "gokuraku:track-ids", t.Id)

	if err != nil {
		return err
	}

	err = t.Validate()

	if err != nil {
		return err
	}

	_, err = redisClient.Do(
		"HMSET", "gokuraku:track:"+t.Id,
		"id", t.Id,
		"title", t.Title,
		"type", t.Type,
		"description", t.Description,
		"image_url", t.ImageUrl,
		"user_image_url", t.UserImageUrl,
		"user_url", t.UserUrl,
		"user_name", t.UserName,
		"url", t.Url,
		"genre", t.Genre,
		"created_at", t.CreatedAt,
	)

	if err != nil {
		return err
	}

	return nil
}