make recently added persistent

This commit is contained in:
sentriz
2019-07-08 21:54:20 +01:00
parent 97b9837910
commit f2ac3b2cdf
7 changed files with 23 additions and 41 deletions

View File

@@ -1,12 +0,0 @@
package model
import "time"
type CrudBase struct {
CreatedAt time.Time
UpdatedAt time.Time
}
type IDBase struct {
ID int `gorm:"primary_key"`
}

View File

@@ -9,7 +9,7 @@ import (
) )
type Artist struct { type Artist struct {
IDBase ID int `gorm:"primary_key"`
Name string `gorm:"not null; unique_index"` Name string `gorm:"not null; unique_index"`
NameUDec string `sql:"default: null"` NameUDec string `sql:"default: null"`
Albums []*Album `gorm:"foreignkey:TagArtistID"` Albums []*Album `gorm:"foreignkey:TagArtistID"`
@@ -24,8 +24,9 @@ func (a *Artist) IndexName() string {
} }
type Track struct { type Track struct {
IDBase ID int `gorm:"primary_key"`
CrudBase CreatedAt time.Time
UpdatedAt time.Time
Filename string `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null"` Filename string `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null"`
FilenameUDec string `sql:"default: null"` FilenameUDec string `sql:"default: null"`
Album *Album Album *Album
@@ -56,8 +57,8 @@ func (t *Track) MIME() string {
} }
type User struct { type User struct {
IDBase ID int `gorm:"primary_key"`
CrudBase CreatedAt time.Time
Name string `gorm:"not null; unique_index" sql:"default: null"` Name string `gorm:"not null; unique_index" sql:"default: null"`
Password string `gorm:"not null" sql:"default: null"` Password string `gorm:"not null" sql:"default: null"`
LastFMSession string `sql:"default: null"` LastFMSession string `sql:"default: null"`
@@ -65,13 +66,12 @@ type User struct {
} }
type Setting struct { type Setting struct {
CrudBase
Key string `gorm:"not null; primary_key; auto_increment:false" sql:"default: null"` Key string `gorm:"not null; primary_key; auto_increment:false" sql:"default: null"`
Value string `sql:"default: null"` Value string `sql:"default: null"`
} }
type Play struct { type Play struct {
IDBase ID int `gorm:"primary_key"`
User *User User *User
UserID int `gorm:"not null; index" sql:"default: null; 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
@@ -81,8 +81,9 @@ type Play struct {
} }
type Album struct { type Album struct {
IDBase ID int `gorm:"primary_key"`
CrudBase UpdatedAt time.Time
ModifiedAt time.Time
LeftPath string `gorm:"unique_index:idx_left_path_right_path"` LeftPath string `gorm:"unique_index:idx_left_path_right_path"`
RightPath string `gorm:"not null; unique_index:idx_left_path_right_path" sql:"default: null"` RightPath string `gorm:"not null; unique_index:idx_left_path_right_path" sql:"default: null"`
RightPathUDec string `sql:"default: null"` RightPathUDec string `sql:"default: null"`

View File

@@ -264,6 +264,7 @@ func (s *Scanner) handleFolder(it *item) error {
folder.LeftPath = it.directory folder.LeftPath = it.directory
folder.RightPath = it.filename folder.RightPath = it.filename
folder.RightPathUDec = decoded(it.filename) folder.RightPathUDec = decoded(it.filename)
folder.ModifiedAt = it.stat.ModTime()
s.db.Save(folder) s.db.Save(folder)
folder.ReceivedPaths = true folder.ReceivedPaths = true
return nil return nil

View File

@@ -6,20 +6,12 @@ import (
"senan.xyz/g/gonic/model" "senan.xyz/g/gonic/model"
) )
func egAlbum(id int) *model.Album {
return &model.Album{
IDBase: model.IDBase{
ID: id,
},
}
}
func TestFolderStack(t *testing.T) { func TestFolderStack(t *testing.T) {
sta := &Stack{} sta := &Stack{}
sta.Push(egAlbum(3)) sta.Push(&model.Album{ID: 3})
sta.Push(egAlbum(4)) sta.Push(&model.Album{ID: 4})
sta.Push(egAlbum(5)) sta.Push(&model.Album{ID: 5})
sta.Push(egAlbum(6)) sta.Push(&model.Album{ID: 6})
expected := "[6, 5, 4, 3, ]" expected := "[6, 5, 4, 3, ]"
actual := sta.String() actual := sta.String()
if expected != actual { if expected != actual {
@@ -28,12 +20,12 @@ func TestFolderStack(t *testing.T) {
} }
// //
sta = &Stack{} sta = &Stack{}
sta.Push(egAlbum(27)) sta.Push(&model.Album{ID: 27})
sta.Push(egAlbum(4)) sta.Push(&model.Album{ID: 4})
sta.Peek() sta.Peek()
sta.Push(egAlbum(5)) sta.Push(&model.Album{ID: 5})
sta.Push(egAlbum(6)) sta.Push(&model.Album{ID: 6})
sta.Push(egAlbum(7)) sta.Push(&model.Album{ID: 7})
sta.Pop() sta.Pop()
expected = "[6, 5, 4, 27, ]" expected = "[6, 5, 4, 27, ]"
actual = sta.String() actual = sta.String()

View File

@@ -9,7 +9,7 @@ import (
func newAlbumByTags(a *model.Album, artist *model.Artist) *subsonic.Album { func newAlbumByTags(a *model.Album, artist *model.Artist) *subsonic.Album {
ret := &subsonic.Album{ ret := &subsonic.Album{
Created: a.CreatedAt, Created: a.ModifiedAt,
ID: a.ID, ID: a.ID,
Name: a.TagTitle, Name: a.TagTitle,
} }

View File

@@ -124,7 +124,7 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
user.ID) user.ID)
q = q.Order("plays.count DESC") q = q.Order("plays.count DESC")
case "newest": case "newest":
q = q.Order("updated_at DESC") q = q.Order("modified_at DESC")
case "random": case "random":
q = q.Order(gorm.Expr("random()")) q = q.Order(gorm.Expr("random()"))
case "recent": case "recent":

View File

@@ -126,7 +126,7 @@ func (c *Controller) GetAlbumListTwo(w http.ResponseWriter, r *http.Request) {
user.ID) user.ID)
q = q.Order("plays.count DESC") q = q.Order("plays.count DESC")
case "newest": case "newest":
q = q.Order("updated_at DESC") q = q.Order("modified_at DESC")
case "random": case "random":
q = q.Order(gorm.Expr("random()")) q = q.Order(gorm.Expr("random()"))
case "recent": case "recent":