/
idHandler.go
44 lines (38 loc) · 1.08 KB
/
idHandler.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
package main
import (
"context"
"encoding/json"
"errors"
"log"
"net/http"
"path"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// checks the exsistence of the user in the databse using the unique hashed id
func checkmeet(id primitive.ObjectID) (Meeting, error) {
var meet Meeting
collection := client.Database("zoom").Collection("meets")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err := collection.FindOne(ctx, Meeting{ID: id}).Decode(&meet)
if meet.ID != id {
err = errors.New("ID not found")
}
return meet, err
}
// Fetch the details of the user using the id of the user
func idHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
res.Header().Set("content-type", "application/json")
id, _ := primitive.ObjectIDFromHex(path.Base(req.URL.Path))
meetingwithID, err := checkmeet(id)
if err != nil {
log.Fatal(err.Error())
res.WriteHeader(http.StatusBadRequest)
return
}
json.NewEncoder(res).Encode(meetingwithID)
}
res.WriteHeader(http.StatusBadRequest)
res.Write([]byte(`{"message":"Bad Request"}`))
}