func AddObservationHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() o := new(Observation) decode := schema.NewDecoder() // Fill the struct with the form data decode.Decode(o, r.Form) id, err := strconv.Atoi(r.FormValue("observationPointId")) if err == nil { o.Point = observationpoints[id] } // check if valid if o.Point == nil || (r.FormValue("Celsius") != "0" && o.Celsius == 0) { http.Redirect(w, r, "/app/observation", http.StatusFound) return } o.Timestamp = time.Now() o.Id = observationcounter.Get() observations = append(observations, o) http.Redirect(w, r, "/app/observation", 302) }
func decodePerson(r *http.Request) *person.Person { // Decode the form data and add the resulting Person type to the Profile. p := &person.Person{} decoder := schema.NewDecoder() decoder.Decode(p, r.Form) return p }
func AddPointHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() o := new(ObservationPoint) decode := schema.NewDecoder() decode.Decode(o, r.Form) if !o.IsValid(r) { http.Redirect(w, r, "/app/observationpoint", http.StatusFound) return } o.Id = pointcounter.Get() observationpoints = append(observationpoints, o) http.Redirect(w, r, "/app/observationpoint", 303) }
// Authenticate process the request and returns a populated Profile. // If the Authenticate method can not authenticate the User based on the // request, an error or a redirect URL wll be return. func (p *Provider) Authenticate(w http.ResponseWriter, r *http.Request) ( up *profile.Profile, url string, err error) { up = profile.New(p.Name, p.URL) // Add the User's Unique ID. If an ID is not provided make this // value "default" up.ID = r.FormValue("ID") if up.ID == "" { up.ID = "default" } // Decode the form data and add the resulting Person type to the Profile. per := &person.Person{} decoder := schema.NewDecoder() decoder.Decode(per, r.Form) up.Person = per return up, "", nil }
func TaskHandler(w http.ResponseWriter, r *http.Request) { PreventCaching(w) if r.Method == "GET" { t, _ := template.ParseFiles("form.jsp") t.Execute(w, nil) return } r.ParseForm() task := NewTask() decode := schema.NewDecoder() decode.Decode(task, r.Form) // Käytännössä W04E07 tehtävä on vain siinä onko edessä sana 'go' vai ei :) go sendForProcessing(task) http.Redirect(w, r, fmt.Sprintf("/%s", task.Id), 302) }
func OrderHandler(w http.ResponseWriter, r *http.Request) { PreventCaching(w) session, _ := store.Get(r, "session-stuff") if r.Method == "GET" { session.Values["possibleitems"] = itemlist Reply("form.html", w, r, session) return } r.ParseForm() // Fill the order struct order := NewOrder() decode := schema.NewDecoder() decode.Decode(order, r.Form) order.ParseItems(r) errlist := order.Check() if errlist != nil { // Add flash warnings for _, err := range errlist { session.AddFlash(err.Error()) } // Remember the previous form data session.Values["Name"] = order.Name session.Values["Address"] = order.Address for _, v := range order.Items { session.Values[v] = "checked" } session.Save(r, w) http.Redirect(w, r, "/order", 302) return } AddOrder(order) session.AddFlash("Thanks for your order") session.Save(r, w) http.Redirect(w, r, fmt.Sprintf("/order/%s", order.Id), 302) }
func RegisterHandler(w http.ResponseWriter, r *http.Request) { PreventCaching(w) session, _ := store.Get(r, "session-stuff") if r.Method == "GET" { Reply("form.html", w, r, session) return } r.ParseForm() // Create new Registration and parse the form into it reg := new(Registration) decode := schema.NewDecoder() decode.Decode(reg, r.Form) errlist := reg.Check() if errlist != nil { for _, err := range errlist { session.AddFlash(err.Error()) } session.Save(r, w) http.Redirect(w, r, "/registration", 302) return } if ContainsRegistration(reg) { session.AddFlash(alreadyThereError.Error()) session.Save(r, w) http.Redirect(w, r, "/registration", 302) return } AddRegistration(reg) session.AddFlash("Thanks for registration") session.Save(r, w) http.Redirect(w, r, "/registration", 302) }
When handler invokers for these method descriptors are called parameters and values will be filled: 1. Parameters: *MyController, http.ResponseWriter, *http.Request, Controller, Action. 2. Values: ( url.Values ), extracted from URL and form data. For the first method, only parameters are used. For the second one, there is an argument (*Action2Input) which is not listed in parameters, so an empty Action2Input object would be created and filled using form data and URL. Here the term 'Filled' means that 'Name' field of the created Action2Input struct would be set to a value with the same name (URL value or form value "Name"). */ var ( decoder = schema.NewDecoder() ) func decode(input interface{}, values url.Values) { removeQuotesFromStringValues(values) decoder.Decode(input, values) } // removeQuotesFromStringValues is used to remove quotes from strings that are added // when javascript strings parameters are passed. func removeQuotesFromStringValues(values url.Values) { for _, vals := range values { for i, val := range vals { if len(val) > 1 && val[0] == '"' && val[len(val)-1] == '"' { vals[i] = val[1 : len(val)-1] }