2021-01-06 19:36:09 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2021-01-08 02:45:59 +00:00
|
|
|
"log"
|
|
|
|
"path"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
|
|
|
|
"reichard.io/imagini/internal/config"
|
2021-01-06 19:36:09 +00:00
|
|
|
)
|
|
|
|
|
2021-01-08 02:45:59 +00:00
|
|
|
var db *gorm.DB
|
2021-01-06 19:36:09 +00:00
|
|
|
|
2021-01-08 02:45:59 +00:00
|
|
|
func ConnectDB(c *config.Config) {
|
|
|
|
fmt.Printf("%+v\n", c)
|
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")
|
|
|
|
db, _ = gorm.Open(sqlite.Open(dbLocation), &gorm.Config{
|
|
|
|
PrepareStmt: true,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log.Fatal("ERROR: Unsupported Database")
|
|
|
|
}
|
2021-01-06 19:36:09 +00:00
|
|
|
|
2021-01-08 02:45:59 +00:00
|
|
|
// Initialize Database
|
|
|
|
db.AutoMigrate(&ServerSetting{})
|
|
|
|
db.AutoMigrate(&User{})
|
|
|
|
db.AutoMigrate(&MediaItem{})
|
|
|
|
db.AutoMigrate(&Tag{})
|
|
|
|
db.AutoMigrate(&Album{})
|
2021-01-06 19:36:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 02:45:59 +00:00
|
|
|
func ItemsFromAlbum(user User, album Album) []MediaItem {
|
|
|
|
var mediaItems []MediaItem
|
|
|
|
// 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) {
|
|
|
|
// }
|
|
|
|
|
|
|
|
func PopulateTestData() {
|
|
|
|
user1 := User{Name: "Evan", Email: "evan@reichard.io", AuthType: "LDAP", Salt: "1234", HashedPWSalt: "1234"}
|
|
|
|
user2 := User{Name: "Ryan", Email: "ryan@example.com", AuthType: "Local", Salt: "2345", HashedPWSalt: "2345"}
|
|
|
|
user3 := User{Name: "Bill", Email: "bill@example.com", 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)
|
2021-01-06 19:36:09 +00:00
|
|
|
}
|