-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
158 lines (128 loc) · 3.99 KB
/
server.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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"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"
)
var client *mongo.Client
var collection *mongo.Collection
type Movie struct {
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
Title string `json:"title,omitempty" bson:"title,omitempty"`
Director string `json:"director,omitempty" bson:"director,omitempty"`
Year int `json:"year,omitempty" bson:"year,omitempty"`
}
func main() {
var err error
client, err = mongo.NewClient(options.Client().ApplyURI("mongodb+srv://SuchirMV:[email protected]/"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)
collection = client.Database("movies").Collection("movies1") // Specify the database name and collection name
router := mux.NewRouter()
router.HandleFunc("/movies", getAllMovies).Methods("GET")
router.HandleFunc("/movies/{id}", getMovie).Methods("GET")
router.HandleFunc("/movies", createMovie).Methods("POST")
router.HandleFunc("/movies/{id}", updateMovie).Methods("PUT")
router.HandleFunc("/movies/{id}", deleteMovie).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080", router))
}
func getAllMovies(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
cursor, err := collection.Find(ctx, bson.M{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer cursor.Close(ctx)
var movies []Movie
if err := cursor.All(ctx, &movies); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movies)
}
func getMovie(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id, _ := strconv.Atoi(params["id"])
var movie Movie
ctx := context.Background()
err := collection.FindOne(ctx, bson.M{"_id": id}).Decode(&movie)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movie)
}
func createMovie(w http.ResponseWriter, r *http.Request) {
var movie Movie
if err := json.NewDecoder(r.Body).Decode(&movie); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ctx := context.Background()
res, err := collection.InsertOne(ctx, movie)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
movie.ID = res.InsertedID.(primitive.ObjectID)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movie)
}
func updateMovie(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id := params["id"]
// Convert id to primitive.ObjectID
objectID, err := primitive.ObjectIDFromHex(id)
if err != nil {
http.Error(w, "Invalid movie ID", http.StatusBadRequest)
return
}
var movie Movie
if err := json.NewDecoder(r.Body).Decode(&movie); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Set the movie ID
movie.ID = objectID
ctx := context.Background()
_, err = collection.ReplaceOne(ctx, bson.M{"_id": objectID}, movie)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Respond with the updated movie
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movie)
}
func deleteMovie(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id, err := primitive.ObjectIDFromHex(params["id"])
if err != nil {
http.Error(w, "Invalid movie ID", http.StatusBadRequest)
return
}
ctx := context.Background()
_, err = collection.DeleteOne(ctx, bson.M{"_id": id})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}