blob: 5b7dd9719f8e910e2f91f010aad2800c9ac37ad6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
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
}
|