Created
May 29, 2019 20:20
-
-
Save sunnyRK/f19052e8fd7b64dd127bd4ea1b919f5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"math/rand" | |
"net/http" | |
"strconv" | |
"github.com/gorilla/mux" | |
) | |
// Book struct (Model) | |
type Book struct { | |
ID string `json:"id"` | |
Isbn string `json:"isbn"` | |
Title string `json:"title"` | |
Author *Author `json:"author"` | |
} | |
// Author struct | |
type Author struct { | |
Firstname string `json:"firstname"` | |
Lastname string `json:"lastname"` | |
} | |
// Init books var as a slice Book struct | |
var books []Book | |
// Get All Books | |
func getBooks(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(books) | |
} | |
// Get single book | |
func getBook(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
params := mux.Vars(r) // Gets params | |
// Loop through books and find one with the id from the params | |
for _, item := range books { | |
if item.ID == params["id"] { | |
json.NewEncoder(w).Encode(item) | |
return | |
} | |
} | |
json.NewEncoder(w).Encode(&Book{}) | |
} | |
// Add new book | |
func AddBook(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
var book Book | |
_ = json.NewDecoder(r.Body).Decode(&book) | |
book.ID = strconv.Itoa(rand.Intn(100000000)) // Mock ID - not safe | |
books = append(books, book) | |
json.NewEncoder(w).Encode(book) | |
} | |
// Update book | |
func updateBook(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
params := mux.Vars(r) | |
for index, item := range books { | |
if item.ID == params["id"] { | |
books = append(books[:index], books[index+1:]...) | |
var book Book | |
_ = json.NewDecoder(r.Body).Decode(&book) | |
book.ID = params["id"] | |
books = append(books, book) | |
json.NewEncoder(w).Encode(book) | |
return | |
} | |
} | |
} | |
// Delete book | |
func deleteBook(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
params := mux.Vars(r) | |
for index, item := range books { | |
if item.ID == params["id"] { | |
books = append(books[:index], books[index+1:]...) | |
break | |
} | |
} | |
json.NewEncoder(w).Encode(books) | |
} | |
// Main function | |
func main() { | |
// Init router | |
r := mux.NewRouter() | |
// Hardcoded data - @todo: add database | |
books = append(books, Book{ID: "1", Isbn: "438227", Title: "Book One", Author: &Author{Firstname: "John", Lastname: "Doe"}}) | |
books = append(books, Book{ID: "2", Isbn: "454555", Title: "Book Two", Author: &Author{Firstname: "Steve", Lastname: "Smith"}}) | |
// Route handles & endpoints | |
r.HandleFunc("/books", getBooks).Methods("GET") | |
r.HandleFunc("/books/{id}", getBook).Methods("GET") | |
r.HandleFunc("/books", AddBook).Methods("POST") | |
r.HandleFunc("/books/{id}", updateBook).Methods("PUT") | |
r.HandleFunc("/books/{id}", deleteBook).Methods("DELETE") | |
// Start server | |
log.Fatal(http.ListenAndServe(":8000", r)) | |
} | |
// Request sample | |
// { | |
// "isbn":"4545454", | |
// "title":"Book Three", | |
// "author":{ | |
// "firstname":"Harry", | |
// "lastname":"White" | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment