refactor handlers and add search for tags
This commit is contained in:
@@ -5,8 +5,8 @@ import (
|
||||
"github.com/sentriz/gonic/server/subsonic"
|
||||
)
|
||||
|
||||
func makeChildFromFolder(f *model.Folder, parent *model.Folder) *subsonic.Child {
|
||||
child := &subsonic.Child{
|
||||
func makeChildFromFolder(f *model.Folder, parent *model.Folder) *subsonic.Track {
|
||||
child := &subsonic.Track{
|
||||
ID: f.ID,
|
||||
Title: f.Name,
|
||||
CoverID: f.CoverID,
|
||||
@@ -18,8 +18,8 @@ func makeChildFromFolder(f *model.Folder, parent *model.Folder) *subsonic.Child
|
||||
return child
|
||||
}
|
||||
|
||||
func makeChildFromTrack(t *model.Track, parent *model.Folder) *subsonic.Child {
|
||||
return &subsonic.Child{
|
||||
func makeChildFromTrack(t *model.Track, parent *model.Folder) *subsonic.Track {
|
||||
return &subsonic.Track{
|
||||
ID: t.ID,
|
||||
Album: t.Album.Title,
|
||||
Artist: t.TrackArtist,
|
||||
@@ -28,7 +28,7 @@ func makeChildFromTrack(t *model.Track, parent *model.Folder) *subsonic.Child {
|
||||
Size: t.Size,
|
||||
Suffix: t.Suffix,
|
||||
Title: t.Title,
|
||||
Track: t.TrackNumber,
|
||||
TrackNumber: t.TrackNumber,
|
||||
ParentID: parent.ID,
|
||||
CoverID: parent.CoverID,
|
||||
Duration: 0,
|
||||
@@ -41,7 +41,6 @@ func makeAlbumFromFolder(f *model.Folder) *subsonic.Album {
|
||||
return &subsonic.Album{
|
||||
ID: f.ID,
|
||||
Title: f.Name,
|
||||
Album: f.Name,
|
||||
CoverID: f.CoverID,
|
||||
ParentID: f.ParentID,
|
||||
Artist: f.Parent.Name,
|
||||
@@ -56,7 +55,7 @@ func makeArtistFromFolder(f *model.Folder) *subsonic.Artist {
|
||||
}
|
||||
}
|
||||
|
||||
func makeDirFromFolder(f *model.Folder, children []*subsonic.Child) *subsonic.Directory {
|
||||
func makeDirFromFolder(f *model.Folder, children []*subsonic.Track) *subsonic.Directory {
|
||||
return &subsonic.Directory{
|
||||
ID: f.ID,
|
||||
Parent: f.ParentID,
|
||||
|
||||
@@ -24,6 +24,7 @@ func makeTrackFromTrack(t *model.Track, album *model.Album) *subsonic.Track {
|
||||
TrackNumber: t.TrackNumber,
|
||||
ContentType: t.ContentType,
|
||||
Path: t.Path,
|
||||
ParentID: t.FolderID,
|
||||
Suffix: t.Suffix,
|
||||
CreatedAt: t.CreatedAt,
|
||||
Size: t.Size,
|
||||
|
||||
@@ -17,9 +17,6 @@ func TestFirstExisting(t *testing.T) {
|
||||
{"first missing",
|
||||
[]string{"", "two", "three"}, "default",
|
||||
"two"},
|
||||
{"middle missing",
|
||||
[]string{"", "two", ""}, "default",
|
||||
"two"},
|
||||
{"all missing",
|
||||
[]string{"", "", ""}, "default",
|
||||
"default"},
|
||||
|
||||
@@ -50,7 +50,7 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, r, 10, "please provide an `id` parameter")
|
||||
return
|
||||
}
|
||||
childrenObj := []*subsonic.Child{}
|
||||
childrenObj := []*subsonic.Track{}
|
||||
var folder model.Folder
|
||||
c.DB.First(&folder, id)
|
||||
//
|
||||
@@ -149,8 +149,8 @@ func (c *Controller) SearchTwo(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, r, 10, "please provide a `query` parameter")
|
||||
return
|
||||
}
|
||||
query = strings.TrimSuffix(query, "*")
|
||||
query = fmt.Sprintf("%%%s%%", query)
|
||||
query = fmt.Sprintf("%%%s%%",
|
||||
strings.TrimSuffix(query, "*"))
|
||||
results := &subsonic.SearchResultTwo{}
|
||||
//
|
||||
// search "artists"
|
||||
@@ -178,7 +178,7 @@ func (c *Controller) SearchTwo(w http.ResponseWriter, r *http.Request) {
|
||||
makeChildFromFolder(&a, a.Parent))
|
||||
}
|
||||
//
|
||||
// search "artists"
|
||||
// search tracks
|
||||
var tracks []model.Track
|
||||
c.DB.
|
||||
Preload("Folder").
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
@@ -139,3 +141,55 @@ func (c *Controller) GetAlbumListTwo(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) SearchThree(w http.ResponseWriter, r *http.Request) {
|
||||
query := getStrParam(r, "query")
|
||||
if query == "" {
|
||||
respondError(w, r, 10, "please provide a `query` parameter")
|
||||
return
|
||||
}
|
||||
query = fmt.Sprintf("%%%s%%",
|
||||
strings.TrimSuffix(query, "*"))
|
||||
results := &subsonic.SearchResultThree{}
|
||||
//
|
||||
// search "artists"
|
||||
var artists []model.Artist
|
||||
c.DB.
|
||||
Where("name LIKE ?", query).
|
||||
Offset(getIntParamOr(r, "artistOffset", 0)).
|
||||
Limit(getIntParamOr(r, "artistCount", 20)).
|
||||
Find(&artists)
|
||||
for _, a := range artists {
|
||||
results.Artists = append(results.Artists,
|
||||
makeArtistFromArtist(&a))
|
||||
}
|
||||
//
|
||||
// search "albums"
|
||||
var albums []model.Album
|
||||
c.DB.
|
||||
Preload("Artist").
|
||||
Where("title LIKE ?", query).
|
||||
Offset(getIntParamOr(r, "albumOffset", 0)).
|
||||
Limit(getIntParamOr(r, "albumCount", 20)).
|
||||
Find(&albums)
|
||||
for _, a := range albums {
|
||||
results.Albums = append(results.Albums,
|
||||
makeAlbumFromAlbum(&a, &a.Artist))
|
||||
}
|
||||
//
|
||||
// search tracks
|
||||
var tracks []model.Track
|
||||
c.DB.
|
||||
Preload("Album").
|
||||
Where("title LIKE ?", query).
|
||||
Offset(getIntParamOr(r, "songOffset", 0)).
|
||||
Limit(getIntParamOr(r, "songCount", 20)).
|
||||
Find(&tracks)
|
||||
for _, t := range tracks {
|
||||
results.Tracks = append(results.Tracks,
|
||||
makeTrackFromTrack(&t, &t.Album))
|
||||
}
|
||||
sub := subsonic.NewResponse()
|
||||
sub.SearchResultThree = results
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
@@ -10,17 +10,24 @@ import (
|
||||
"github.com/sentriz/gonic/server/subsonic"
|
||||
)
|
||||
|
||||
type metaResponse struct {
|
||||
XMLName xml.Name `xml:"subsonic-response" json:"-"`
|
||||
*subsonic.Response `json:"subsonic-response"`
|
||||
}
|
||||
|
||||
func respondRaw(w http.ResponseWriter, r *http.Request,
|
||||
code int, sub *subsonic.Response) {
|
||||
res := subsonic.MetaResponse{
|
||||
w.WriteHeader(code)
|
||||
res := metaResponse{
|
||||
Response: sub,
|
||||
}
|
||||
switch r.URL.Query().Get("f") {
|
||||
switch getStrParam(r, "f") {
|
||||
case "json":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to json: %v\n", err)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
case "jsonp":
|
||||
@@ -28,9 +35,9 @@ func respondRaw(w http.ResponseWriter, r *http.Request,
|
||||
data, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to json: %v\n", err)
|
||||
return
|
||||
}
|
||||
callback := r.URL.Query().Get("callback")
|
||||
w.Write([]byte(callback))
|
||||
w.Write([]byte(getStrParamOr(r, "callback", "cb")))
|
||||
w.Write([]byte("("))
|
||||
w.Write(data)
|
||||
w.Write([]byte(");"))
|
||||
@@ -39,6 +46,7 @@ func respondRaw(w http.ResponseWriter, r *http.Request,
|
||||
data, err := xml.MarshalIndent(res, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("could not marshall to xml: %v\n", err)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user