func selectRoute(r martini.Router, method string, h martini.Handler) { switch method { case "GET": r.Get("/", h) case "PATCH": r.Patch("/", h) case "POST": r.Post("/", h) case "PUT": r.Put("/", h) case "DELETE": r.Delete("/", h) case "OPTIONS": r.Options("/", h) case "HEAD": r.Head("/", h) default: panic("bad method") } }
func setupRoutes(r martini.Router) { // static r.Get("/", func(r render.Render) { r.HTML(http.StatusOK, "index", View{Title: "A browser-based Todo.txt application"}) }) r.NotFound(func(r render.Render) { r.HTML(http.StatusNotFound, "404", View{Title: "404 - Not Found"}) }) // api - for tasks r.Get("/api/tasks", func(tasks todo.TaskList, r render.Render) { r.JSON(http.StatusOK, tasks) }) r.Get("/api/task/:id", func(tasks todo.TaskList, params martini.Params, r render.Render) { id, err := strconv.Atoi(params["id"]) if err != nil { r.Error(http.StatusInternalServerError) return } task, err := tasks.GetTask(id) if err != nil { r.Error(http.StatusNotFound) return } r.JSON(http.StatusOK, task) }) r.Post("/api/task", binding.Bind(todo.Task{}), func(newTask todo.Task, tasks todo.TaskList, config *Config, params martini.Params, r render.Render) { newTask.CreatedDate = time.Now() tasks.AddTask(&newTask) if err := tasks.WriteToFilename(config.TodoTxtFilename); err != nil { r.Error(http.StatusInternalServerError) return } r.JSON(http.StatusCreated, newTask) }) r.Put("/api/task/:id", binding.Bind(todo.Task{}), func(updatedTask todo.Task, tasks todo.TaskList, config *Config, params martini.Params, r render.Render) { id, err := strconv.Atoi(params["id"]) if err != nil { r.Error(http.StatusInternalServerError) return } currentTask, err := tasks.GetTask(id) if err != nil { r.Error(http.StatusNotFound) return } *currentTask = updatedTask if err := tasks.WriteToFilename(config.TodoTxtFilename); err != nil { r.Error(http.StatusInternalServerError) return } r.JSON(http.StatusOK, updatedTask) }) r.Delete("/api/task/:id", func(tasks todo.TaskList, config *Config, params martini.Params, r render.Render) { id, err := strconv.Atoi(params["id"]) if err != nil { r.Error(http.StatusInternalServerError) return } if err := tasks.RemoveTaskById(id); err != nil { r.Error(http.StatusNotFound) return } if err := tasks.WriteToFilename(config.TodoTxtFilename); err != nil { r.Error(http.StatusInternalServerError) return } r.JSON(http.StatusNoContent, `{}`) }) r.Delete("/api/tasks", func(tasks todo.TaskList, config *Config, r render.Render) { openTasks := make(todo.TaskList, 0) for _, task := range tasks { if !task.Completed { openTasks.AddTask(&task) } } if err := openTasks.WriteToFilename(config.TodoTxtFilename); err != nil { r.Error(http.StatusInternalServerError) return } r.JSON(http.StatusOK, openTasks) }) // api - for config file r.Get("/api/config", func(config *Config, r render.Render) { r.JSON(http.StatusOK, config) }) r.Put("/api/config", binding.Bind(Config{}), func(bindConfig Config, r render.Render) { if err := bindConfig.writeConfigurationFile(configFile); err != nil { r.Error(http.StatusInternalServerError) return } r.JSON(http.StatusOK, bindConfig) }) }