SocialDragon/database/main.go
Andreas Mieke be67ae464c Add folder support
This adds folder support as image source, the folder must be specified
in the config file as "Folder" under group "Folder". Parsed images will
be deleted from the folder!
2017-05-13 23:29:56 +02:00

78 lines
1.2 KiB
Go

package database
import (
"log"
"time"
"git.1750studios.com/AniNite/SocialDragon/config"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
)
type Service uint
const (
Snapchat Service = iota
Twitter
Instagram
Folder
)
type State uint
const (
Inbox State = iota
Approved
Rejected
)
type updatecallback func(Item)
type Item struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
UserID uint `sql:"index"`
Service Service
State State
IsVideo bool
Path string
OriginalID string
}
type User struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
Name string
DisplayName string
Service Service
Blocked bool
}
var Db *gorm.DB
var cb updatecallback
func InitDb(callback updatecallback) {
cb = callback
var err error
Db, err = gorm.Open("postgres", config.C.DatabaseConnection)
if err != nil {
log.Fatalf("Database error: %+v", err)
}
Db.LogMode(false)
Db.SingularTable(true)
Db.Model(&Item{}).AddForeignKey("user_id", "\"user\"(id)", "RESTRICT", "RESTRICT")
Db.AutoMigrate(&Item{}, &User{})
}
func (IT *Item) AfterSave(scope *gorm.Scope) (err error) {
go cb(*IT)
return
}