This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstudent.go
More file actions
94 lines (81 loc) · 2.71 KB
/
student.go
File metadata and controls
94 lines (81 loc) · 2.71 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
// SPDX-FileCopyrightText: 2022-present Intel Corporation
// SPDX-FileCopyrightText: 2021 Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/omec-project/util/logger"
"go.mongodb.org/mongo-driver/bson"
)
type Student struct {
// ID primitive.ObjectID `bson:"_id,omitempty"`
Name string `bson:"name,omitempty"`
Age int `bson:"age,omitempty"`
Subject string `bson:"subject,omitempty"`
CreatedAt time.Time `bson:"createdAt,omitempty"`
CustomInfo map[string]interface{} `bson:"customInfo,omitempty"`
}
func StudentRecordTest(c *gin.Context) {
c.String(http.StatusOK, "StudentRecordTest!")
collName := "student"
_, errVal := mongoHndl.CreateIndex(collName, "Name")
if errVal != nil {
logger.MongoapiLog.Errorln("create index failed on Name field:", errVal)
}
// add document to student collection.
insertStudentInDB(collName, "Osman Amjad", 21)
// update document in student collection.
insertStudentInDB(collName, "Osman Amjad", 22)
// fetch document from student db based on index
student, err := getStudentFromDB(collName, "Osman Amjad")
if err == nil {
logger.MongoapiLog.Infof("retrieved student %v", student)
} else {
logger.MongoapiLog.Errorf("failed to retrieve student %v. Error - %+v", student, err)
}
insertStudentInDB(collName, "John Smith", 25)
// test document fetch from student that doesn't exist.
qName := "Nerf Doodle"
_, err = getStudentFromDB(collName, qName)
if err == nil {
logger.MongoapiLog.Infof("retrieved student %v", qName)
} else {
logger.MongoapiLog.Errorf("failed to retrieve student %v. Error - %+v", qName, err)
}
c.JSON(http.StatusOK, gin.H{})
}
func insertStudentInDB(collName string, name string, age int) {
student := Student{
Name: name,
Age: age,
CreatedAt: time.Now(),
}
filter := bson.M{}
_, err := mongoHndl.PutOneCustomDataStructure(collName, filter, student)
if err != nil {
logger.MongoapiLog.Errorf("inserting student %v failed with error %+v", student, err)
return
}
logger.MongoapiLog.Infof("inserting student %v successful", student)
}
func getStudentFromDB(collName string, name string) (Student, error) {
var student Student
filter := bson.M{}
filter["name"] = name
result, err := mongoHndl.GetOneCustomDataStructure(collName, filter)
if err == nil {
bsonBytes, errMarshal := bson.Marshal(result)
if errMarshal != nil {
return student, errMarshal
}
if errUnmarshal := bson.Unmarshal(bsonBytes, &student); errUnmarshal != nil {
return student, errUnmarshal
}
return student, nil
}
return student, err
}