-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
171 lines (140 loc) · 3.53 KB
/
client.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
166
167
168
169
170
171
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
type Movie struct {
ID string `json:"id"`
Title string `json:"title"`
Director string `json:"director"`
Year int `json:"year"`
}
func main() {
var choice int
for {
fmt.Println("Menu:")
fmt.Println("1. Create a new movie")
fmt.Println("2. View all movies")
fmt.Println("3. Update a movie")
fmt.Println("4. Delete a movie")
fmt.Println("5. Exit")
fmt.Print("Enter your choice: ")
fmt.Scanln(&choice)
switch choice {
case 1:
createMovie()
case 2:
getAllMovies()
case 3:
updateMovie()
case 4:
deleteMovie()
case 5:
fmt.Println("Exiting...")
os.Exit(0)
default:
fmt.Println("Invalid choice")
}
}
}
func createMovie() {
var movie Movie
fmt.Println("Enter details of the new movie:")
reader := bufio.NewReader(os.Stdin)
fmt.Print("Title: ")
title, _ := reader.ReadString('\n')
movie.Title = strings.TrimSpace(title)
fmt.Print("Director: ")
director, _ := reader.ReadString('\n')
movie.Director = strings.TrimSpace(director)
fmt.Print("Year: ")
fmt.Scanln(&movie.Year)
jsonData, err := json.Marshal(movie)
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}
resp, err := http.Post("http://localhost:8080/movies", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating movie:", err)
return
}
defer resp.Body.Close()
fmt.Println("Movie created successfully")
}
func getAllMovies() {
resp, err := http.Get("http://localhost:8080/movies")
if err != nil {
fmt.Println("Error fetching movies:", err)
return
}
defer resp.Body.Close()
var movies []Movie
err = json.NewDecoder(resp.Body).Decode(&movies)
if err != nil {
fmt.Println("Error decoding response:", err)
return
}
fmt.Println("All movies:")
for _, movie := range movies {
fmt.Printf("ID: %s, Title: %s, Director: %s, Year: %d\n", movie.ID, movie.Title, movie.Director, movie.Year)
}
}
func updateMovie() {
var movie Movie
fmt.Println("Enter ID of the movie to update: ")
fmt.Scanln(&movie.ID)
var updatedMovie Movie
fmt.Println("Enter new details of the movie:")
reader := bufio.NewReader(os.Stdin)
fmt.Print("Title: ")
title, _ := reader.ReadString('\n')
updatedMovie.Title = strings.TrimSpace(title)
fmt.Print("Director: ")
director, _ := reader.ReadString('\n')
updatedMovie.Director = strings.TrimSpace(director)
fmt.Print("Year: ")
fmt.Scanln(&updatedMovie.Year)
jsonData, err := json.Marshal(updatedMovie)
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}
url := fmt.Sprintf("http://localhost:8080/movies/%s", movie.ID)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Set content type header
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Error updating movie:", err)
return
}
defer resp.Body.Close()
fmt.Println("Movie updated successfully")
}
func deleteMovie() {
var movie Movie
fmt.Println("Enter ID of the movie to delete: ")
fmt.Scanln(&movie.ID)
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("http://localhost:8080/movies/%s", movie.ID), nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Error deleting movie:", err)
return
}
defer resp.Body.Close()
fmt.Println("Movie deleted successfully")
}