-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery.go
More file actions
87 lines (73 loc) · 2.15 KB
/
Copy pathgallery.go
File metadata and controls
87 lines (73 loc) · 2.15 KB
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
// 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 (
"fmt"
"net/http"
"time"
"github.com/bnixon67/webapp/webauth"
"github.com/bnixon67/webapp/webhandler"
"github.com/bnixon67/webapp/webutil"
)
// GalleryPageData contains data passed to the HTML template.
type GalleryPageData struct {
Title string
Message string
User webauth.User
Items []Item
}
// GalleryHandler displays a gallery of items.
func (app *BidApp) GalleryHandler(w http.ResponseWriter, r *http.Request) {
// Get logger with request info and function name.
logger := webhandler.RequestLoggerWithFuncName(r)
// Check if the HTTP method is valid.
if !webutil.IsMethodOrError(w, r, http.MethodGet) {
logger.Error("invalid method")
return
}
user, err := app.DB.UserFromRequest(w, r)
if err != nil {
logger.Error("failed to get user", "err", err)
webutil.RespondWithError(w, http.StatusInternalServerError)
return
}
if app.BidDB == nil {
logger.Error("database is nil")
webutil.RespondWithError(w, http.StatusInternalServerError)
return
}
items, err := app.BidDB.GetItems()
if err != nil {
logger.Error("failed to get items", "err", err)
webutil.RespondWithError(w, http.StatusInternalServerError)
return
}
layout := "Mon Jan 2, 2006 3:04 PM MST"
now := time.Now()
var message string
switch {
case now.Before(app.AuctionStart):
message = fmt.Sprintf("Auction opens %s",
app.AuctionStart.Format(layout))
case now.Before(app.AuctionEnd):
message = fmt.Sprintf("Auction closes %s",
app.AuctionEnd.Format(layout))
default:
message = fmt.Sprintf("Auction closed %s",
app.AuctionEnd.Format(layout))
}
logger.Info("GalleryHandler", "username", user.Username)
err = webutil.RenderTemplateOrError(app.Tmpl, w, "gallery.html",
GalleryPageData{
Title: app.Cfg.App.Name,
Message: message,
User: user,
Items: items,
})
if err != nil {
logger.Error("unable to render template", "err", err)
webutil.RespondWithError(w, http.StatusInternalServerError)
return
}
logger.Info("success", "username", user.Username, "items", len(items))
}