2021-01-16 22:00:17 +00:00
|
|
|
package db
|
2021-01-06 19:36:09 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-18 21:24:28 +00:00
|
|
|
"path"
|
2021-01-08 02:45:59 +00:00
|
|
|
|
2021-01-18 21:24:28 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
// "gorm.io/gorm/logger"
|
|
|
|
"gorm.io/driver/sqlite"
|
2021-01-12 22:06:27 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-01-08 02:45:59 +00:00
|
|
|
|
2021-01-18 21:24:28 +00:00
|
|
|
"reichard.io/imagini/internal/config"
|
|
|
|
"reichard.io/imagini/internal/models"
|
2021-01-06 19:36:09 +00:00
|
|
|
)
|
|
|
|
|
2021-01-16 22:00:17 +00:00
|
|
|
type DBManager struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMgr(c *config.Config) *DBManager {
|
2021-01-10 00:44:02 +00:00
|
|
|
gormConfig := &gorm.Config{
|
|
|
|
PrepareStmt: true,
|
2021-01-18 21:16:52 +00:00
|
|
|
// Logger: logger.Default.LogMode(logger.Silent),
|
2021-01-10 00:44:02 +00:00
|
|
|
}
|
2021-01-16 22:00:17 +00:00
|
|
|
|
|
|
|
// Create manager
|
|
|
|
dbm := &DBManager{}
|
2021-01-06 19:36:09 +00:00
|
|
|
|
2021-01-08 02:45:59 +00:00
|
|
|
if c.DBType == "SQLite" {
|
|
|
|
dbLocation := path.Join(c.ConfigPath, "imagini.db")
|
2021-01-16 22:00:17 +00:00
|
|
|
dbm.db, _ = gorm.Open(sqlite.Open(dbLocation), gormConfig)
|
2021-01-08 02:45:59 +00:00
|
|
|
} else {
|
2021-01-12 22:06:27 +00:00
|
|
|
log.Fatal("Unsupported Database")
|
2021-01-08 02:45:59 +00:00
|
|
|
}
|
2021-01-06 19:36:09 +00:00
|
|
|
|
2021-01-12 22:06:27 +00:00
|
|
|
// Initialize database
|
2021-01-16 22:00:17 +00:00
|
|
|
dbm.db.AutoMigrate(&models.ServerSetting{})
|
2021-01-18 04:56:56 +00:00
|
|
|
dbm.db.AutoMigrate(&models.Device{})
|
2021-01-16 22:00:17 +00:00
|
|
|
dbm.db.AutoMigrate(&models.User{})
|
|
|
|
dbm.db.AutoMigrate(&models.MediaItem{})
|
|
|
|
dbm.db.AutoMigrate(&models.Tag{})
|
|
|
|
dbm.db.AutoMigrate(&models.Album{})
|
2021-01-12 22:06:27 +00:00
|
|
|
|
|
|
|
// Determine whether to bootstrap
|
|
|
|
var count int64
|
2021-01-16 22:00:17 +00:00
|
|
|
dbm.db.Model(&models.User{}).Count(&count)
|
2021-01-12 22:06:27 +00:00
|
|
|
if count == 0 {
|
2021-01-16 22:00:17 +00:00
|
|
|
dbm.bootstrapDatabase()
|
2021-01-12 22:06:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 22:00:17 +00:00
|
|
|
return dbm
|
2021-01-06 19:36:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 22:00:17 +00:00
|
|
|
func (dbm *DBManager) bootstrapDatabase() {
|
2021-01-12 22:06:27 +00:00
|
|
|
log.Info("[query] Bootstrapping database.")
|
2021-01-18 21:16:52 +00:00
|
|
|
_, err := dbm.CreateUser(models.User{
|
2021-01-12 22:06:27 +00:00
|
|
|
Username: "admin",
|
|
|
|
Password: "admin",
|
|
|
|
AuthType: "Local",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("[query] Unable to bootstrap database.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 22:00:17 +00:00
|
|
|
func (dbm *DBManager) ItemsFromAlbum(user models.User, album models.Album) []models.MediaItem {
|
2021-01-10 00:44:02 +00:00
|
|
|
var mediaItems []models.MediaItem
|
2021-01-08 02:45:59 +00:00
|
|
|
// db.Table("media_albums").
|
|
|
|
// Select("media_item.*").
|
|
|
|
// Joins("INNER JOIN media_items ON media_albums.ID = media_items.Albums").
|
|
|
|
// Where("media_albums.album_id = ? AND media_items.User = ?", albumID, userID).
|
|
|
|
|
|
|
|
|
2021-01-16 22:00:17 +00:00
|
|
|
dbm.db.
|
2021-01-08 02:45:59 +00:00
|
|
|
//Where("album = ? AND user = ?", albumID, userID).
|
|
|
|
Find(&mediaItems)
|
|
|
|
return mediaItems
|
|
|
|
|
|
|
|
// db.Raw(`
|
|
|
|
// SELECT
|
|
|
|
// MediaItems.*
|
|
|
|
// FROM
|
|
|
|
// MediaAlbums
|
|
|
|
// INNER JOIN MediaItems ON MediaAlbums.mediaID = MediaItems.mediaID
|
|
|
|
// WHERE MediaAlbums.albumID = ? AND MediaItems.userID = ?`, albumID, userID)
|
2021-01-06 19:36:09 +00:00
|
|
|
}
|