SocialDragon/database/main.go

77 lines
1.2 KiB
Go
Raw Normal View History

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
)
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
}