2017-01-17 22:38:35 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
type State uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
Inbox State = iota
|
|
|
|
Approved
|
|
|
|
Rejected
|
|
|
|
)
|
|
|
|
|
2017-01-19 18:49:53 +00:00
|
|
|
type updatecallback func(Item)
|
|
|
|
|
2017-01-17 22:38:35 +00:00
|
|
|
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
|
2017-01-19 18:49:53 +00:00
|
|
|
var cb updatecallback
|
2017-01-17 22:38:35 +00:00
|
|
|
|
2017-01-19 18:49:53 +00:00
|
|
|
func InitDb(callback updatecallback) {
|
|
|
|
cb = callback
|
2017-01-17 22:38:35 +00:00
|
|
|
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{})
|
|
|
|
}
|
2017-01-19 18:49:53 +00:00
|
|
|
|
|
|
|
func (IT *Item) AfterSave(scope *gorm.Scope) (err error) {
|
|
|
|
go cb(*IT)
|
|
|
|
return
|
|
|
|
}
|