move models into db package

This commit is contained in:
sentriz
2020-02-19 20:08:37 +00:00
parent e67588623b
commit a42edc3441
23 changed files with 133 additions and 145 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/jinzhu/gorm"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
"senan.xyz/g/gonic/server/ctrlsubsonic/params"
"senan.xyz/g/gonic/server/ctrlsubsonic/spec"
)
@@ -20,7 +20,7 @@ import (
// under the root directory
func (c *Controller) ServeGetIndexes(r *http.Request) *spec.Response {
var folders []*model.Album
var folders []*db.Album
c.DB.
Select("*, count(sub.id) as child_count").
Joins(`
@@ -65,11 +65,11 @@ func (c *Controller) ServeGetMusicDirectory(r *http.Request) *spec.Response {
return spec.NewError(10, "please provide an `id` parameter")
}
childrenObj := []*spec.TrackChild{}
folder := &model.Album{}
folder := &db.Album{}
c.DB.First(folder, id)
//
// start looking for child childFolders in the current dir
var childFolders []*model.Album
var childFolders []*db.Album
c.DB.
Where("parent_id = ?", id).
Find(&childFolders)
@@ -78,7 +78,7 @@ func (c *Controller) ServeGetMusicDirectory(r *http.Request) *spec.Response {
}
//
// start looking for child childTracks in the current dir
var childTracks []*model.Track
var childTracks []*db.Track
c.DB.
Where("album_id = ?", id).
Preload("Album").
@@ -118,7 +118,7 @@ func (c *Controller) ServeGetAlbumList(r *http.Request) *spec.Response {
case "alphabeticalByName":
q = q.Order("right_path")
case "frequent":
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
q = q.Joins(`
JOIN plays
ON albums.id = plays.album_id AND plays.user_id = ?`,
@@ -129,7 +129,7 @@ func (c *Controller) ServeGetAlbumList(r *http.Request) *spec.Response {
case "random":
q = q.Order(gorm.Expr("random()"))
case "recent":
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
q = q.Joins(`
JOIN plays
ON albums.id = plays.album_id AND plays.user_id = ?`,
@@ -138,7 +138,7 @@ func (c *Controller) ServeGetAlbumList(r *http.Request) *spec.Response {
default:
return spec.NewError(10, "unknown value `%s` for parameter 'type'", listType)
}
var folders []*model.Album
var folders []*db.Album
q.
Where("albums.tag_artist_id IS NOT NULL").
Offset(params.GetIntOr("offset", 0)).
@@ -165,7 +165,7 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response {
results := &spec.SearchResultTwo{}
//
// search "artists"
var artists []*model.Album
var artists []*db.Album
c.DB.
Where(`
parent_id = 1
@@ -181,7 +181,7 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response {
}
//
// search "albums"
var albums []*model.Album
var albums []*db.Album
c.DB.
Where(`
tag_artist_id IS NOT NULL
@@ -196,7 +196,7 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response {
}
//
// search tracks
var tracks []*model.Track
var tracks []*db.Track
c.DB.
Preload("Album").
Where(`

View File

@@ -8,14 +8,14 @@ import (
"github.com/jinzhu/gorm"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
"senan.xyz/g/gonic/server/ctrlsubsonic/params"
"senan.xyz/g/gonic/server/ctrlsubsonic/spec"
"senan.xyz/g/gonic/server/lastfm"
)
func (c *Controller) ServeGetArtists(r *http.Request) *spec.Response {
var artists []*model.Artist
var artists []*db.Artist
c.DB.
Select("*, count(sub.id) as album_count").
Joins(`
@@ -57,7 +57,7 @@ func (c *Controller) ServeGetArtist(r *http.Request) *spec.Response {
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
artist := &model.Artist{}
artist := &db.Artist{}
c.DB.
Preload("Albums").
First(artist, id)
@@ -77,7 +77,7 @@ func (c *Controller) ServeGetAlbum(r *http.Request) *spec.Response {
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
album := &model.Album{}
album := &db.Album{}
err = c.DB.
Preload("TagArtist").
Preload("Tracks", func(db *gorm.DB) *gorm.DB {
@@ -121,7 +121,7 @@ func (c *Controller) ServeGetAlbumListTwo(r *http.Request) *spec.Response {
params.GetIntOr("toYear", 2200))
q = q.Order("tag_year")
case "frequent":
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
q = q.Joins(`
JOIN plays
ON albums.id = plays.album_id AND plays.user_id = ?`,
@@ -132,7 +132,7 @@ func (c *Controller) ServeGetAlbumListTwo(r *http.Request) *spec.Response {
case "random":
q = q.Order(gorm.Expr("random()"))
case "recent":
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
q = q.Joins(`
JOIN plays
ON albums.id = plays.album_id AND plays.user_id = ?`,
@@ -141,7 +141,7 @@ func (c *Controller) ServeGetAlbumListTwo(r *http.Request) *spec.Response {
default:
return spec.NewError(10, "unknown value `%s` for parameter 'type'", listType)
}
var albums []*model.Album
var albums []*db.Album
q.
Where("albums.tag_artist_id IS NOT NULL").
Offset(params.GetIntOr("offset", 0)).
@@ -169,7 +169,7 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
results := &spec.SearchResultThree{}
//
// search "artists"
var artists []*model.Artist
var artists []*db.Artist
c.DB.
Where(`
name LIKE ? OR
@@ -184,7 +184,7 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
}
//
// search "albums"
var albums []*model.Album
var albums []*db.Album
c.DB.
Preload("TagArtist").
Where(`
@@ -200,7 +200,7 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
}
//
// search tracks
var tracks []*model.Track
var tracks []*db.Track
c.DB.
Preload("Album").
Where(`
@@ -229,7 +229,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
if apiKey == "" {
return spec.NewError(0, "please set ask your admin to set the last.fm api key")
}
artist := &model.Artist{}
artist := &db.Artist{}
err = c.DB.
Where("id = ?", id).
Find(artist).
@@ -263,7 +263,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
if i == count {
break
}
artist = &model.Artist{}
artist = &db.Artist{}
err = c.DB.
Select("*, count(albums.id) as album_count").
Where("name = ?", similarInfo.Name).

View File

@@ -10,7 +10,7 @@ import (
"github.com/jinzhu/gorm"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
"senan.xyz/g/gonic/scanner"
"senan.xyz/g/gonic/server/ctrlsubsonic/params"
"senan.xyz/g/gonic/server/ctrlsubsonic/spec"
@@ -44,12 +44,12 @@ func (c *Controller) ServeScrobble(r *http.Request) *spec.Response {
return spec.NewError(10, "please provide an `id` parameter")
}
// fetch user to get lastfm session
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
if user.LastFMSession == "" {
return spec.NewError(0, "you don't have a last.fm session")
}
// fetch track for getting info to send to last.fm function
track := &model.Track{}
track := &db.Track{}
c.DB.
Preload("Album").
Preload("Artist").
@@ -96,7 +96,7 @@ func (c *Controller) ServeStartScan(r *http.Request) *spec.Response {
func (c *Controller) ServeGetScanStatus(r *http.Request) *spec.Response {
var trackCount int
c.DB.
Model(model.Track{}).
Model(db.Track{}).
Count(&trackCount)
sub := spec.NewResponse()
sub.ScanStatus = &spec.ScanStatus{
@@ -107,7 +107,7 @@ func (c *Controller) ServeGetScanStatus(r *http.Request) *spec.Response {
}
func (c *Controller) ServeGetUser(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
sub := spec.NewResponse()
sub.User = &spec.User{
Username: user.Name,
@@ -123,8 +123,8 @@ func (c *Controller) ServeNotFound(r *http.Request) *spec.Response {
}
func (c *Controller) ServeGetPlaylists(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*model.User)
var playlists []*model.Playlist
user := r.Context().Value(CtxUser).(*db.User)
var playlists []*db.Playlist
c.DB.Where("user_id = ?", user.ID).Find(&playlists)
sub := spec.NewResponse()
sub.Playlists = &spec.Playlists{
@@ -144,7 +144,7 @@ func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
playlist := model.Playlist{}
playlist := db.Playlist{}
err = c.DB.
Where("id = ?", playlistID).
Find(&playlist).
@@ -152,7 +152,7 @@ func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
if gorm.IsRecordNotFoundError(err) {
return spec.NewError(70, "playlist with id `%d` not found", playlistID)
}
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
sub := spec.NewResponse()
sub.Playlist = spec.NewPlaylist(&playlist)
sub.Playlist.Owner = user.Name
@@ -160,7 +160,7 @@ func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
trackIDs := playlist.GetItems()
sub.Playlist.List = make([]*spec.TrackChild, len(trackIDs))
for i, id := range trackIDs {
track := model.Track{}
track := db.Track{}
c.DB.
Where("id = ?", id).
Preload("Album").
@@ -171,7 +171,7 @@ func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
}
func (c *Controller) ServeUpdatePlaylist(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*model.User)
user := r.Context().Value(CtxUser).(*db.User)
params := r.Context().Value(CtxParams).(params.Params)
var playlistID int
if p := params.GetFirstList("id", "playlistId"); p != nil {
@@ -179,7 +179,7 @@ func (c *Controller) ServeUpdatePlaylist(r *http.Request) *spec.Response {
}
// playlistID may be 0 from above. in that case we get a new playlist
// as intended
playlist := &model.Playlist{ID: playlistID}
playlist := &db.Playlist{ID: playlistID}
c.DB.Where(playlist).First(playlist)
// ** begin update meta info
playlist.UserID = user.ID
@@ -211,13 +211,13 @@ func (c *Controller) ServeDeletePlaylist(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
c.DB.
Where("id = ?", params.GetIntOr("id", 0)).
Delete(&model.Playlist{})
Delete(&db.Playlist{})
return spec.NewResponse()
}
func (c *Controller) ServeGetPlayQueue(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*model.User)
queue := model.PlayQueue{}
user := r.Context().Value(CtxUser).(*db.User)
queue := db.PlayQueue{}
err := c.DB.
Where("user_id = ?", user.ID).
Find(&queue).
@@ -235,7 +235,7 @@ func (c *Controller) ServeGetPlayQueue(r *http.Request) *spec.Response {
trackIDs := queue.GetItems()
sub.PlayQueue.List = make([]*spec.TrackChild, len(trackIDs))
for i, id := range trackIDs {
track := model.Track{}
track := db.Track{}
c.DB.
Where("id = ?", id).
Preload("Album").
@@ -251,8 +251,8 @@ func (c *Controller) ServeSavePlayQueue(r *http.Request) *spec.Response {
if tracks == nil {
return spec.NewError(10, "please provide some `id` parameters")
}
user := r.Context().Value(CtxUser).(*model.User)
queue := &model.PlayQueue{UserID: user.ID}
user := r.Context().Value(CtxUser).(*db.User)
queue := &db.PlayQueue{UserID: user.ID}
c.DB.Where(queue).First(queue)
queue.Current = params.GetIntOr("current", 0)
queue.Position = params.GetIntOr("position", 0)
@@ -268,7 +268,7 @@ func (c *Controller) ServeGetSong(r *http.Request) *spec.Response {
if err != nil {
return spec.NewError(10, "provide an `id` parameter")
}
track := &model.Track{}
track := &db.Track{}
err = c.DB.
Where("id = ?", id).
Preload("Album").

View File

@@ -7,7 +7,7 @@ import (
"github.com/jinzhu/gorm"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
"senan.xyz/g/gonic/server/ctrlsubsonic/params"
"senan.xyz/g/gonic/server/ctrlsubsonic/spec"
)
@@ -24,7 +24,7 @@ func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *s
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
folder := &model.Album{}
folder := &db.Album{}
err = c.DB.
Select("id, left_path, right_path, cover").
First(folder, id).
@@ -51,7 +51,7 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
track := &model.Track{}
track := &db.Track{}
err = c.DB.
Preload("Album").
First(track, id).
@@ -68,8 +68,8 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
http.ServeFile(w, r, absPath)
//
// after we've served the file, mark the album as played
user := r.Context().Value(CtxUser).(*model.User)
play := model.Play{
user := r.Context().Value(CtxUser).(*db.User)
play := db.Play{
AlbumID: track.Album.ID,
UserID: user.ID,
}

View File

@@ -1,8 +1,8 @@
package spec
import "senan.xyz/g/gonic/model"
import "senan.xyz/g/gonic/db"
func NewPlaylist(p *model.Playlist) *Playlist {
func NewPlaylist(p *db.Playlist) *Playlist {
return &Playlist{
ID: p.ID,
Name: p.Name,

View File

@@ -3,10 +3,10 @@ package spec
import (
"path"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
)
func NewAlbumByFolder(f *model.Album) *Album {
func NewAlbumByFolder(f *db.Album) *Album {
a := &Album{
Artist: f.Parent.RightPath,
ID: f.ID,
@@ -21,7 +21,7 @@ func NewAlbumByFolder(f *model.Album) *Album {
return a
}
func NewTCAlbumByFolder(f *model.Album) *TrackChild {
func NewTCAlbumByFolder(f *db.Album) *TrackChild {
trCh := &TrackChild{
ID: f.ID,
IsDir: true,
@@ -35,7 +35,7 @@ func NewTCAlbumByFolder(f *model.Album) *TrackChild {
return trCh
}
func NewTCTrackByFolder(t *model.Track, parent *model.Album) *TrackChild {
func NewTCTrackByFolder(t *db.Track, parent *db.Album) *TrackChild {
trCh := &TrackChild{
ID: t.ID,
ContentType: t.MIME(),
@@ -66,7 +66,7 @@ func NewTCTrackByFolder(t *model.Track, parent *model.Album) *TrackChild {
return trCh
}
func NewArtistByFolder(f *model.Album) *Artist {
func NewArtistByFolder(f *db.Album) *Artist {
return &Artist{
ID: f.ID,
Name: f.RightPath,
@@ -74,7 +74,7 @@ func NewArtistByFolder(f *model.Album) *Artist {
}
}
func NewDirectoryByFolder(f *model.Album, children []*TrackChild) *Directory {
func NewDirectoryByFolder(f *db.Album, children []*TrackChild) *Directory {
dir := &Directory{
ID: f.ID,
Name: f.RightPath,

View File

@@ -3,10 +3,10 @@ package spec
import (
"path"
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/db"
)
func NewAlbumByTags(a *model.Album, artist *model.Artist) *Album {
func NewAlbumByTags(a *db.Album, artist *db.Artist) *Album {
ret := &Album{
Created: a.ModifiedAt,
ID: a.ID,
@@ -23,7 +23,7 @@ func NewAlbumByTags(a *model.Album, artist *model.Artist) *Album {
return ret
}
func NewTrackByTags(t *model.Track, album *model.Album) *TrackChild {
func NewTrackByTags(t *db.Track, album *db.Album) *TrackChild {
ret := &TrackChild{
ID: t.ID,
ContentType: t.MIME(),
@@ -55,7 +55,7 @@ func NewTrackByTags(t *model.Track, album *model.Album) *TrackChild {
return ret
}
func NewArtistByTags(a *model.Artist) *Artist {
func NewArtistByTags(a *db.Artist) *Artist {
return &Artist{
ID: a.ID,
Name: a.Name,

Binary file not shown.