-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
95 lines (77 loc) · 2.52 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package app
import (
"database/sql"
"net/http"
"strconv"
"github.com/go-playground/pure"
"github.com/luizdepra/go-rest-api/app/repository"
"github.com/luizdepra/go-rest-api/app/route"
)
// App holds common API structures.
type App struct {
Router *pure.Mux
Repository *repository.TaskRepository
}
// New creates and returns a new App instance.
func New(router *pure.Mux, db *sql.DB) *App {
repo := repository.New(db)
return &App{
Router: router,
Repository: repo,
}
}
// RegisterRoutes registers all availlable routes into the router.
func (app *App) RegisterRoutes() {
app.Router.Get("/", app.GetRoot)
tasks := app.Router.Group("/tasks")
tasks.Get("/", app.ListTasks)
tasks.Post("/", app.CreateTask)
tasks.Get("/:id/", app.GetTask)
tasks.Put("/:id/", app.UpdateTask)
tasks.Delete("/:id/", app.DeleteTask)
}
// parseID parses the Id from the Request.
func (app *App) parseID(request *http.Request) (int64, error) {
requestVars := pure.RequestVars(request)
idParam := requestVars.URLParam("id")
return strconv.ParseInt(idParam, 10, 64)
}
// GetRoot warps the RootHandler.
func (app *App) GetRoot(writer http.ResponseWriter, request *http.Request) {
route.RootHandler(writer, request)
}
// ListTasks wraps the ListTasksHandler.
func (app *App) ListTasks(writer http.ResponseWriter, request *http.Request) {
route.ListTasksHandler(app.Repository, writer, request)
}
// CreateTask wraps the CreateTaskHandler.
func (app *App) CreateTask(writer http.ResponseWriter, request *http.Request) {
route.CreateTaskHandler(app.Repository, writer, request)
}
// GetTask wraps the GetTaskHandler.
func (app *App) GetTask(writer http.ResponseWriter, request *http.Request) {
id, err := app.parseID(request)
if err != nil {
route.MakeJSONErrorResponse(writer, http.StatusBadRequest, "invalid id value")
return
}
route.GetTaskHandler(app.Repository, writer, request, id)
}
// UpdateTask wraps the UpdateTaskHandler.
func (app *App) UpdateTask(writer http.ResponseWriter, request *http.Request) {
id, err := app.parseID(request)
if err != nil {
route.MakeJSONErrorResponse(writer, http.StatusBadRequest, "invalid id value")
return
}
route.UpdateTaskHandler(app.Repository, writer, request, id)
}
// DeleteTask wraps the DeleteTaskHandler.
func (app *App) DeleteTask(writer http.ResponseWriter, request *http.Request) {
id, err := app.parseID(request)
if err != nil {
route.MakeJSONErrorResponse(writer, http.StatusBadRequest, "invalid id value")
return
}
route.DeleteTaskHandler(app.Repository, writer, request, id)
}