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."
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
139
140
141
142
143
144
145
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
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
Rounds []Round
}
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:"-"`
roundID round
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:"-"`
ElectionID uint
parameter parameter
name string
date time.Time
tour uint
deskRounds []deskRound
}
type deskRound struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
RoundID uint
completed bool
dateCompletion time.Time
validated bool
Votes []Vote
}
type candidateList struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
PartyID uint
name string
candidates []candidate
Votes []Vote
}
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
}
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
// 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)
// Add FK with SQLite doesn't work
// db.Model(&Operation{}).AddForeignKey("creditor", "bank_account(id)", "Set Null", "Set Null")
// Migrate the schema
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
}