-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
582 lines (476 loc) · 14.6 KB
/
main.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package main
import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var client *mongo.Client
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
port := os.Getenv("PORT")
if port == "" {
port = "5000"
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// uri := os.Getenv("MONGODB_URI")
uri := "" // place your atlas URI here
client, _ = mongo.Connect(ctx, options.Client().ApplyURI(uri))
router := gin.New()
router.Use(CORSMiddleware())
router.Use(gin.Logger())
router.POST("/createjob", func(c *gin.Context) {
var job Job
if err := c.BindJSON(&job); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
collection := client.Database("gomongo").Collection("job")
result, err := collection.InsertOne(ctx, job)
if err != nil {
msg := fmt.Sprintf("Job was not created")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, result)
})
router.GET("/jobs", func(c *gin.Context) {
jobs := []Job{}
collection := client.Database("gomongo").Collection("job")
cursor, err := collection.Find(context.TODO(), bson.M{})
if err != nil {
msg := fmt.Sprintf("no jobs found")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
for cursor.Next(context.TODO()) {
var job Job
cursor.Decode(&job)
jobs = append(jobs, job)
}
defer cancel()
c.JSON(http.StatusOK, gin.H{
"data": jobs,
})
return
})
router.GET("/job/:jobId", func(c *gin.Context) {
jobId := c.Param("jobId")
job := Job{}
collection := client.Database("gomongo").Collection("job")
objId, _ := primitive.ObjectIDFromHex(jobId)
err := collection.FindOne(context.TODO(), bson.M{"_id": objId}).Decode(&job)
if err != nil {
msg := fmt.Sprintf("current job not found")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, gin.H{
"data": job,
})
})
router.GET("/jobs/:jobtype", func(c *gin.Context) {
jobs := []Job{}
jobtype := c.Param("jobtype")
collection := client.Database("gomongo").Collection("job")
// filter := bson.D{{"jobtype", jobtype}}
cursor, err := collection.Find(context.TODO(), bson.D{{"jobtype", jobtype}})
if err != nil {
msg := fmt.Sprintf("no jobs found under this section")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
for cursor.Next(context.TODO()) {
var job Job
cursor.Decode(&job)
jobs = append(jobs, job)
}
defer cancel()
c.JSON(http.StatusOK, gin.H{
"data": jobs,
})
})
// student routes
router.POST("/login", func(c *gin.Context) {
// var student Student
stu := Student{}
CollegeID := c.Query("collegeid")
Password := c.Query("password")
collection := client.Database("gomongo").Collection("student")
err := collection.FindOne(context.TODO(), bson.M{"collegeid": CollegeID}).Decode(&stu)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"message": "Student not found",
})
return
}
if Password != *stu.Password {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"message": "Incorrect Password",
})
return
}
token, err := CreateToken(*stu.CollegeID)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"message": "Could not create token",
})
return
}
defer cancel()
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "Login successful",
"collegeid": stu.CollegeID,
"pending": stu.Pending,
"token": token,
})
})
router.POST("/addstudent", func(c *gin.Context) {
var student Student
if err := c.BindJSON(&student); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
v := rand.Intn(999999999-100000000) + 100000000
CollegeID := strconv.Itoa(v)
Password := CollegeID
FirstName := student.FirstName
MiddleName := student.MiddleName
LastName := student.LastName
DateOfBirth := student.DateOfBirth
Gender := student.Gender
FatherName := student.FatherName
MotherName := student.MotherName
MotherMaidenName := student.MotherMaidenName
BloodGroup := student.BloodGroup
MartialStatus := student.MartialStatus
MotherTongue := student.MotherTongue
Caste := student.Caste
PersonalEmail := student.PersonalEmail
Identification := student.Identification
Disability := student.Disability
PlaceOfBirth := student.PlaceOfBirth
Heigth := student.Heigth
Weight := student.Weight
Religion := student.Religion
Nationality := student.Nationality
AdmissionDate := student.AdmissionDate
MajorDegree := student.MajorDegree
Reference := student.Reference
Program := student.Program
Regulation := student.Regulation
Pending := true
newStudent := Student{
CollegeID: &CollegeID,
Password: &Password,
FirstName: FirstName,
MiddleName: MiddleName,
LastName: LastName,
DateOfBirth: DateOfBirth,
Gender: Gender,
FatherName: FatherName,
MotherName: MotherName,
MotherMaidenName: MotherMaidenName,
BloodGroup: BloodGroup,
MartialStatus: MartialStatus,
MotherTongue: MotherTongue,
Caste: Caste,
PersonalEmail: PersonalEmail,
Identification: Identification,
Disability: Disability,
PlaceOfBirth: PlaceOfBirth,
Heigth: Heigth,
Weight: Weight,
Religion: Religion,
Nationality: Nationality,
AdmissionDate: AdmissionDate,
MajorDegree: MajorDegree,
Reference: Reference,
Program: Program,
Regulation: Regulation,
Pending: &Pending,
}
collection := client.Database("gomongo").Collection("student")
result, err := collection.InsertOne(context.TODO(), newStudent)
if err != nil {
msg := fmt.Sprintf("student was not added")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, result)
})
router.GET("/studentDetails/:collegeid", func(c *gin.Context) {
collegeid := c.Param("collegeid")
student := Student{}
// address := Address{}
// contact := Contacts{}
// identity := Identity{}
// qualification := Qualifications{}
collection := client.Database("gomongo").Collection("student")
// collection2 := client.Database("gomongo").Collection("address")
// collection3 := client.Database("gomongo").Collection("contacts")
// collection4 := client.Database("gomongo").Collection("identity")
// collection5 := client.Database("gomongo").Collection("qualifications")
err := collection.FindOne(context.TODO(), bson.M{"collegeid": collegeid}).Decode(&student)
// err2 := collection2.FindOne(context.TODO(), bson.M{"collegeid": collegeid}).Decode(&address)
// err3 := collection3.FindOne(context.TODO(), bson.M{"collegeid": collegeid}).Decode(&contact)
// err4 := collection4.FindOne(context.TODO(), bson.M{"collegeid": collegeid}).Decode(&identity)
// err5 := collection5.FindOne(context.TODO(), bson.M{"collegeid": collegeid}).Decode(&qualification)
if err != nil {
msg := fmt.Sprintf("student not found")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
// if err3 != nil {
// fmt.Sprintf("details not found")
// return
// }
defer cancel()
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"studentDetails": student,
// "studentAddress": address,
// "studentContact": contact,
// "studentIdentity": identity,
// "studentQualification": qualification,
})
})
router.POST("/changePassword/:collegeid", func(c *gin.Context) {
var student Student
isForgot := c.Query("forgot")
collegeid := c.Param("collegeid")
if err := c.BindJSON(&student); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
password := student.Password
if isForgot == "yes" {
c.JSON(http.StatusOK, gin.H{
"message": "under process need to send a verification email",
})
} else {
newData := bson.M{
"$set": bson.M{
"password": password,
"pending": false,
},
}
collection := client.Database("gomongo").Collection("student")
_, err := collection.UpdateOne(context.TODO(), bson.M{"collegeid": collegeid}, newData)
if err != nil {
log.Printf("Error, Reason: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{
"status": 500,
"message": "Something went wrong",
})
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "password successfully updated",
})
}
})
//address routes
router.POST("/addaddress/:stuId", func(c *gin.Context) {
stuId := c.Param("stuId")
var address Address
if err := c.BindJSON(&address); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
CollegeID := stuId
AddressType := address.AddressType
Doorno := address.Doorno
Street := address.Street
Landmark := address.Landmark
City := address.City
District := address.District
State := address.State
Country := address.Country
newAddess := Address{
CollegeID: &CollegeID,
AddressType: AddressType,
Doorno: Doorno,
Street: Street,
Landmark: Landmark,
City: City,
District: District,
State: State,
Country: Country,
}
collection := client.Database("gomongo").Collection("address")
result, err := collection.InsertOne(context.TODO(), newAddess)
if err != nil {
msg := fmt.Sprintf("contact of student was not added")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, result)
})
// contact routes
router.POST("/addcontact/:stuId", func(c *gin.Context) {
stuId := c.Param("stuId")
var contact Contacts
if err := c.BindJSON(&contact); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
CollegeID := stuId
ContactType := contact.ContactType
ContactPerson := contact.ContactPerson
PhoneNumber := contact.PhoneNumber
newContact := Contacts{
CollegeID: &CollegeID,
ContactType: ContactType,
ContactPerson: ContactPerson,
PhoneNumber: PhoneNumber,
}
collection := client.Database("gomongo").Collection("contacts")
result, err := collection.InsertOne(context.TODO(), newContact)
if err != nil {
msg := fmt.Sprintf("contact of student was not added")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, result)
})
//identity routes
router.POST("/additentity/:stuId", func(c *gin.Context) {
stuId := c.Param("stuId")
var identity Identity
if err := c.BindJSON(&identity); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
CollegeID := stuId
IdentityType := identity.IdentityType
IdentityNumber := identity.IdentityNumber
IssuedOn := identity.IssuedOn
DateOfExpiry := identity.DateOfExpiry
PlaceOfIssue := identity.PlaceOfIssue
newIdentity := Identity{
CollegeID: &CollegeID,
IdentityType: IdentityType,
IdentityNumber: IdentityNumber,
IssuedOn: IssuedOn,
DateOfExpiry: DateOfExpiry,
PlaceOfIssue: PlaceOfIssue,
}
collection := client.Database("gomongo").Collection("identity")
result, err := collection.InsertOne(context.TODO(), newIdentity)
if err != nil {
msg := fmt.Sprintf("identity of student was not added")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, result)
})
//qualification routes
router.POST("/addqualification/:stuId", func(c *gin.Context) {
stuId := c.Param("stuId")
var qualifications Qualifications
if err := c.BindJSON(&qualifications); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
CollegeID := stuId
Qualification := qualifications.Qualification
Board := qualifications.Board
EduName := qualifications.EduName
CGPA := qualifications.CGPA
YearOfPassing := qualifications.YearOfPassing
Specilization := qualifications.Specilization
newQualifications := Qualifications{
CollegeID: &CollegeID,
Qualification: Qualification,
Board: Board,
EduName: EduName,
CGPA: CGPA,
YearOfPassing: YearOfPassing,
Specilization: Specilization,
}
collection := client.Database("gomongo").Collection("qualifications")
result, err := collection.InsertOne(context.TODO(), newQualifications)
if err != nil {
msg := fmt.Sprintf("qualifications of student was not added")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, result)
})
//course router
router.POST("/addcourse/:stuId", func(c *gin.Context) {
stuId := c.Param("stuId")
var courses Courses
if err := c.BindJSON(&courses); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
CollegeID := stuId
Year := courses.Year
AcademicYear := courses.AcademicYear
Semester := courses.Semester
CourseCode := courses.CourseCode
CourseDesc := courses.CourseDesc
LTPS := courses.LTPS
Section := courses.Section
FacultyName := courses.FacultyName
newCourse := Courses{
CollegeID: &CollegeID,
Year: Year,
AcademicYear: AcademicYear,
Semester: Semester,
CourseCode: CourseCode,
CourseDesc: CourseDesc,
LTPS: LTPS,
Section: Section,
FacultyName: FacultyName,
}
collection := client.Database("gomongo").Collection("courses")
result, err := collection.InsertOne(context.TODO(), newCourse)
if err != nil {
msg := fmt.Sprintf("course of student was not added")
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
return
}
defer cancel()
c.JSON(http.StatusOK, result)
})
router.Run(":" + port)
}