-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.go
45 lines (39 loc) · 1.09 KB
/
report.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
package handler
import (
"bytes"
"io"
"net/http"
"strings"
"github.com/kerti/idcra-api/model"
"github.com/kerti/idcra-api/service"
uuid "github.com/satori/go.uuid"
)
func SurveyReport() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-type", "application/octet-stream")
w.Header().Set("Content-type", "application/pdf")
ctx := r.Context()
filename := strings.TrimPrefix(r.URL.Path, "/reports/surveys/")
idString := strings.Replace(filename, ".pdf", "", -1)
id, err := uuid.FromString(idString)
if err != nil {
response := &model.Response{
Code: http.StatusBadRequest,
Error: err.Error(),
}
writeResponse(w, response, response.Code)
return
}
reportData, err := ctx.Value("reportService").(*service.ReportService).GenerateSurveyPDF(id)
if err != nil {
response := &model.Response{
Code: http.StatusInternalServerError,
Error: err.Error(),
}
writeResponse(w, response, response.Code)
return
}
reportBytes := bytes.NewReader(reportData.Bytes())
io.Copy(w, reportBytes)
})
}