-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (93 loc) · 2.82 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type Book struct {
ID string `json: "id"`
Title string `json: "title"`
Author string `json: "author"`
Publisher string `json: "publisher"`
Year string `json: "year"`
}
// Repository of books
var library []Book
var id int = 0
// Get all the movies in the library.
func getBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(library)
}
// Get one book from the library according to its name.
func getBookByName(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
params := mux.Vars(r)
for _, book := range library {
if book.Title == params["title"] {
json.NewEncoder(w).Encode(book)
return
}
}
}
// Delete a book in the library according to its id.
func deleteBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
params := mux.Vars(r)
for index, book := range library {
if book.ID == params["id"] {
library = append(library[:index], library[index+1:]...)
break
}
}
}
// Create a new book and append it in the library.
func createBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:5500")
var book Book
err := json.NewDecoder(r.Body).Decode(&book)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
json.NewEncoder(w).Encode("Something went wrong")
return
}
id++
book.ID = strconv.Itoa(id)
library = append(library, book)
json.NewEncoder(w).Encode(book)
}
// Update a book in the library.
func updateBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
params := mux.Vars(r)
for index, book := range library {
if book.ID == params["id"] {
library = append(library[:index], library[index+1:]...)
var updatedBook Book
err := json.NewDecoder(r.Body).Decode(&updatedBook)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
json.NewEncoder(w).Encode("Something went wrong")
return
}
updatedBook.ID = params["id"]
library = append(library, updatedBook)
json.NewEncoder(w).Encode(updatedBook)
break
}
}
}
func main() {
r := mux.NewRouter()
library = append(library, Book{ID: "0", Title: "Example", Author: "Lucas Herlon", Publisher: "Example", Year: "2023"})
r.HandleFunc("/api/books", getBooks).Methods("GET")
r.HandleFunc("/api/books/{title}", getBookByName).Methods("GET")
r.HandleFunc("/api/books", createBook).Methods("POST")
r.HandleFunc("/api/books/{id}", updateBook).Methods("PUT")
r.HandleFunc("/api/books/{id}", deleteBook).Methods("DELETE")
fmt.Printf("Access the API: localhost:8080/api/books\n")
log.Fatal(http.ListenAndServe(":8080", r))
}