1
Fork 0

Replace database, initial setup now works

This commit is contained in:
viktorstrate 2020-11-28 21:49:33 +01:00
parent e42ac2436b
commit 31da5e1c07
No known key found for this signature in database
GPG Key ID: 3F855605109C1E8A
10 changed files with 50 additions and 41 deletions

View File

@ -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"`
}

View File

@ -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"`

View File

@ -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 {

View File

@ -21,6 +21,10 @@ type MediaEXIF struct {
GPSLonitude *float64
}
func (MediaEXIF) TableName() string {
return "media_exif"
}
func (exif *MediaEXIF) Media() *Media {
panic("not implemented")
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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)
}