-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
56 lines (49 loc) · 1.08 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
package main
import (
"context"
"net/url"
m "share-Gutenberg/models"
s "share-Gutenberg/services"
"strconv"
"strings"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// HERE ITS A GOOD PLACE TO SET CONTROLLERS
func (a *App) GetBooks(params map[string]string) (*m.Gutendex, error) {
paramsDone := url.Values{}
for key, value := range params {
if value != "" {
paramsDone.Set(key, value)
}
}
return s.BooksFetcher(paramsDone)
}
func (a *App) GetBook(id string) (*m.Book, *m.Err) {
if _, err := strconv.ParseInt(strings.Trim(id, " "), 10, 0); err != nil {
return nil, &m.Err{
Error: err,
Message: "expected a number for id",
Status: 400,
}
}
book, err := s.BookFetcher(id)
if err != nil {
return nil, err
}
return book, nil
}
func (a *App) GetBookFile(bi m.BookFileInfo) *m.Err {
return s.GetBookFile(bi)
}