func RootHandler(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 } header := &gen.HeaderHTML{ Username: user, Alerts: len(alerts), AlertText: alerts, UserImage: userImg, } cities, err := gen.ReturnFilter(db) if err != nil { return err } listings, err := gen.ReturnAllListings(db) if err != nil { return err } body := &gen.ListingsHTML{ Filter: cities, Listings: listings, Homepage: true, } page := struct { Header gen.HeaderHTML Body gen.ListingsHTML }{ *header, *body, } err = templates.ExecuteTemplate(w, "listings.html", page) if err != nil { return util.NewError(err, "No se cargó la página", 500) } return nil }
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 }
func DashSettingsHandler(w http.ResponseWriter, r *http.Request) error { db, err := util.OpenDb() if err != nil { return err } defer db.Close() user, userId, userImg, 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) } 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, } page := struct { Header gen.HeaderHTML }{ *header, } err = templates.ExecuteTemplate(w, "dashSettings.html", page) if err != nil { return util.NewError(err, "No se cargó la página", 500) } return nil }
func ListingsHandler(w http.ResponseWriter, r *http.Request) error { // Convert POST to GET (also does a time validation) if r.FormValue("Origin") != "" || r.FormValue("Destination") != "" { convertedDate := "" convertedTime := "" var err error if r.FormValue("Date") == "" { convertedDate, convertedTime = util.ReturnCurrentTimeString(true) } else if r.FormValue("Time") == "" { convertedDate, _, err = util.ReturnTimeString(false, r.FormValue("Date"), "00:00") currentDate, currentTime := util.ReturnCurrentTimeString(true) if currentDate == convertedDate { convertedTime = currentTime } else { convertedTime = "00:00" } } else { convertedDate, convertedTime, err = util.ReturnTimeString(false, r.FormValue("Date"), r.FormValue("Time")) if err != nil { return err } } http.Redirect(w, r, "https://5sur.com/l/?o="+r.FormValue("Origin")+"&d="+r.FormValue("Destination")+"&t="+convertedDate+"&h="+convertedTime, 303) return nil } // Query string validation query, err := util.ValidListingQuery(r.URL) if err != nil { return err } // 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 } header := &gen.HeaderHTML{ Title: "Viajes", Username: user, Alerts: len(alerts), AlertText: alerts, UserImage: userImg, } cities, err := gen.ReturnFilter(db) if err != nil { return err } listings, err := gen.ReturnListings(db, query.Origin, query.Destination, query.Date+" "+query.Time) if err != nil { return err } // Convert date to be human readable query.Date, query.Time, err = util.ReturnTimeString(true, query.Date, query.Time) if err != nil { return err } body := &gen.ListingsHTML{ Filter: cities, Listings: listings, Query: query, } page := struct { Header gen.HeaderHTML Body gen.ListingsHTML }{ *header, *body, } err = templates.ExecuteTemplate(w, "listings.html", page) if err != nil { return util.NewError(err, "No se cargó la página", 500) } return nil }
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 }
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 }
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 }