Beispiel #1
0
func DeleteListingHandler(w http.ResponseWriter, r *http.Request) error {
	// Database initialization
	db, err := util.OpenDb()
	if err != nil {
		return err
	}
	defer db.Close()

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

	if userId == 0 {
		return util.NewError(nil, "Se requiere ingreso a la cuenta", 401)
	}

	if r.PostFormValue("d") == "" {
		listingId, err := util.ValidDashQuery(r.URL)
		if err != nil {
			return err
		}
		err = templates.ExecuteTemplate(w, "deleteListing.html", listingId)
		if err != nil {
			return util.NewError(err, "No se cargó la página", 500)
		}
		return nil
	}
	listingId, err := strconv.Atoi(r.FormValue("d"))
	if err != nil {
		return util.NewError(nil, "Viaje invalido", 400)
	}

	registeredUsers, err := gen.DeleteListing(db, userId, listingId)
	if err != nil {
		return err
	}

	for _, value := range registeredUsers {
		err = gen.CreateAlert(db, value.Id, "deleted", listingId)
		if err != nil {
			return err
		}
		err = gen.DeleteAlert(db, value.Id, "accepted", listingId)
		if err != nil {
			return err
		}
		err = gen.DeleteAlert(db, value.Id, "removed", listingId)
		if err != nil {
			return err
		}
	}

	err = gen.DeleteAlert(db, userId, "pending", listingId)
	if err != nil {
		return err
	}
	err = gen.DeleteAlert(db, userId, "dropped", listingId)
	if err != nil {
		return err
	}

	http.Redirect(w, r, "https://5sur.com/dashboard/listings", 303)
	return nil
}
Beispiel #2
0
func DashReservationsHandler(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 "",0 if not logged in
	if err != nil {
		return err
	}

	if userId == 0 {
		return util.NewError(nil, "Se requiere ingreso a la cuenta", 401)
	}

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

	reservation := gen.Reservation{}
	token, err := util.ValidDashQuery(r.URL)
	if err == nil {
		reservation, err = gen.SpecificDashReservation(db, dashReservations, token)
		if err != nil {
			return err
		}
		err = gen.DeleteAlert(db, userId, "accepted", token)
		if err != nil {
			return err
		}
	} else {
		err = gen.DeleteAlert(db, userId, "removed", 0)
		if err != nil {
			return err
		}
		err = gen.DeleteAlert(db, userId, "deleted", 0)
		if err != nil {
			return err
		}
	}

	url, err := gen.CheckReservePost(db, userId, r, token)
	if err != nil {
		return err
	}
	if url != "" {
		http.Redirect(w, r, url, 303)
		return nil
	}

	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.DashReservationsHTML{
		SidebarReservations: dashReservations,
		Reservation:         reservation,
	}

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

	err = templates.ExecuteTemplate(w, "dashReservations.html", page)
	if err != nil {
		return util.NewError(err, "No se cargó la página", 500)
	}
	return nil
}
Beispiel #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, 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
}
Beispiel #4
0
func DashMessagesHandler(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
	}

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

	messages := gen.MessageThread{}
	token, err := util.ValidDashQuery(r.URL) // Ignore error here
	if err == nil {
		err = gen.DeleteAlert(db, userId, "message", token)
		if err != nil {
			return err
		}
		messages, err = gen.SpecificDashMessage(db, dashMessages, token, userId)
		if err != nil {
			return err
		}
		err = gen.SetMessagesClosed(db, token, userId)
		if err != nil {
			return err
		}
		for key := range dashMessages {
			if dashMessages[key].Name == messages.Name {
				dashMessages[key].Count = 0
			}
		}
	}

	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.DashMessagesHTML{
		SidebarMessages: dashMessages,
		MessageThread:   messages,
	}

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

	err = templates.ExecuteTemplate(w, "dashMessages.html", page)
	if err != nil {
		return util.NewError(err, "No se cargó la página", 500)
	}
	return nil
}
Beispiel #5
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
}