Example #1
0
func ReserveHandler(w http.ResponseWriter, r *http.Request) error {
	//Check POST data
	values, err := util.ValidReservePost(r)
	if err != nil {
		return err
	}

	// Database initialization
	db, err := util.OpenDb()
	if err != nil {
		return err
	}
	defer db.Close()

	// User authentication
	user, userId, err := util.CheckAppCookie(r, db) // return "" if not logged in
	if err != nil {
		return err
	}
	if user == "" {
		return util.NewError(nil, "Login required", 401)
	}

	err = gen.CreateReservation(db, userId, values.ListingId, values.Seats, r.FormValue("Message"))
	if err != nil {
		return err
	}

	w.WriteHeader(200)
	fmt.Fprint(w, "You registered, woo")
	return nil
}
Example #2
0
func LogoutHandler(w http.ResponseWriter, r *http.Request) error {
	db, err := util.OpenDb()
	if err != nil {
		return err
	}
	defer db.Close()

	// User authentication
	_, userId, err := util.CheckAppCookie(r, db) // return "" if not logged in
	if err != nil {
		return err
	}

	// Create gen.InvalidateCookie
	err, expiredCookie := util.DeleteCookie(db, userId, true)
	if err != nil {
		return err
	}
	http.SetCookie(w, &expiredCookie)
	w.WriteHeader(200)

	fmt.Fprint(w, "You logged out")
	return nil
}
Example #3
0
func DashListingsHandler(w http.ResponseWriter, r *http.Request) error {
	token, err := util.ValidDashQuery(r.URL)
	specificListing := false
	if err == nil {
		specificListing = true
	} else {
		token = 0
	}
	// Database initialization
	db, err := util.OpenDb()
	if err != nil {
		return err
	}
	defer db.Close()

	// User authentication
	user, userId, err := util.CheckAppCookie(r, db) // return "" if not logged in
	if err != nil {
		return err
	}
	if user == "" {
		return util.NewError(nil, "Login required", 401)
	}

	// Check post data for if a button was clicked that directed the user here.
	if specificListing {
		err = gen.DeleteAlert(db, userId, "dropped", token)
		if err != nil {
			return err
		}
		err := gen.CheckPost(db, userId, r, token)
		if err != nil {
			return err
		}
	}

	dashListings, err := gen.GetDashListings(db, userId)
	if err != nil {
		return err
	}

	var listing gen.SpecificListing
	if specificListing {
		listing, err = gen.SpecificDashListing(db, dashListings, token)
		if err != nil {
			return err
		}
	}

	body := &gen.DashListingsHTML{
		SidebarListings: dashListings,
		Listing:         listing,
	}

	formatted, err := json.MarshalIndent(body, "", "    ")
	if err != nil {
		return util.NewError(err, "Json conversion failed", 500)
	}
	fmt.Fprint(w, string(formatted))

	return nil
}