/
get_new_book.go
87 lines (68 loc) 路 1.68 KB
/
get_new_book.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
package rest
import (
"github.com/beauxarts/fedorov/data"
"github.com/beauxarts/scrinium/litres_integration"
"github.com/boggydigital/nod"
"net/http"
"strings"
)
type NewBookViewModel struct {
Id string
Title string
Authors []string
Downloads []*DownloadViewModel
}
type DownloadViewModel struct {
Id string
Filename string
Description string
}
func GetNewBook(w http.ResponseWriter, r *http.Request) {
// GET /new_book?id
id := r.URL.Query().Get("id")
var err error
if rdx, err = rdx.RefreshReader(); err != nil {
http.Error(w, nod.Error(err).Error(), http.StatusInternalServerError)
return
}
title := ""
if t, ok := rdx.GetLastVal(data.TitleProperty, id); ok {
title = t
}
var authors []string
if aus, err := authorsFullNames(id, rdx); err == nil {
authors = aus
}
kv, err := data.NewArtsReader(litres_integration.ArtsTypeFiles)
if err != nil {
http.Error(w, nod.Error(err).Error(), http.StatusInternalServerError)
return
}
artFiles, err := kv.ArtsFiles(id)
if err != nil {
http.Error(w, nod.Error(err).Error(), http.StatusInternalServerError)
return
}
nbvm := &NewBookViewModel{
Id: id,
Title: title,
Authors: authors,
}
for _, dt := range artFiles.DownloadsTypes() {
fn := dt.Filename
if ext := dt.Extension; ext != nil {
fn = strings.Replace(fn, "zip", *ext, 1)
}
dvm := &DownloadViewModel{
Id: id,
Filename: fn,
Description: dt.TypeDescription(),
}
nbvm.Downloads = append(nbvm.Downloads, dvm)
}
DefaultHeaders(w)
if err := tmpl.ExecuteTemplate(w, "new_book", nbvm); err != nil {
http.Error(w, nod.Error(err).Error(), http.StatusInternalServerError)
return
}
}