-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise.go
56 lines (49 loc) · 1.29 KB
/
exercise.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
package controllers
import (
"callisplanics/db"
"callisplanics/models"
"context"
"go.mongodb.org/mongo-driver/bson"
"log"
"net/http"
)
func GetExercisesHandler() []models.Exercise {
client := db.GetMongoClient()
coll := client.Database("sample_restaurants").Collection("restaurants")
cursor, err := coll.Find(context.TODO(), bson.M{})
if err != nil {
log.Fatal(err)
}
var results []models.Exercise
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
return results
}
func AddExerciseHandler(w http.ResponseWriter, r *http.Request) {
// Check if the request method is POST
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
err := r.ParseForm()
if err != nil {
log.Fatal("Unable to parse form")
http.Error(w, "Unable to parse form", http.StatusBadRequest)
}
name := r.FormValue("name")
description := r.FormValue("description")
log.Print(name)
log.Print(description)
w.WriteHeader(http.StatusCreated)
return GetExercisesHandler()
}
func AddExerciseHandle(exercise models.Exercise) {
client := db.GetMongoClient()
coll := client.Database("sample_restaurants").Collection("restaurants")
result, err := coll.InsertOne(context.TODO(), exercise)
if err != nil {
log.Fatal(err)
}
log.Print(result)
}