/
winners.go
116 lines (98 loc) · 2.97 KB
/
winners.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2023 Bill Nixon. All rights reserved.
// Use of this source code is governed by the license found in the LICENSE file.
package main
import (
"net/http"
"time"
"github.com/bnixon67/webapp/webhandler"
"github.com/bnixon67/webapp/weblogin"
"github.com/bnixon67/webapp/webutil"
)
// Winner represents current winners.
type Winner struct {
ID int
Title string
Artist string
CurrentBid float64
Modified time.Time
ModifiedBy string
Email string
FullName string
}
// WinnerPageData holds the data to be passed to the winners page template.
type WinnerPageData struct {
Title string
User weblogin.User
Winners []Winner
}
// WinnerHandler handles requests for the winners page.
func (app *BidApp) WinnerHandler(w http.ResponseWriter, r *http.Request) {
// Get logger with request info and function name.
logger := webhandler.GetRequestLoggerWithFunc(r)
// Check if the HTTP method is valid.
if !webutil.ValidMethod(w, r, http.MethodGet) {
// Method not allowed. Response w updated appropriately.
logger.Error("invalid method")
return
}
// Get the user from the request.
user, err := app.DB.GetUserFromRequest(w, r)
if err != nil {
logger.Error("failed to GetUser", "err", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// Retrieve the list of winners from the database.
winners, err := app.BidDB.GetWinners()
if err != nil {
logger.Error("failed to GetWinners", "err", err)
}
// Render page.
err = webutil.RenderTemplate(app.Tmpl, w, "winners.html",
WinnerPageData{
Title: app.Cfg.Name,
User: user,
Winners: winners,
})
if err != nil {
logger.Error("unable to render page", "err", err)
return
}
logger.Info("displayed winners", "winners", len(winners))
}
// WinnersCSVHandler provides list of the current users as a CSV file.
func (app *BidApp) WinnersCSVHandler(w http.ResponseWriter, r *http.Request) {
// Get logger with request info and function name.
logger := webhandler.GetRequestLoggerWithFunc(r)
// Check if the HTTP method is valid.
if !webutil.ValidMethod(w, r, http.MethodGet) {
logger.Error("invalid method")
return
}
user, err := app.DB.GetUserFromRequest(w, r)
if err != nil {
logger.Error("failed to GetUser", "err", err)
webutil.HttpError(w, http.StatusInternalServerError)
return
}
if !user.IsAdmin {
logger.Error("user not authorized", "user", user)
webutil.HttpError(w, http.StatusUnauthorized)
return
}
winners, err := app.BidDB.GetWinners()
if err != nil {
logger.Error("failed to get winners", "err", err)
webutil.HttpError(w, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", "attachment;filename=winners.csv")
err = webutil.SliceOfStructsToCSV(w, winners)
if err != nil {
logger.Error("failed to convert struct to CSV",
"err", err, "winners", winners)
webutil.HttpError(w, http.StatusInternalServerError)
return
}
}