Пример #1
0
func SendMessageHandler(w http.ResponseWriter, r *http.Request) error {
	recipientId, err := util.ValidMessageURL(r)
	if err != nil {
		return err
	}

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

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

	if userId == 0 {
		return util.NewError(nil, "Se requiere ingreso a la cuenta", 401)
	}
	userInfo, err := gen.ReturnUserInfo(db, recipientId)
	if err != nil {
		return err
	}

	err = templates.ExecuteTemplate(w, "message.html", userInfo)
	if err != nil {
		return util.NewError(err, "No se cargó la página", 500)
	}
	return nil
}
Пример #2
0
func UserHandler(w http.ResponseWriter, r *http.Request) error {
	// Database initialization
	db, err := util.OpenDb()
	if err != nil {
		return err
	}
	defer db.Close()

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

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

	splits := strings.Split(r.URL.Path, "/")
	userInfo, err := gen.ReturnUserInfo(db, splits[2])
	if err != nil {
		return err
	}

	header := &gen.HeaderHTML{
		Title:     user,
		Username:  user,
		Alerts:    len(alerts),
		AlertText: alerts,
		UserImage: userImg,
	}

	page := struct {
		Header gen.HeaderHTML
		Body   gen.User
	}{
		*header,
		userInfo,
	}

	err = templates.ExecuteTemplate(w, "user.html", page)
	if err != nil {
		return util.NewError(err, "No se cargó la página", 500)
	}
	return nil
}
Пример #3
0
func ReserveFormHandler(w http.ResponseWriter, r *http.Request) error {
	l, err := util.ValidReserveURL(r)
	if err != nil {
		return err
	}

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

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

	if user == "" {
		return util.NewError(nil, "Se requiere ingreso a la cuenta", 401)
	}
	listing, err := gen.ReturnIndividualListing(db, l)
	if err != nil {
		return err
	}

	seats := make([]int, 0)

	for i := 1; i <= listing.Seats; i++ {
		seats = append(seats, i)
	}

	driver, err := gen.ReturnUserInfo(db, listing.Driver)

	reserve := &gen.ReserveHTML{
		ListingId: listing.Id,
		Driver:    driver.Name,
		Seats:     seats,
	}

	err = templates.ExecuteTemplate(w, "reserve.html", reserve)
	if err != nil {
		return util.NewError(err, "No se cargó la página", 500)
	}
	return nil
}
Пример #4
0
func UserHandler(w http.ResponseWriter, r *http.Request) error {
	// Database initialization
	db, err := util.OpenDb()
	if err != nil {
		return err
	}
	defer db.Close()

	splits := strings.Split(r.URL.Path, "/")
	user, err := gen.ReturnUserInfo(db, splits[3])
	if err != nil {
		return err
	}

	formatted, err := json.MarshalIndent(user, "", "    ")
	if err != nil {
		return util.NewError(err, "Json conversion failed", 500)
	}

	fmt.Fprint(w, string(formatted))
	return nil
}