package controllers import ( "github.com/jinzhu/gorm" // _ "github.com/mattn/go-sqlite3" "database/sql" "github.com/revel/revel" "time" ) // Website user type User struct { Id int64 Name string Email string Password string // Domains []Domain CreatedAt time.Time UpdatedAt time.Time DeletedAt time.Time } /* type Domain struct { Id int64 UserId int64 Domain string CreatedAt time.Time UpdatedAt time.Time DeletedAt time.Time } */ // Domains for which we do E-Mail hosting type VirtualDomain struct { Id int64 User User UserId int64 Name string } // User for postfix type VirtualUser struct { Id int64 UserId int64 VirtualDomain VirtualDomain VirtualDomainId int64 Password string Email string } // E-Mail aliase for postfix type VirtualAliase struct { Id int64 VirtualDomain VirtualDomain VirtualDomainId int64 Source string Destination string } type GormController struct { *revel.Controller Gdb *gorm.DB } var Gdb gorm.DB func InitDB() { // db.Init() Gdb, err := gorm.Open(revel.Config.StringDefault("db.driver", "sqlite3"), revel.Config.StringDefault("db.spec", "email.db")) if err != nil { revel.ERROR.Printf("%s", err) } Gdb.LogMode(true) Gdb.SetLogger(gorm.Logger{revel.TRACE}) user := User{} Gdb.AutoMigrate(&user) Gdb.Model(&user).AddUniqueIndex("idx_user_name", "name") Gdb.Model(&user).AddUniqueIndex("idx_user_email", "email") /* domain := Domain{} Gdb.AutoMigrate(&domain) Gdb.Model(&domain).AddUniqueIndex("idx_domain_name", "domain") */ vu := VirtualUser{} Gdb.AutoMigrate(&vu) Gdb.Model(&vu).AddUniqueIndex("idx_virtual_user_email", "email") Gdb.Debug().Model(&vu).Related(&user) Gdb.Debug().Model(&user).Related(&vu) vd := VirtualDomain{} Gdb.AutoMigrate(&vd) va := VirtualAliase{} Gdb.AutoMigrate(&va) } // transactions // This method fills the c.Gdb before each transaction func (c *GormController) Begin() revel.Result { txn := Gdb.Begin() if txn.Error != nil { //panic(txn.Error) revel.ERROR.Printf("%s \n", txn.Error) } c.Gdb = txn return nil } // This method clears the c.Gdb after each transaction func (c *GormController) Commit() revel.Result { if c.Gdb == nil { return nil } c.Gdb.Commit() if err := c.Gdb.Error; err != nil && err != sql.ErrTxDone { panic(err) } c.Gdb = nil return nil } // This method clears the c.Gdb after each transaction, too func (c *GormController) Rollback() revel.Result { if c.Gdb == nil { return nil } c.Gdb.Rollback() if err := c.Gdb.Error; err != nil && err != sql.ErrTxDone { panic(err) } c.Gdb = nil return nil }