-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
165 lines (136 loc) · 3.4 KB
/
mongo.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
package main
import (
"context"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"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"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
const PORT string = "4000"
const DATABASE_NAME = "talent"
const PERSON_COLLECTION_NAME = "person"
const DB_URI = "mongodb+srv://<username>:<password>@hostandoptions"
type Person struct {
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
}
func main() {
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(DB_URI))
if err != nil {
log.Println("CON ERR")
return
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Println("PING ERR")
return
}
repo := InitPersonRepo(client)
handler := &PersonHandler{
Repo: repo,
}
mux := http.NewServeMux()
mux.HandleFunc("/", handler.Handle)
http.ListenAndServe(":"+PORT, mux)
}
func (h *PersonHandler) Handle(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL.Path)
w.Header().Set("content-type", "application/json")
if r.Method == http.MethodGet {
h.Read(w, r)
return
}
if r.Method == http.MethodPost {
h.Create(w, r)
return
}
err := errors.New("method not allowed")
log.Println(err)
http.Error(w, err.Error(), http.StatusMethodNotAllowed)
}
type PersonHandler struct {
Repo *PersonRepo
}
func (h *PersonHandler) Read(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
people, err := h.Repo.Read(ctx)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Marshal struct to JSON
jsonResp, err := json.Marshal(people)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Write to resp
w.Write(jsonResp)
}
func (h *PersonHandler) Create(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
b, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var person Person
err = json.Unmarshal(b, &person)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id, err := h.Repo.Create(ctx, person)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(id))
}
type PersonRepo struct {
Coll *mongo.Collection
}
func InitPersonRepo(client *mongo.Client) *PersonRepo {
database := client.Database(DATABASE_NAME)
coinCollection := database.Collection(PERSON_COLLECTION_NAME)
return &PersonRepo{
Coll: coinCollection,
}
}
func (repo *PersonRepo) Read(ctx context.Context) ([]Person, error) {
var people []Person
cursor, err := repo.Coll.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
if err = cursor.All(ctx, &people); err != nil {
return nil, err
}
return people, nil
}
func (repo *PersonRepo) Create(ctx context.Context, person Person) (string, error) {
insertResult, err := repo.Coll.InsertOne(ctx, person)
if err != nil {
return "", err
}
// convert inserted id into string
objectId, ok := insertResult.InsertedID.(primitive.ObjectID)
if !ok {
errorStr := "something gone wrong while getting inserted id"
err = errors.New(errorStr)
return "", err
}
return objectId.Hex(), nil
}