Newer
Older
"github.com/jinzhu/gorm"
// Needed for sqlite
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// DataHandler init a gorm DB an presents API handlers
type DataHandler struct {
db *gorm.DB
}
// ErrorIDDoesNotExist = "id does not exist"
const ErrorIDDoesNotExist = "id does not exist"
// ErrorIDIsMissing = "id is missing"
const ErrorIDIsMissing = "id is missing"
// ErrorCannotAccessRessource = "You can not access this ressource"
const ErrorCannotAccessRessource = "You can not access this ressource"
// ErrorRoleOfLoggedUser = "Could not get role of logged user"
const ErrorRoleOfLoggedUser = "Could not get role of logged user"
// ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
const ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
type election struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
name string
ballotType string
areas []area
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
}
type area struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
electionID uint
name string
seatNumber uint
mapID string
sections []section
}
type section struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
areaID uint
name string
seatNumber uint
mapID string
desks []desk
}
type desk struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
sectionID uint
name string
subscribed uint
witnessDesk bool
}
type party struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
name string
color string
candidateList []candidateList
}
type capturer struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
userID int `gorm:"not null;unique"`
name string
deskRounds []deskRound
}
type parameter struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
countBalnkAndNull bool
showOnlyCompleted bool
showMap bool
}
type round struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
parameter parameter
name string
date time.Time
tour uint
deskRounds []deskRound
candidateLists []candidateList
}
type deskRound struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
completed bool
dateCompletion time.Time
validated bool
}
type candidateList struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
partyID uint
roundID uint
area area `gorm:"foreignkey:AreaRefer"`
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
}
type candidate struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
candidateListID uint
fullName string
rank uint
communityCounseller bool
birthdate time.Time
potentialIncompatibility bool
refused bool
removed bool
}
type vote struct {
deskRoundID uint `gorm:"primary_key"`
candidateListID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
voiceNumber uint
blank bool
null bool
}
// NewDataHandler init a DataHandler and returns a pointer to it
func NewDataHandler() *DataHandler {
db, err := gorm.Open("sqlite3", "./data/test.db")
if err != nil {
panic("failed to connect database")
}
db.LogMode(true)
// Migrate the schema
db.AutoMigrate(&capturer{})
db.AutoMigrate(&election{})
db.AutoMigrate(&area{})
db.AutoMigrate(§ion{})
db.AutoMigrate(&desk{})
db.AutoMigrate(&round{})
db.AutoMigrate(&deskRound{})
db.AutoMigrate(&party{})
db.AutoMigrate(&candidateList{})
db.AutoMigrate(&candidate{})
db.AutoMigrate(&vote{})
db.AutoMigrate(¶meter{})
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
return &DataHandler{db: db}
}
// ProcessAPI redirect API call to DataHandler to each correct API func
func (d *DataHandler) ProcessAPI(w http.ResponseWriter, r *http.Request) {
api := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/"), "/")[0]
switch api {
}
}
func (d *DataHandler) getLoggedUser(w http.ResponseWriter, r *http.Request) interface{} {
// user := auth.GetLoggedUserTechnical(w, r)
// if user.Role != "" && (user.Role == "BANKER") {
// var o UserBanker
// if err := d.db.Where(reqUserID, user.ID).First(&o).Error; err != nil {
// o := UserBanker{UserID: user.ID, Name: user.Login}
// d.db.Create(&o)
// d.db.Where(reqUserID, user.ID).First(&o)
// return o
// }
// return o
// } else if user.Role != "" && (user.Role == "CLIENT") {
// var o UserClient
// if err := d.db.Where(reqUserID, user.ID).First(&o).Error; err != nil {
// o := UserClient{UserID: user.ID, Name: user.Login}
// d.db.Create(&o)
// d.db.Where(reqUserID, user.ID).First(&o)
// return o
// }
// return o
// }
return nil
}