This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/query/query.go

148 lines
4.0 KiB
Go
Raw Normal View History

2021-01-12 04:48:32 +00:00
package query
2021-01-06 19:36:09 +00:00
import (
2021-01-08 02:45:59 +00:00
"path"
"gorm.io/gorm"
2021-01-10 00:44:02 +00:00
"gorm.io/gorm/logger"
2021-01-08 02:45:59 +00:00
"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
"reichard.io/imagini/internal/config"
2021-01-10 00:44:02 +00:00
"reichard.io/imagini/internal/models"
2021-01-06 19:36:09 +00:00
)
2021-01-12 04:48:32 +00:00
func NewDB(c *config.Config) *gorm.DB {
2021-01-10 00:44:02 +00:00
gormConfig := &gorm.Config{
PrepareStmt: true,
Logger: logger.Default.LogMode(logger.Silent),
}
2021-01-12 04:48:32 +00:00
var db *gorm.DB
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-10 00:44:02 +00:00
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-10 00:44:02 +00:00
db.AutoMigrate(&models.ServerSetting{})
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.MediaItem{})
db.AutoMigrate(&models.Tag{})
db.AutoMigrate(&models.Album{})
2021-01-12 22:06:27 +00:00
// Determine whether to bootstrap
var count int64
db.Model(&models.User{}).Count(&count)
if count == 0 {
bootstrapDatabase(db)
}
2021-01-12 04:48:32 +00:00
return db
2021-01-06 19:36:09 +00:00
}
2021-01-12 22:06:27 +00:00
func bootstrapDatabase(db *gorm.DB) {
log.Info("[query] Bootstrapping database.")
err := CreateUser(db, models.User{
Username: "admin",
Password: "admin",
AuthType: "Local",
})
if err != nil {
log.Fatal("[query] Unable to bootstrap database.")
}
}
2021-01-12 04:48:32 +00:00
func ItemsFromAlbum(db *gorm.DB, 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).
db.
//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
}
2021-01-08 02:45:59 +00:00
// func ItemsFromTags(userID int, tagID int) []MediaItem {
// return nil
// }
//
// func IndexMediaItems(newItems []MediaItem) {
// }
2021-01-10 00:44:02 +00:00
// func PopulateTestData() {
// user1 := User{Username: "Evan", Email: "evan@reichard.io", FirstName: "Evan", LastName: "Reichard", AuthType: "LDAP", Salt: "1234", HashedPWSalt: "1234"}
// user2 := User{Username: "Ryan", Email: "ryan@example.com", FirstName: "Ryan", LastName: "Dunfrey", AuthType: "Local", Salt: "2345", HashedPWSalt: "2345"}
// user3 := User{Username: "Bill", Email: "bill@example.com", FirstName: "Bill", LastName: "Smith", AuthType: "LDAP", Salt: "3456", HashedPWSalt: "3456"}
//
// mi1 := MediaItem{
// User: user1,
// EXIFDate: time.Now(),
// Latitude: "1234",
// Longitude: "1234",
// RelPath: "./1234.jpg",
// Tags: []Tag{
// {Name: "Tag1"},
// {Name: "Tag2"},
// },
// Albums: []Album{
// {Name: "Album1"},
// {Name: "Album2"},
// },
// }
//
// mi2 := MediaItem{
// User: user2,
// EXIFDate: time.Now(),
// Latitude: "1234",
// Longitude: "1234",
// RelPath: "./1234.jpg",
// Tags: []Tag{
// {Name: "Tag3"},
// {Name: "Tag4"},
// },
// Albums: []Album{
// {Name: "Album3"},
// {Name: "Album4"},
// },
// }
//
// mi3 := MediaItem{
// User: user3,
// EXIFDate: time.Now(),
// Latitude: "1234",
// Longitude: "1234",
// RelPath: "./1234.jpg",
// Tags: []Tag{
// {Name: "Tag4"},
// {Name: "Tag5"},
// },
// Albums: []Album{
// {Name: "Album1"},
// {Name: "Album7"},
// },
// }
//
// // db.Create(&user1)
// // db.Create(&user2)
// // db.Create(&user3)
// db.Create(&mi1)
// db.Create(&mi2)
// db.Create(&mi3)
// }