Skip to content

Commit ee42932

Browse files
committed
REST API | MongoDB | Update user controller
1 parent e83f944 commit ee42932

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Install Mongo
2+
3+
# Go get driver for mongo
4+
5+
```
6+
go get gopkg.in/mgo.v2
7+
go get gopkg.in/mgo.v2/bson
8+
```
9+
10+
# In this step:
11+
12+
Don't run this code
13+
14+
Just making updates - a several step process.
15+
16+
We will need a mongo session to use in the CRUD methods.
17+
18+
We need our UserController to have access to a mongo session.
19+
20+
Let's add this to controllers/user.go
21+
22+
```
23+
UserController struct {
24+
session *mgo.Session
25+
}
26+
```
27+
28+
And now add this to controllers/user.go
29+
30+
```
31+
func NewUserController(s *mgo.Session) *UserController {
32+
return &UserController{s}
33+
}
34+
```
35+
36+
And now add this to main.go
37+
38+
```
39+
func getSession() *mgo.Session {
40+
// Connect to our local mongo
41+
s, err := mgo.Dial("mongodb://localhost")
42+
43+
// Check if connection error, is mongo running?
44+
if err != nil {
45+
panic(err)
46+
}
47+
return s
48+
}
49+
```
50+
51+
and this
52+
53+
```
54+
uc := controllers.NewUserController(getSession())
55+
```
56+
57+
1. Enter this at the terminal
58+
59+
```
60+
curl http://localhost:8080/user/1
61+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/aditya43/118-MongoDB-REST/05-mongodb/01-update-user-controller/models"
9+
"github.com/julienschmidt/httprouter"
10+
"gopkg.in/mgo.v2"
11+
)
12+
13+
// added session to our userController
14+
type UserController struct {
15+
session *mgo.Session
16+
}
17+
18+
// added session to our userController
19+
func NewUserController(s *mgo.Session) *UserController {
20+
return &UserController{s}
21+
}
22+
23+
func (uc UserController) GetUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
24+
u := models.User{
25+
Name: "James Bond",
26+
Gender: "male",
27+
Age: 32,
28+
Id: p.ByName("id"),
29+
}
30+
31+
uj, err := json.Marshal(u)
32+
if err != nil {
33+
fmt.Println(err)
34+
}
35+
w.Header().Set("Content-Type", "application/json")
36+
w.WriteHeader(http.StatusOK) // 200
37+
fmt.Fprintf(w, "%s\n", uj)
38+
}
39+
40+
func (uc UserController) CreateUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
41+
u := models.User{}
42+
43+
_ = json.NewDecoder(r.Body).Decode(&u)
44+
45+
u.Id = "007"
46+
47+
uj, err := json.Marshal(u)
48+
if err != nil {
49+
fmt.Println(err)
50+
}
51+
52+
w.Header().Set("Content-Type", "application/json")
53+
w.WriteHeader(http.StatusCreated) // 201
54+
fmt.Fprintf(w, "%s\n", uj)
55+
}
56+
57+
func (uc UserController) DeleteUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
58+
// TODO: only write code to delete user
59+
w.WriteHeader(http.StatusOK) // 200
60+
fmt.Fprint(w, "Write code to delete user\n")
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/aditya43/118-MongoDB-REST/05-mongodb/01-update-user-controller/controllers"
8+
"github.com/julienschmidt/httprouter"
9+
"gopkg.in/mgo.v2"
10+
)
11+
12+
func main() {
13+
r := httprouter.New()
14+
// Get a UserController instance
15+
uc := controllers.NewUserController(getSession())
16+
r.GET("/user/:id", uc.GetUser)
17+
r.POST("/user", uc.CreateUser)
18+
r.DELETE("/user/:id", uc.DeleteUser)
19+
log.Fatal(http.ListenAndServe("localhost:8080", r))
20+
}
21+
22+
func getSession() *mgo.Session {
23+
// Connect to our local mongo
24+
s, err := mgo.Dial("mongodb://localhost")
25+
26+
// Check if connection error, is mongo running?
27+
if err != nil {
28+
panic(err)
29+
}
30+
return s
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package models
2+
3+
type User struct {
4+
Name string `json:"name"`
5+
Gender string `json:"gender"`
6+
Age int `json:"age"`
7+
Id string `json:"id"`
8+
}

0 commit comments

Comments
 (0)