use default null instead of pointer to primary key
This commit is contained in:
@@ -10,5 +10,5 @@ type CrudBase struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IDBase struct {
|
type IDBase struct {
|
||||||
ID *int `gorm:"primary_key"`
|
ID int `gorm:"primary_key"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,6 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// q: why in tarnation are all the foreign keys pointers to ints?
|
|
||||||
// a: so they will be true sqlite null values instead of go zero
|
|
||||||
// values when we save a row without that value
|
|
||||||
//
|
|
||||||
// q: what in tarnation are the `IsNew`s for?
|
// q: what in tarnation are the `IsNew`s for?
|
||||||
// a: it's a bit of a hack - but we set a models IsNew to true if
|
// a: it's a bit of a hack - but we set a models IsNew to true if
|
||||||
// we just filled it in for the first time, so when it comes
|
// we just filled it in for the first time, so when it comes
|
||||||
@@ -18,14 +14,14 @@ type Album struct {
|
|||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
AlbumArtist AlbumArtist
|
AlbumArtist AlbumArtist
|
||||||
AlbumArtistID *int `gorm:"index" sql:"type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
AlbumArtistID int `gorm:"index" sql:"default: null; type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
||||||
Title string `gorm:"not null;index"`
|
Title string `gorm:"not null; index"`
|
||||||
// an Album having a `Path` is a little weird when browsing by tags
|
// an Album having a `Path` is a little weird when browsing by tags
|
||||||
// (for the most part - the library's folder structure is treated as
|
// (for the most part - the library's folder structure is treated as
|
||||||
// if it were flat), but this solves the "American Football problem"
|
// if it were flat), but this solves the "American Football problem"
|
||||||
// https://en.wikipedia.org/wiki/American_Football_(band)#Discography
|
// https://en.wikipedia.org/wiki/American_Football_(band)#Discography
|
||||||
Path string `gorm:"not null;unique_index"`
|
Path string `gorm:"not null; unique_index"`
|
||||||
CoverID *int `sql:"type:int REFERENCES covers(id)"`
|
CoverID int `sql:"default: null; type:int REFERENCES covers(id)"`
|
||||||
Cover Cover
|
Cover Cover
|
||||||
Year int
|
Year int
|
||||||
Tracks []Track
|
Tracks []Track
|
||||||
@@ -36,7 +32,7 @@ type Album struct {
|
|||||||
type AlbumArtist struct {
|
type AlbumArtist struct {
|
||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Name string `gorm:"not null;unique_index"`
|
Name string `gorm:"not null; unique_index"`
|
||||||
Albums []Album
|
Albums []Album
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,9 +41,9 @@ type Track struct {
|
|||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Album Album
|
Album Album
|
||||||
AlbumID *int `gorm:"index" sql:"type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
AlbumID int `gorm:"index" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||||
AlbumArtist AlbumArtist
|
AlbumArtist AlbumArtist
|
||||||
AlbumArtistID *int `gorm:"index" sql:"type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
AlbumArtistID int `gorm:"index" sql:"default: null; type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
||||||
Artist string
|
Artist string
|
||||||
Bitrate int
|
Bitrate int
|
||||||
Codec string
|
Codec string
|
||||||
@@ -62,8 +58,8 @@ type Track struct {
|
|||||||
ContentType string
|
ContentType string
|
||||||
Size int
|
Size int
|
||||||
Folder Folder
|
Folder Folder
|
||||||
FolderID *int `gorm:"not null;index" sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
FolderID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
||||||
Path string `gorm:"not null;unique_index"`
|
Path string `gorm:"not null; unique_index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cover represents the covers table
|
// Cover represents the covers table
|
||||||
@@ -71,7 +67,7 @@ type Cover struct {
|
|||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Image []byte
|
Image []byte
|
||||||
Path string `gorm:"not null;unique_index"`
|
Path string `gorm:"not null; unique_index"`
|
||||||
IsNew bool `gorm:"-"`
|
IsNew bool `gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +75,7 @@ type Cover struct {
|
|||||||
type User struct {
|
type User struct {
|
||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Name string `gorm:"not null;unique_index"`
|
Name string `gorm:"not null; unique_index"`
|
||||||
Password string
|
Password string
|
||||||
LastFMSession string
|
LastFMSession string
|
||||||
IsAdmin bool
|
IsAdmin bool
|
||||||
@@ -88,7 +84,7 @@ type User struct {
|
|||||||
// Setting represents the settings table
|
// Setting represents the settings table
|
||||||
type Setting struct {
|
type Setting struct {
|
||||||
CrudBase
|
CrudBase
|
||||||
Key string `gorm:"primary_key;auto_increment:false"`
|
Key string `gorm:"primary_key; auto_increment:false"`
|
||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,11 +92,11 @@ type Setting struct {
|
|||||||
type Play struct {
|
type Play struct {
|
||||||
IDBase
|
IDBase
|
||||||
User User
|
User User
|
||||||
UserID *int `gorm:"not null;index" sql:"type:int REFERENCES users(id) ON DELETE CASCADE"`
|
UserID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
|
||||||
Album Album
|
Album Album
|
||||||
AlbumID *int `gorm:"not null;index" sql:"type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
AlbumID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||||
Folder Folder
|
Folder Folder
|
||||||
FolderID *int `gorm:"not null;index" sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
FolderID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
||||||
Time time.Time
|
Time time.Time
|
||||||
Count int
|
Count int
|
||||||
}
|
}
|
||||||
@@ -110,11 +106,11 @@ type Folder struct {
|
|||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Name string
|
Name string
|
||||||
Path string `gorm:"not null;unique_index"`
|
Path string `gorm:"not null; unique_index"`
|
||||||
Parent *Folder
|
Parent *Folder
|
||||||
ParentID *int `sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
ParentID int `sql:"default: null; type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
||||||
CoverID *int `sql:"type:int REFERENCES covers(id)"`
|
CoverID int `sql:"default: null; type:int REFERENCES covers(id)"`
|
||||||
HasTracks bool `gorm:"not null;index"`
|
HasTracks bool `gorm:"not null; index"`
|
||||||
Cover Cover
|
Cover Cover
|
||||||
IsNew bool `gorm:"-"`
|
IsNew bool `gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ func (s *folderStack) Peek() model.Folder {
|
|||||||
return (*s)[l-1]
|
return (*s)[l-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *folderStack) PeekID() *int {
|
func (s *folderStack) PeekID() int {
|
||||||
l := len(*s)
|
l := len(*s)
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
return nil
|
return 0
|
||||||
}
|
}
|
||||||
return (*s)[l-1].ID
|
return (*s)[l-1].ID
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,12 @@ func (c *Controller) SetSetting(key, value string) {
|
|||||||
|
|
||||||
func (c *Controller) GetUserFromName(name string) *model.User {
|
func (c *Controller) GetUserFromName(name string) *model.User {
|
||||||
var user model.User
|
var user model.User
|
||||||
c.DB.Where("name = ?", name).First(&user)
|
err := c.DB.
|
||||||
|
Where("name = ?", name).
|
||||||
|
First(&user).
|
||||||
|
Error
|
||||||
|
if gorm.IsRecordNotFoundError(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return &user
|
return &user
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.GetUserFromName(username)
|
user := c.GetUserFromName(username)
|
||||||
if !(username == user.Name && password == user.Password) {
|
if user == nil || password != user.Password {
|
||||||
session.AddFlash("invalid username / password")
|
session.AddFlash("invalid username / password")
|
||||||
session.Save(r, w)
|
session.Save(r, w)
|
||||||
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
|
|||||||
indexes = append(indexes, index)
|
indexes = append(indexes, index)
|
||||||
}
|
}
|
||||||
index.Artists = append(index.Artists, &subsonic.Artist{
|
index.Artists = append(index.Artists, &subsonic.Artist{
|
||||||
ID: *folder.ID,
|
ID: folder.ID,
|
||||||
Name: folder.Name,
|
Name: folder.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -58,11 +58,11 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
|||||||
Find(&folders)
|
Find(&folders)
|
||||||
for _, folder := range folders {
|
for _, folder := range folders {
|
||||||
childrenObj = append(childrenObj, &subsonic.Child{
|
childrenObj = append(childrenObj, &subsonic.Child{
|
||||||
Parent: *cFolder.ID,
|
Parent: cFolder.ID,
|
||||||
ID: *folder.ID,
|
ID: folder.ID,
|
||||||
Title: folder.Name,
|
Title: folder.Name,
|
||||||
IsDir: true,
|
IsDir: true,
|
||||||
CoverID: *folder.CoverID,
|
CoverID: folder.CoverID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
@@ -80,14 +80,14 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
|||||||
track.Suffix = "mp3"
|
track.Suffix = "mp3"
|
||||||
}
|
}
|
||||||
childrenObj = append(childrenObj, &subsonic.Child{
|
childrenObj = append(childrenObj, &subsonic.Child{
|
||||||
ID: *track.ID,
|
ID: track.ID,
|
||||||
Album: track.Album.Title,
|
Album: track.Album.Title,
|
||||||
Artist: track.Artist,
|
Artist: track.Artist,
|
||||||
ContentType: track.ContentType,
|
ContentType: track.ContentType,
|
||||||
CoverID: *cFolder.CoverID,
|
CoverID: cFolder.CoverID,
|
||||||
Duration: 0,
|
Duration: 0,
|
||||||
IsDir: false,
|
IsDir: false,
|
||||||
Parent: *cFolder.ID,
|
Parent: cFolder.ID,
|
||||||
Path: track.Path,
|
Path: track.Path,
|
||||||
Size: track.Size,
|
Size: track.Size,
|
||||||
Suffix: track.Suffix,
|
Suffix: track.Suffix,
|
||||||
@@ -100,8 +100,8 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
|||||||
// respond section
|
// respond section
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
sub.Directory = &subsonic.Directory{
|
sub.Directory = &subsonic.Directory{
|
||||||
ID: *cFolder.ID,
|
ID: cFolder.ID,
|
||||||
Parent: *cFolder.ParentID,
|
Parent: cFolder.ParentID,
|
||||||
Name: cFolder.Name,
|
Name: cFolder.Name,
|
||||||
Children: childrenObj,
|
Children: childrenObj,
|
||||||
}
|
}
|
||||||
@@ -162,11 +162,11 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
|
|||||||
listObj := []*subsonic.Album{}
|
listObj := []*subsonic.Album{}
|
||||||
for _, folder := range folders {
|
for _, folder := range folders {
|
||||||
listObj = append(listObj, &subsonic.Album{
|
listObj = append(listObj, &subsonic.Album{
|
||||||
ID: *folder.ID,
|
ID: folder.ID,
|
||||||
Title: folder.Name,
|
Title: folder.Name,
|
||||||
Album: folder.Name,
|
Album: folder.Name,
|
||||||
CoverID: *folder.CoverID,
|
CoverID: folder.CoverID,
|
||||||
ParentID: *folder.ParentID,
|
ParentID: folder.ParentID,
|
||||||
IsDir: true,
|
IsDir: true,
|
||||||
Artist: folder.Parent.Name,
|
Artist: folder.Parent.Name,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) {
|
|||||||
indexes.List = append(indexes.List, index)
|
indexes.List = append(indexes.List, index)
|
||||||
}
|
}
|
||||||
index.Artists = append(index.Artists, &subsonic.Artist{
|
index.Artists = append(index.Artists, &subsonic.Artist{
|
||||||
ID: *artist.ID,
|
ID: artist.ID,
|
||||||
Name: artist.Name,
|
Name: artist.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -49,17 +49,17 @@ func (c *Controller) GetArtist(w http.ResponseWriter, r *http.Request) {
|
|||||||
albumsObj := []*subsonic.Album{}
|
albumsObj := []*subsonic.Album{}
|
||||||
for _, album := range artist.Albums {
|
for _, album := range artist.Albums {
|
||||||
albumsObj = append(albumsObj, &subsonic.Album{
|
albumsObj = append(albumsObj, &subsonic.Album{
|
||||||
ID: *album.ID,
|
ID: album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
Artist: artist.Name,
|
Artist: artist.Name,
|
||||||
ArtistID: *artist.ID,
|
ArtistID: artist.ID,
|
||||||
CoverID: *album.CoverID,
|
CoverID: album.CoverID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
sub.Artist = &subsonic.Artist{
|
sub.Artist = &subsonic.Artist{
|
||||||
ID: *artist.ID,
|
ID: artist.ID,
|
||||||
Name: artist.Name,
|
Name: artist.Name,
|
||||||
Albums: albumsObj,
|
Albums: albumsObj,
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,7 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
tracksObj := []*subsonic.Track{}
|
tracksObj := []*subsonic.Track{}
|
||||||
for _, track := range album.Tracks {
|
for _, track := range album.Tracks {
|
||||||
tracksObj = append(tracksObj, &subsonic.Track{
|
tracksObj = append(tracksObj, &subsonic.Track{
|
||||||
ID: *track.ID,
|
ID: track.ID,
|
||||||
Title: track.Title,
|
Title: track.Title,
|
||||||
Artist: track.Artist, // track artist
|
Artist: track.Artist, // track artist
|
||||||
TrackNo: track.TrackNumber,
|
TrackNo: track.TrackNumber,
|
||||||
@@ -90,17 +90,17 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
Created: track.CreatedAt,
|
Created: track.CreatedAt,
|
||||||
Size: track.Size,
|
Size: track.Size,
|
||||||
Album: album.Title,
|
Album: album.Title,
|
||||||
AlbumID: *album.ID,
|
AlbumID: album.ID,
|
||||||
ArtistID: *album.AlbumArtist.ID, // album artist
|
ArtistID: album.AlbumArtist.ID, // album artist
|
||||||
CoverID: *album.CoverID,
|
CoverID: album.CoverID,
|
||||||
Type: "music",
|
Type: "music",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
sub.Album = &subsonic.Album{
|
sub.Album = &subsonic.Album{
|
||||||
ID: *album.ID,
|
ID: album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
CoverID: *album.CoverID,
|
CoverID: album.CoverID,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
Artist: album.AlbumArtist.Name,
|
Artist: album.AlbumArtist.Name,
|
||||||
Tracks: tracksObj,
|
Tracks: tracksObj,
|
||||||
@@ -164,12 +164,12 @@ func (c *Controller) GetAlbumListTwo(w http.ResponseWriter, r *http.Request) {
|
|||||||
listObj := []*subsonic.Album{}
|
listObj := []*subsonic.Album{}
|
||||||
for _, album := range albums {
|
for _, album := range albums {
|
||||||
listObj = append(listObj, &subsonic.Album{
|
listObj = append(listObj, &subsonic.Album{
|
||||||
ID: *album.ID,
|
ID: album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
CoverID: *album.CoverID,
|
CoverID: album.CoverID,
|
||||||
Artist: album.AlbumArtist.Name,
|
Artist: album.AlbumArtist.Name,
|
||||||
ArtistID: *album.AlbumArtist.ID,
|
ArtistID: album.AlbumArtist.ID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (c *Controller) WithUserSession(next http.HandlerFunc) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
// take username from sesion and add the user row to the context
|
// take username from sesion and add the user row to the context
|
||||||
user := c.GetUserFromName(username)
|
user := c.GetUserFromName(username)
|
||||||
if *user.ID == 0 {
|
if user == nil {
|
||||||
// the username in the client's session no longer relates to a
|
// the username in the client's session no longer relates to a
|
||||||
// user in the database (maybe the user was deleted)
|
// user in the database (maybe the user was deleted)
|
||||||
session.Options.MaxAge = -1
|
session.Options.MaxAge = -1
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ func (c *Controller) WithValidSubsonicArgs(next http.HandlerFunc) http.HandlerFu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.GetUserFromName(username)
|
user := c.GetUserFromName(username)
|
||||||
if *user.ID == 0 {
|
if user == nil {
|
||||||
// the user does not exist
|
// the user does not exist
|
||||||
respondError(w, r, 40, "invalid username")
|
respondError(w, r, 40, "invalid username")
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user