-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (197 loc) · 6.93 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
package main
import (
"awesomeProjects/Mongodb"
"awesomeProjects/handlers"
"fmt"
"github.com/gorilla/mux"
"log"
"math/rand"
"net/http"
"time"
)
func main() {
// Seed the random number generator to get different values each time
rand.Seed(time.Now().UnixNano())
err := Mongodb.ConnectMongoDB()
if err != nil {
fmt.Println(err)
return
}
router := mux.NewRouter()
router.HandleFunc("/students", handlers.AddStudent).Methods(http.MethodPost)
router.HandleFunc("/students/{id}", handlers.UpdateStudent).Methods(http.MethodPut)
router.HandleFunc("/students/{id}", handlers.DeleteStudent).Methods(http.MethodDelete)
router.HandleFunc("/students", handlers.GetAllStudents).Methods(http.MethodGet)
router.HandleFunc("/students/{id}", handlers.GetStudent).Methods(http.MethodGet)
router.HandleFunc("/students/{id}", handlers.PayAmount).Methods(http.MethodPut)
log.Println("API is running!")
err = http.ListenAndServe(":4000", router)
if err != nil {
log.Println("Rest Server error" + err.Error())
}
/*// Define the range
start := 13
end := 25
// Create a list to store 2 students
var studentList []models.Student
for i := 0; i < 2; i++ {
// Randomize values
name := generateRandomName()
zipCode := generateRandomZipCode()
age := generateRandomAge(start, end)
height := generateRandomHeight()
balance := generateRandomBalance()
// Create a student using the CreateStudent function
student, _ := Services.CreateStudent(name, zipCode, age, height, balance, "[email protected]")
// Append the student to the list
studentList = append(studentList, student)
}
for i := 0; i < len(studentList); i++ {
fmt.Printf("Student ID: %d\n", studentList[i].ID)
fmt.Printf("Name: %s\n", studentList[i].Name)
fmt.Printf("Zip Code: %s\n", studentList[i].ZipCode)
fmt.Printf("Age: %d\n", studentList[i].Age)
fmt.Printf("Height: %.2f\n", studentList[i].Height)
fmt.Printf("Balance: %.2f\n", studentList[i].Balance)
fmt.Println("---------------------")
}
thresholdBalance := 500.0
belowThresholdStudents := Services.BalanceChecker(studentList, thresholdBalance)
// Print details of students with a balance below the threshold
fmt.Println("Students with a balance below $500:")
for _, student := range belowThresholdStudents {
fmt.Printf("Student ID: %d\n", student.ID)
fmt.Printf("Name: %s\n", student.Name)
fmt.Printf("Balance: %.2f\n", student.Balance)
fmt.Println("---------------------")
}
searchName := "Veom"
foundStudents, err := Mongodb.SearchStudentsByName(searchName)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Students with name '%s':\n", searchName)
for _, student := range foundStudents {
fmt.Printf("Student ID: %s\n", student.ID.Hex())
fmt.Printf("Name: %s\n", student.Name)
fmt.Printf("Zip Code: %s\n", student.ZipCode)
fmt.Printf("Age: %d\n", student.Age)
fmt.Printf("Height: %.2f\n", student.Height)
fmt.Printf("Balance: %.2f\n", student.Balance)
fmt.Println("---------------------")
}
// Search for students with a balance greater than or equal to a certain amount
minBalance := 500.0
foundStudents, err = Mongodb.SearchStudentsByBalance(minBalance)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Students with a balance greater than or equal to $%.2f:\n", minBalance)
for _, student := range foundStudents {
fmt.Printf("Student ID: %s\n", student.ID.Hex())
fmt.Printf("Name: %s\n", student.Name)
fmt.Printf("Zip Code: %s\n", student.ZipCode)
fmt.Printf("Age: %d\n", student.Age)
fmt.Printf("Height: %.2f\n", student.Height)
fmt.Printf("Balance: %.2f\n", student.Balance)
fmt.Println("---------------------")
}
// Delete all students from MongoDB
//err = Services.DeleteAll()
//if err != nil {
// fmt.Println(err)
// return
//}
fmt.Println("All students deleted from MongoDB.")
existingStudent, err := Mongodb.GetAnyStudent()
if err != nil {
fmt.Println(err)
return
}
// Retrieve the ObjectID of the existing student
existingStudentID := existingStudent.ID
// Print details of the existing student
fmt.Printf("Existing Student Details:\n")
fmt.Printf("Student ID: %s\n", existingStudentID.Hex())
fmt.Printf("Name: %s\n", existingStudent.Name)
fmt.Printf("Zip Code: %s\n", existingStudent.ZipCode)
fmt.Printf("Age: %d\n", existingStudent.Age)
fmt.Printf("Height: %.2f\n", existingStudent.Height)
fmt.Printf("Balance: %.2f\n", existingStudent.Balance)
// Now you can perform operations on the existing student, such as paying an amount
amount := 50.0
err = Services.Payamount(existingStudentID, amount)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Paid $%.2f for existing student with ID %s\n", amount, existingStudentID.Hex())*/
// Retrieve the student's balance from MongoDB
/*studentIDToUpdate := 5
additionalBalance := 100.0
//updatedStudentList, err := Services.SearchStudentByIDAndUpdateBalance(studentList, studentIDToUpdate, additionalBalance)
// Handle the error, if any
if err != nil {
fmt.Println(err)
return
}
// Print details of the updated student list
fmt.Println("Updated student list:")
for _, student := range updatedStudentList {
fmt.Printf("Student ID: %d\n", student.ID)
fmt.Printf("Name: %s\n", student.Name)
fmt.Printf("Balance: %.2f\n", student.Balance)
fmt.Println("---------------------")
}
// Call the function to print odd numbers in the range
//util.PrintOddNumbers(start, end)
//util.PrintEvenNumbers(start, end)
// Define a string
//inputString := "programming is fun and challenging"
// Find the maximum occurring character
//maxChar, maxCount := util.FindMaxOccurrence(inputString)
// Print the result
//fmt.Printf("Maximum occurring character: %c\n", maxChar)
//fmt.Printf("Occurrences: %d\n", maxCount)
/*student := Services.CreateStudent("Jane Doe", "54321", 22, 160.0, 150.6, 1)
fmt.Println("Name:", student.Name)
fmt.Println("Zip Code:", student.ZipCode)
fmt.Println("Age:", student.Age)
fmt.Println("Height:", student.Height)
}
// Function to generate a random name
func generateRandomName() string {
names := []string{"Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace", "Henry", "Ivy", "Jack"}
return names[rand.Intn(len(names))]
}
// Function to generate a random zip code
func generateRandomZipCode() string {
return fmt.Sprintf("%05d", rand.Intn(100000))
}
// Function to generate a random age in the specified range
func generateRandomAge(start, end int) int {
return rand.Intn(end-start+1) + start
}
// Function to generate a random height
func generateRandomHeight() float64 {
return rand.Float64()*50 + 150
}
// Function to generate a random balance
func generateRandomBalance() float64 {
return rand.Float64() * 1000
*/
/*if err != nil {
fmt.Println(err)
return
}
err = Services.DeleteAll()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("All students deleted from MongoDB.")
*/
Mongodb.DisconnectMongoDB()
}