-
Notifications
You must be signed in to change notification settings - Fork 31
/
todo.go
65 lines (55 loc) · 1.62 KB
/
todo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package todo
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/tonyalaribe/todoapi/withmongodb/internal/config"
)
type Config struct {
*config.Config
}
func New(configuration *config.Config) *Config {
return &Config{configuration}
}
func (config *Config) Routes() *chi.Mux {
router := chi.NewRouter()
router.Get("/{todoID}", config.GetATodo)
router.Delete("/{todoID}", config.DeleteTodo)
router.Post("/", config.CreateTodo)
router.Get("/", config.GetAllTodos)
return router
}
type Todo struct {
Slug string `json:"slug"`
Title string `json:"title"`
Body string `json:"body"`
}
func (config *Config) GetATodo(w http.ResponseWriter, r *http.Request) {
todoID := chi.URLParam(r, "todoID")
todos := Todo{
Slug: todoID,
Title: "Hello world",
Body: "Heloo world from planet earth",
}
render.JSON(w, r, todos) // A chi router helper for serializing and returning json
}
func (config *Config) DeleteTodo(w http.ResponseWriter, r *http.Request) {
response := make(map[string]string)
response["message"] = "Deleted TODO successfully"
render.JSON(w, r, response) // Return some demo response
}
func (config *Config) CreateTodo(w http.ResponseWriter, r *http.Request) {
response := make(map[string]string)
response["message"] = "Created TODO successfully"
render.JSON(w, r, response) // Return some demo response
}
func (config *Config) GetAllTodos(w http.ResponseWriter, r *http.Request) {
todos := []Todo{
{
Slug: "slug",
Title: "Hello world",
Body: "Heloo world from planet earth",
},
}
render.JSON(w, r, todos) // A chi router helper for serializing and returning json
}