Replace database, initial setup now works
This commit is contained in:
parent
e42ac2436b
commit
31da5e1c07
|
@ -9,12 +9,12 @@ import (
|
|||
|
||||
type Album struct {
|
||||
Model
|
||||
Title string
|
||||
Title string `gorm:"not null"`
|
||||
ParentAlbumID *int
|
||||
ParentAlbum *Album
|
||||
OwnerID int
|
||||
OwnerID int `gorm:"not null"`
|
||||
Owner User
|
||||
Path string
|
||||
Path string `gorm:"not null"`
|
||||
PathHash string `gorm:"unique"`
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,11 @@ import (
|
|||
)
|
||||
|
||||
type Model struct {
|
||||
ID int `gorm:"primarykey"`
|
||||
ID int `gorm:"primarykey"`
|
||||
ModelTimestamps
|
||||
}
|
||||
|
||||
type ModelTimestamps struct {
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
|
|
|
@ -10,18 +10,18 @@ import (
|
|||
|
||||
type Media struct {
|
||||
Model
|
||||
Title string
|
||||
Path string
|
||||
PathHash string
|
||||
AlbumID int
|
||||
Title string `gorm:"not null"`
|
||||
Path string `gorm:"not null"`
|
||||
PathHash string `gorm:"not null"`
|
||||
AlbumID int `gorm:"not null"`
|
||||
Album Album
|
||||
ExifID *int
|
||||
Exif *MediaEXIF
|
||||
MediaURL []MediaURL
|
||||
DateShot time.Time
|
||||
DateImported time.Time
|
||||
Favorite bool
|
||||
Type MediaType
|
||||
DateShot time.Time `gorm:"not null"`
|
||||
DateImported time.Time `gorm:"not null"`
|
||||
Favorite bool `gorm:"not null, default:false"`
|
||||
Type MediaType `gorm:"not null"`
|
||||
VideoMetadataID *int
|
||||
VideoMetadata *VideoMetadata
|
||||
SideCarPath *string
|
||||
|
@ -44,14 +44,14 @@ const (
|
|||
|
||||
type MediaURL struct {
|
||||
Model
|
||||
MediaID int
|
||||
MediaID int `gorm:"not null"`
|
||||
Media Media
|
||||
MediaName string
|
||||
Width int
|
||||
Height int
|
||||
Purpose MediaPurpose
|
||||
ContentType string
|
||||
FileSize int64
|
||||
MediaName string `gorm:"not null"`
|
||||
Width int `gorm:"not null"`
|
||||
Height int `gorm:"not null"`
|
||||
Purpose MediaPurpose `gorm:"not null"`
|
||||
ContentType string `gorm:"not null"`
|
||||
FileSize int64 `gorm:"not null"`
|
||||
}
|
||||
|
||||
func (p *MediaURL) URL() string {
|
||||
|
|
|
@ -21,6 +21,10 @@ type MediaEXIF struct {
|
|||
GPSLonitude *float64
|
||||
}
|
||||
|
||||
func (MediaEXIF) TableName() string {
|
||||
return "media_exif"
|
||||
}
|
||||
|
||||
func (exif *MediaEXIF) Media() *Media {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
|
|
@ -6,13 +6,13 @@ import (
|
|||
|
||||
type ShareToken struct {
|
||||
Model
|
||||
Value string
|
||||
OwnerID int
|
||||
Value string `gorm:"not null"`
|
||||
OwnerID int `gorm:"not null"`
|
||||
Owner User
|
||||
Expire *time.Time
|
||||
Password *string
|
||||
AlbumID *int
|
||||
Album Album
|
||||
Album *Album
|
||||
MediaID *int
|
||||
Media *Media
|
||||
}
|
||||
|
|
|
@ -6,10 +6,13 @@ import (
|
|||
)
|
||||
|
||||
type SiteInfo struct {
|
||||
Model
|
||||
InitialSetup bool
|
||||
PeriodicScanInterval int
|
||||
ConcurrentWorkers int
|
||||
InitialSetup bool `gorm:"not null"`
|
||||
PeriodicScanInterval int `gorm:"not null"`
|
||||
ConcurrentWorkers int `gorm:"not null"`
|
||||
}
|
||||
|
||||
func (SiteInfo) TableName() string {
|
||||
return "site_info"
|
||||
}
|
||||
|
||||
// GetSiteInfo gets the site info row from the database, and creates it if it does not exist
|
||||
|
|
|
@ -26,10 +26,10 @@ type User struct {
|
|||
|
||||
type AccessToken struct {
|
||||
Model
|
||||
UserID int
|
||||
User User `gorm:"constraint:OnDelete:CASCADE;"`
|
||||
Value string `gorm:"size:24`
|
||||
Expire time.Time
|
||||
UserID int `gorm:"not null"`
|
||||
User User `gorm:"constraint:OnDelete:CASCADE;"`
|
||||
Value string `gorm:"not null, size:24`
|
||||
Expire time.Time `gorm:"not null"`
|
||||
}
|
||||
|
||||
var ErrorInvalidUserCredentials = errors.New("invalid credentials")
|
||||
|
@ -163,12 +163,10 @@ func (user *User) GenerateAccessToken(db *gorm.DB) (*AccessToken, error) {
|
|||
|
||||
func VerifyTokenAndGetUser(db *gorm.DB, token string) (*User, error) {
|
||||
|
||||
now := time.Now().UTC().Format("2006-01-02 15:04:05")
|
||||
|
||||
// row := database.QueryRow("SELECT (user_id) FROM access_token WHERE expire > ? AND value = ?", now, token)
|
||||
|
||||
var accessToken AccessToken
|
||||
result := db.Where("expire > ? AND value = ?", now, token).First(&accessToken)
|
||||
result := db.Where("expire > ? AND value = ?", time.Now(), token).First(&accessToken)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@ package models
|
|||
|
||||
type VideoMetadata struct {
|
||||
Model
|
||||
Width int
|
||||
Height int
|
||||
Duration float64
|
||||
Width int `gorm:"not null"`
|
||||
Height int `gorm:"not null"`
|
||||
Duration float64 `gorm:"not null"`
|
||||
Codec *string
|
||||
Framerate *float64
|
||||
Bitrate *string
|
||||
|
|
|
@ -23,7 +23,7 @@ func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter, onl
|
|||
}
|
||||
|
||||
if showEmpty == nil || *showEmpty == false {
|
||||
subQuery := r.Database.Model(&models.Media{}).Where("album_id = album.album_id")
|
||||
subQuery := r.Database.Model(&models.Media{}).Where("album_id = albums.id")
|
||||
|
||||
if onlyWithFavorites != nil && *onlyWithFavorites == true {
|
||||
subQuery = subQuery.Where("favorite = 1")
|
||||
|
@ -35,7 +35,7 @@ func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter, onl
|
|||
// TODO: Incorporate models.FormatSQL
|
||||
|
||||
var albums []*models.Album
|
||||
if err := query.Find(albums).Error; err != nil {
|
||||
if err := query.Find(&albums).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -118,7 +118,7 @@ func (r *albumResolver) Thumbnail(ctx context.Context, obj *models.Album) (*mode
|
|||
func (r *albumResolver) SubAlbums(ctx context.Context, parent *models.Album, filter *models.Filter) ([]*models.Album, error) {
|
||||
|
||||
var albums []*models.Album
|
||||
if err := r.Database.Where("parent_album = ?", parent.ID).Find(albums).Error; err != nil {
|
||||
if err := r.Database.Where("parent_album = ?", parent.ID).Find(&albums).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -138,7 +138,7 @@ func (r *albumResolver) Owner(ctx context.Context, obj *models.Album) (*models.U
|
|||
func (r *albumResolver) Shares(ctx context.Context, album *models.Album) ([]*models.ShareToken, error) {
|
||||
|
||||
var shareTokens []*models.ShareToken
|
||||
if err := r.Database.Where("album_id = ?", album.ID).Find(shareTokens).Error; err != nil {
|
||||
if err := r.Database.Where("album_id = ?", album.ID).Find(&shareTokens).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -105,7 +105,7 @@ func (r *Resolver) Media() api.MediaResolver {
|
|||
|
||||
func (r *mediaResolver) Shares(ctx context.Context, media *models.Media) ([]*models.ShareToken, error) {
|
||||
var shareTokens []*models.ShareToken
|
||||
if err := r.Database.Where("media_id = ?", media.ID).Find(shareTokens).Error; err != nil {
|
||||
if err := r.Database.Where("media_id = ?", media.ID).Find(&shareTokens).Error; err != nil {
|
||||
return nil, errors.Wrapf(err, "get shares for media (%s)", media.Path)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue