Beispiel #1
0
// Registers the new user
func registerHandler(w http.ResponseWriter, r *http.Request) {

	usr := new(user.User)
	usr.Email = r.FormValue("email")
	pass := r.FormValue("pwd")

	if len(pass) > 0 {
		usr.Password = codify.SHA(pass)
		if user.DoesAccountExist(usr.Email) {
			http.Redirect(w, r, "/account-exists", http.StatusFound)
		} else {
			ok := user.CreateAccount(usr)
			if ok {
				http.Redirect(w, r, "/success", http.StatusFound)
			} else {
				http.Redirect(w, r, "/failed", http.StatusFound)
			}
		}
	} else {
		viewHandler(w, r)
	}
}
Beispiel #2
0
// Adds a new book to the database/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)
		}

		viewHandler(w, r)
	}
}