func handleProduct(service api.Service) { makeHandler("/product", func(w http.ResponseWriter, r *http.Request) { input := api.ProductInput{} err := decoder.Decode(&input, r.Form) if err != nil { fmt.Printf("%s\n", err) } productId := string("") if len(r.Form["productid"]) != 0 { productId = r.Form["productid"][0] } product, err := service.PutProduct(productId, input) if err != nil { fmt.Printf("%s\n", err) return } productBytes, err := json.Marshal(product) if err != nil { fmt.Printf("%s\n", err) return } fmt.Fprintf(w, string(productBytes)) }) }
func handleOrder(service api.Service) { makeHandler("/order", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": count, err := strconv.Atoi(r.Form["count"][0]) if err != nil { fmt.Printf("%s\n", err) return } _, err = service.PutOrder(r.Form["date"][0], r.Form["email"][0], r.Form["productid"][0], count) if err != nil { fmt.Printf("%s\n", err) return } fmt.Fprintf(w, "Save Successfully") case "DELETE": err := service.RemoveOrder(r.Form["date"][0], r.Form["email"][0], r.Form["productid"][0]) if err != nil { fmt.Printf("%s\n", err) return } fmt.Fprintf(w, "Delete Successfully") } }) }
func handleProfile(service api.Service) { makeHandler("/profile", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": input := api.UserInput{} decoder.Decode(&input, r.Form) _, err := service.PutUser(input.Email, input) if err != nil { fmt.Printf("%s\n", err) return } http.Redirect(w, r, "/order.html", http.StatusFound) case "DELETE": if err := service.RemoveUser(r.Form["email"][0]); err != nil { fmt.Printf("%s\n", err) return } } }) }