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

View File

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

View File

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

View File

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

View File

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

View File

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