/
model.go
203 lines (179 loc) · 4.03 KB
/
model.go
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
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
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
package volunteer
// swagger:model Volunteer
type Volunteer struct {
// Id of the volunteer
// in: int64
// read only: true
Id int `json:"id"`
// Firstname of the volunteer
// in: string
// required: true
// example: Ash
Firstname string `json:"firstname"`
// Lastname of the volunteer
// in: string
// required: true
// example: Ketchum
Lastname string `json:"lastname"`
// Email of the volunteer
// in: string
// required: true
// example: ash.ketchum@indigo.kt
Email string `json:"email"`
// Discord pseudo
// in: string
// required: true
// example: TheVeryBest
Discord string `json:"discord"`
// Phone number
// in: string
// required: true
// example: 0123456789
Phone string `json:"phone"`
// University name
// in: string
// required: true
// example: Indigo
University string `json:"university"`
// Postal address
// in: string
// required: true
// example: 01 bis house, 00001 Pallet Town, Kanto
PostalAddress string `json:"postal_address"`
// Status of the volunteer
// in: bool
// required: true
Actif bool `json:"actif"`
// Does the volunteer has a bureau role
// in: bool
// required: true
// example: false
Bureau bool `json:"bureau"`
// Status of the volunteer
// in: string
// required: true
// example: volunteer
HrStatus string `json:"hr_status"`
// Started of date of volunteering
// in: string
// required: true
// example: 1997-04-01
StartedDate string `json:"started_date"`
}
type Volunteers []Volunteer
func NewVolunteer(vlt *Volunteer) {
stmt, _ := TheDb().Prepare(
`INSERT INTO ` + db_name + `
(firstname, lastname, email, discord, phone, university, postal_address, actif, bureau,
hr_status, started_date)
VALUES (?,?,?,?,?,?,?,?,?,?,?);`,
)
_, err := stmt.Exec(
vlt.Firstname,
vlt.Lastname,
vlt.Email,
vlt.Discord,
vlt.Phone,
vlt.University,
vlt.PostalAddress,
vlt.Actif,
vlt.Bureau,
vlt.HrStatus,
vlt.StartedDate,
)
if err != nil {
TheLogger().LogError(log_name, "can't create new volunteer.", err)
}
}
func FindVolunteerById(id int) *Volunteer {
var vlt Volunteer
row := TheDb().QueryRow("SELECT * FROM "+db_name+" WHERE id = ?;", id)
err := row.Scan(
&vlt.Id,
&vlt.Firstname,
&vlt.Lastname,
&vlt.Email,
&vlt.Discord,
&vlt.Phone,
&vlt.University,
&vlt.PostalAddress,
&vlt.Actif,
&vlt.Bureau,
&vlt.HrStatus,
&vlt.StartedDate,
)
if err != nil {
TheLogger().LogWarning(log_name, "volunteer id not found.", err)
}
return &vlt
}
func AllVolunteers() *Volunteers {
var vlts Volunteers
rows, err := TheDb().Query("SELECT * FROM " + db_name)
if err != nil {
TheLogger().LogError(log_name, "problem with the db.", err)
}
// Close rows after all readed
defer rows.Close()
for rows.Next() {
var vlt Volunteer
err := rows.Scan(
&vlt.Id,
&vlt.Firstname,
&vlt.Lastname,
&vlt.Email,
&vlt.Discord,
&vlt.Phone,
&vlt.University,
&vlt.PostalAddress,
&vlt.Actif,
&vlt.Bureau,
&vlt.HrStatus,
&vlt.StartedDate,
)
if err != nil {
TheLogger().LogError(log_name, "volunteers not found.", err)
}
vlts = append(vlts, vlt)
}
return &vlts
}
func UpdateVolunteer(vlt *Volunteer) {
stmt, err := TheDb().Prepare(
`UPDATE ` + db_name + ` SET
firstname=?, lastname=?, email=?, discord=?, phone=?, university=?, postal_address=?,
actif=?, bureau=?, hr_status=?, started_date=?
WHERE id=?;`,
)
if err != nil {
TheLogger().LogError(log_name, "problem with the db.", err)
}
_, err = stmt.Exec(
vlt.Firstname,
vlt.Lastname,
vlt.Email,
vlt.Discord,
vlt.Phone,
vlt.University,
vlt.PostalAddress,
vlt.Actif,
vlt.Bureau,
vlt.HrStatus,
vlt.StartedDate,
vlt.Id,
)
if err != nil {
TheLogger().LogError(log_name, "volunteer can't be updated.", err)
}
}
func DeleteVolunteerById(id int) error {
stmt, err := TheDb().Prepare("DELETE FROM " + db_name + " WHERE id=?;")
if err != nil {
TheLogger().LogError(log_name, "problem with the db.", err)
}
_, err = stmt.Exec(id)
if err != nil {
TheLogger().LogError(log_name, "volunteer can't be deleted.", err)
}
return err
}