func (ut UpdateTodo) ServeHTTP(w http.ResponseWriter, r *http.Request) { // TODO: validation tlID := mux.Vars(r)["tlid"] tID := mux.Vars(r)["tid"] name := r.FormValue("name") description := r.FormValue("description") status := r.FormValue("status") todo := &Todo{ TodoID: tID, Name: name, Description: description, Status: status, } fmt.Printf("\n%+v\n", *todo) err := ut.TodoListRepository.UpdateTodo(tlID, *todo) if err != nil { RespondErr(w, http.StatusInternalServerError, err.Error()) return } RespondData(w, "200") }
func (dtl DeleteTodo) ServeHTTP(w http.ResponseWriter, r *http.Request) { tlID := mux.Vars(r)["tlid"] tID := mux.Vars(r)["tid"] err := dtl.TodoListRepository.RemoveTodo(tlID, tID) if err != nil { RespondErr(w, http.StatusInternalServerError, err.Error()) return } RespondData(w, "200") }
func (gtl GetTodoListID) ServeHTTP(w http.ResponseWriter, r *http.Request) { tlID := mux.Vars(r)["id"] todoList, err := gtl.TodoListRepository.FetchTodoListsByID(tlID) if err != nil { RespondErr(w, http.StatusNotFound, err.Error()) return } data, err := json.Marshal(todoList) RespondData(w, string(data)) }
func main() { router := mux.NewRouter(nil) router.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "adljasldk") })) router.Get("/x", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "yyyy") })) router.Middlewares(HowLong, MiddlewareGenerator("one"), MiddlewareGenerator("two")).Group("/api", func(r mux.Router) { r.Get("/", HandlerGenerator("api:get")) r.Get("/user", HandlerGenerator("user:get")) r.Post("/user", HandlerGenerator("user:post")) r.Get("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] fmt.Fprintf(w, "Hello, %s!", id) })) }) http.ListenAndServe(":8080", router) }