Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 18, 2015 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/add3b442b042944fcd69 to your computer and use it in GitHub Desktop.
Save lettergram/add3b442b042944fcd69 to your computer and use it in GitHub Desktop.
// Adds a new book to the book database and user
func bookHandler(w http.ResponseWriter, r *http.Request) {
// Parses username from the cookie
cookie, _ := r.Cookie("SessionID")
sessionID := cookie.Value
z := strings.Split(sessionID, ":")
username := z[0]
// Checks to see if user exists
if !user.DoesAccountExist(username) {
http.HandleFunc("/", viewHandler)
}
book := new(bkz.Book)
book.Title = r.FormValue("book")
book.Author = r.FormValue("author")
book.ISBN = r.FormValue("isbn")
book.Genre = r.FormValue("genre")
// isbn is being used as Id for simplicity
book.Id = book.ISBN
if len(book.Title) > 0 {
// Creates new book entry in database
ok := bkz.CreateBook(book)
// Redirects based on outcome and adds book to users collection
if ok && user.UpdateCollection(username, book) {
http.Redirect(w, r, "/add-book-success", http.StatusFound)
} else {
http.Redirect(w, r, "/add-book-failed", http.StatusFound)
}
} else {
viewHandler(w, r)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment