-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
473 lines (388 loc) · 12 KB
/
main.go
File metadata and controls
473 lines (388 loc) · 12 KB
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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/joho/godotenv/autoload"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
/*-------- Models --------*/
// Participants
type Gender string
const (
MALE Gender = "MALE"
FEMALE Gender = "FEMALE"
)
type Department string
const (
Aero Department = "AERO"
Ce Department = "CE"
Civil Department = "CIVIL"
Csd Department = "CSD"
Ec Department = "EC"
Ele Department = "ELE"
Ic Department = "IC"
It Department = "IT"
Mech Department = "MEC"
Mca Department = "MCA"
)
type ShirtSize string
const (
XS ShirtSize = "XS"
S ShirtSize = "S"
M ShirtSize = "M"
L ShirtSize = "L"
XL ShirtSize = "XL"
XXL ShirtSize = "XXL"
XXXL ShirtSize = "XXXL"
)
type Batch string
const (
FY Batch = "FY"
SY Batch = "SY"
TY Batch = "TY"
LY Batch = "LY"
)
type Participant struct{
gorm.Model
Name string `json:"name" validate:"required"`
Email string `json:"email" gorm:"unique_index"`
Phone int `json:"phone" validate:"required"`
// Gender string `gorm:"type:gender" json:"gender"`
Department `json:"department" validate:"required"`
Batch `json:"batch" validate:"required"`
ShirtSize `json:"shirt_size"`
TeamID uint `json:"team_id" validate:"required"`
Team Team `json:"team"`
}
// Teams
type ProjectType string
const (
HARDWARE ProjectType = "HARDWARE"
IOT ProjectType = "IOT"
SOFTWARE ProjectType = "SOFTWARE"
)
type Team struct {
gorm.Model
Name string `json:"name" validate:"required"`
MaleCount int `json:"male_count" gorm:"default:0"`
FemaleCount int `json:"female_count" gorm:"default:0"`
ProjectType `gorm:"type:project_type" json:"project_type" validate:"required"`
LocationID uint `json:"location_id" validate:"required"`
Location Location `json:"location" validate:"required"`
Members []Participant `json:"members" validate:"required"`
}
// Locations
type Wing string
const (
CEF Wing = "CEF"
CES Wing = "CES"
IT Wing = "IT"
EC Wing = "EC"
MCA Wing ="MCA"
ARCH Wing = "ARCH"
CIVIL Wing = "CIVIL"
)
type Location struct {
gorm.Model
Name string `json:"name" validate:"required"`
Wing `json:"wing" validate:"required"`
Capacity int `json:"capacity" validate:"required"` // in terms of teams
Teams []Team `json:"teams" validate:"required"`
}
// Attendance
type Attendance struct {
gorm.Model
ActionID uint `json:"action_id" validate:"required"`
Action Action `json:"action" validate:"required"`
ParticipantID uint `json:"participant_id" validate:"required"`
Participant Participant `json:"participant" validate:"required"`
}
// Actions
type Action struct {
gorm.Model
Title string `json:"title" validate:"required"`
Valid bool `json:"valid" gorm:"default:true"`
Attendance []Attendance
}
var db *gorm.DB
var err error
func main() {
// Loading enviroment variables
host := os.Getenv("DB_HOST")
dbport := os.Getenv("DB_PORT")
user := os.Getenv("DB_USER")
dbname := os.Getenv("DB_NAME")
dbpassword := os.Getenv("DB_PASSWORD")
// Database connection string
dsn := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s port=%s", host, user, dbname, dbpassword, dbport)
// Openning connection to database
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
FullSaveAssociations: true,
})
if err != nil {
panic(err)
} else {
log.Printf("🤝 Database connected successfully")
}
// Migrate the schema
db.AutoMigrate(&Location{}, &Team{}, &Participant{}, &Action{}, &Attendance{})
db.AutoMigrate(&Team{})
db.AutoMigrate(&Participant{})
// Close the databse connection when the main function closes
//// defer db.Close()
/*----------- API routes ------------*/
port := "8080"
if fromEnv := os.Getenv("PORT"); fromEnv != "" {
port = fromEnv
}
log.Printf("🚀 Starting up on http://localhost:%s", port)
// r := mux.NewRouter()
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
})
r.Route("/teams", func(r chi.Router) {
r.Get("/", GetTeams)
r.Get("/{id}", GetTeam)
r.Post("/", CreateTeam)
r.Delete("/{id}", DeleteTeam)
})
r.Route("/participants", func(r chi.Router) {
r.Get("/", GetParticipants)
r.Get("/{id}", GetParticipant)
r.Post("/", CreateParticipant)
r.Delete("/{id}", DeleteParticipant)
})
r.Route("/locations", func(r chi.Router) {
r.Get("/", GetLocations)
r.Get("/{id}", GetLocation)
r.Post("/", CreateLocation)
r.Delete("/{id}", DeleteLocation)
})
r.Route("/attendance", func(r chi.Router) {
r.Get("/", GetAttendance)
// r.Get("/count/{action_id}", GetAttendance)
r.Post("/", CreateAttendance)
r.Delete("/{id}", DeleteAttendance)
})
r.Route("/actions", func(r chi.Router) {
r.Get("/", GetActions)
r.Get("/with_data", GetActionsWithData)
r.Post("/", CreateAction)
r.Delete("/{id}", DeleteAction)
})
http.ListenAndServe(":8080", r)
}
/*-------- API Controllers --------*/
/*----- Team ------*/
func GetTeam(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var team Team
var participants []Participant
db.First(&team, id)
db.Model(&team)
team.Members = participants
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&team)
}
func GetTeams(w http.ResponseWriter, r *http.Request) {
var teams []Team
db.Preload("Location").Preload("Members").Find(&teams)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&teams)
}
func CreateTeam(w http.ResponseWriter, r *http.Request) {
var team Team
// ctx := context.WithValue(r.Context(), "user", "123")
json.NewDecoder(r.Body).Decode(&team)
// fmt.Printf("%+v", team)
createdTeam := db.Create(&team)
err = createdTeam.Error
if err != nil {
fmt.Println(err)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&createdTeam)
}
func DeleteTeam(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var team Team
db.First(&team, id)
db.Delete(&team)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&team)
}
/*------- Participant ------*/
func GetParticipant(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var participant Participant
db.Preload("Team").Preload("Team.Location").First(&participant, id)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&participant)
}
func GetParticipants(w http.ResponseWriter, r *http.Request) {
var participants []Participant
db.Preload("Team").Find(&participants)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&participants)
}
func CreateParticipant(w http.ResponseWriter, r *http.Request) {
var participant Participant
json.NewDecoder(r.Body).Decode(&participant)
createdParticipant := db.Create(&participant)
err = createdParticipant.Error
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened!"))
}
// var team Team
// db.Where("ID = ?", participant.TeamID).First(&team)
// // Increment Counter
// if(participant.Gender == MALE){
// db.Model(&team).Update("MaleCount", team.MaleCount+1)
// } else{
// db.Model(&team).Update("FemaleCount", team.FemaleCount+1)
// }
// db.Model(&Team{}).Where("ID = ?", participant.TeamID).Update("name", "hello")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&createdParticipant)
}
func DeleteParticipant(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var participant Participant
db.First(&participant, id)
db.Delete(&participant)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&participant)
}
/*------- Location ------*/
func GetLocation(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var location Location
db.First(&location, id)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&location)
}
func GetLocations(w http.ResponseWriter, r *http.Request) {
var locations []Location
db.Preload("Teams").Find(&locations)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&locations)
}
func CreateLocation(w http.ResponseWriter, r *http.Request) {
var location Location
json.NewDecoder(r.Body).Decode(&location)
createdLocation := db.Create(&location)
err = createdLocation.Error
if err != nil {
fmt.Println(err)
}
j, _ := json.MarshalIndent(createdLocation, "", "🐱");
fmt.Println(string(j))
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&createdLocation)
}
func DeleteLocation(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var location Location
db.First(&location, id)
db.Delete(&location)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&location)
}
/*------- Attendance ------*/
func GetAttendance(w http.ResponseWriter, r *http.Request) {
var attendance []Attendance
db.Preload("Participant").Preload("Action").Find(&attendance)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&attendance)
}
// func GetAttendanceCount(w http.ResponseWriter, r *http.Request) {
// action_id := chi.URLParam(r,"action_id")
// var attendance []Attendance
// db.Table("Attendance").Select("COUNT()").Find(&attendance)
// w.Header().Set("Content-Type", "application/json")
// json.NewEncoder(w).Encode(&attendance)
// }
func CreateAttendance(w http.ResponseWriter, r *http.Request) {
var attendance Attendance
json.NewDecoder(r.Body).Decode(&attendance)
allow_duplicates:= chi.URLParam(r,"allowDuplicates")
fmt.Println(allow_duplicates)
// if(allow_duplicates=="true"){
fmt.Println("Allowed duplicates")
createdAttendance := db.Create(&attendance)
err = createdAttendance.Error
if err != nil {
fmt.Println(err)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&createdAttendance)
// }
// if(allow_duplicates=="false" || allow_duplicates==""){
// fmt.Println("Disallowed duplicates")
// createdAttendance := db.FirstOrCreate(&attendance)
// fmt.Println("2",allow_duplicates)
// err = createdAttendance.Error
// if err != nil {
// fmt.Println(err)
// }
// if(createdAttendance.RowsAffected ==0){
// w.WriteHeader(304)
// }
// w.Header().Set("Content-Type", "application/json")
// json.NewEncoder(w).Encode(&createdAttendance)
// }
}
func DeleteAttendance(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var attendance Attendance
db.First(&attendance, id)
db.Delete(&attendance)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&attendance)
}
/*------- Action ------*/
func GetActions(w http.ResponseWriter, r *http.Request) {
// var actions struct {
// ID uint
// title string }
// db.Table("actions").Select("ID","title").Where("valid is true").Scan(&actions)
var actions []Action
db.Preload("Attendance").Preload("Attendance.Action").Where(&Action{Valid: true}).Find(&actions)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&actions)
}
func GetActionsWithData(w http.ResponseWriter, r *http.Request) {
var actions []Action
db.Preload("Attendance").Preload("Attendance.Action").Preload("Attendance.Participant").Preload("Attendance.Participant.Team").Preload("Attendance.Participant.Team.Location").Find(&actions)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&actions)
}
func CreateAction(w http.ResponseWriter, r *http.Request) {
var action Action
json.NewDecoder(r.Body).Decode(&action)
createdAction := db.Create(&action)
err = createdAction.Error
if err != nil {
fmt.Println(err)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&createdAction)
}
func DeleteAction(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r,"id")
var action Action
db.First(&action, id)
db.Delete(&action)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&action)
}