Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// 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
You can’t perform that action at this time.