2021-01-16 22:00:17 +00:00
|
|
|
package db
|
2021-01-06 19:36:09 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-01 23:24:09 +00:00
|
|
|
"fmt"
|
2021-01-18 21:24:28 +00:00
|
|
|
"path"
|
2021-02-01 23:24:09 +00:00
|
|
|
"errors"
|
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"
|
2021-02-02 20:34:10 +00:00
|
|
|
"reichard.io/imagini/graph/model"
|
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-02-02 20:34:10 +00:00
|
|
|
dbm.db.AutoMigrate(&model.Device{})
|
|
|
|
dbm.db.AutoMigrate(&model.User{})
|
|
|
|
dbm.db.AutoMigrate(&model.MediaItem{})
|
|
|
|
dbm.db.AutoMigrate(&model.Tag{})
|
|
|
|
dbm.db.AutoMigrate(&model.Album{})
|
2021-01-12 22:06:27 +00:00
|
|
|
|
|
|
|
// Determine whether to bootstrap
|
|
|
|
var count int64
|
2021-02-02 20:34:10 +00:00
|
|
|
dbm.db.Model(&model.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-02-02 20:34:10 +00:00
|
|
|
|
|
|
|
password := "admin"
|
|
|
|
user := &model.User{
|
2021-01-12 22:06:27 +00:00
|
|
|
Username: "admin",
|
|
|
|
AuthType: "Local",
|
2021-02-02 20:34:10 +00:00
|
|
|
Password: &password,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := dbm.CreateUser(user)
|
2021-01-12 22:06:27 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("[query] Unable to bootstrap database.")
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 23:24:09 +00:00
|
|
|
|
2021-02-02 20:34:10 +00:00
|
|
|
// func (dmb *DBManager) {}
|
|
|
|
|
2021-02-01 23:24:09 +00:00
|
|
|
func (dbm *DBManager) QueryBuilder(dest interface{}, params []byte) (int64, error) {
|
|
|
|
// TODO:
|
|
|
|
// - Where Filters
|
|
|
|
// - Sort Filters
|
|
|
|
// - Paging Filters
|
|
|
|
|
|
|
|
objType := fmt.Sprintf("%T", dest)
|
2021-02-02 20:34:10 +00:00
|
|
|
if objType == "*[]model.MediaItem" {
|
2021-02-01 23:24:09 +00:00
|
|
|
// TODO: Validate MediaItem Type
|
|
|
|
} else {
|
|
|
|
// Return Error
|
|
|
|
return 0, errors.New("Invalid type")
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
err := dbm.db.Find(dest).Count(&count).Error;
|
|
|
|
return count, err
|
|
|
|
|
|
|
|
// Paging:
|
|
|
|
// - Regular Pagination:
|
|
|
|
// - /api/v1/MediaItems?page[limit]=50&page=2
|
|
|
|
// - Meta Count Only
|
|
|
|
// - /api/v1/MediaItems?page[limit]=0
|
|
|
|
|
|
|
|
// Sorting:
|
|
|
|
// - Ascending Sort:
|
|
|
|
// - /api/v1/MediaItems?sort=created_at
|
|
|
|
// - Descending Sort:
|
|
|
|
// - /api/v1/MediaItems?sort=-created_at
|
|
|
|
|
|
|
|
// Filters:
|
|
|
|
// - Greater Than / Less Than (created_at, updated_at, exif_date)
|
|
|
|
// - /api/v1/MediaItems?filter[created_at]>=2020-01-01&filter[created_at]<=2021-01-01
|
|
|
|
// - Long / Lat Range (latitude, longitude)
|
|
|
|
// - /api/v1/MediaItems?filter[latitude]>=71.1827&filter[latitude]<=72.0000&filter[longitude]>=100.000&filter[longitude]<=101.0000
|
|
|
|
// - Image / Video (media_type)
|
|
|
|
// - /api/v1/MediaItems?filter[media_type]=Image
|
|
|
|
// - Tags (tags)
|
|
|
|
// - /api/v1/MediaItems?filter[tags]=id1,id2,id3
|
|
|
|
// - Albums (albums)
|
|
|
|
// - /api/v1/MediaItems?filter[albums]=id1
|
|
|
|
}
|