Created
March 18, 2015 23:45
-
-
Save lettergram/add3b442b042944fcd69 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
// 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