예제 #1
0
파일: 5sur.go 프로젝트: ErikBrown/5sur
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, userImg, 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)
	}

	// 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
		}
	}

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

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

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

	page := struct {
		Header gen.HeaderHTML
		Body   gen.DashListingsHTML
	}{
		*header,
		*body,
	}

	err = templates.ExecuteTemplate(w, "dashListings.html", page)
	if err != nil {
		return util.NewError(err, "No se cargó la página", 500)
	}
	return nil
}
예제 #2
0
파일: handlers.go 프로젝트: ErikBrown/5sur
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
}