fixup
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"nestly/internal/models"
|
||||
)
|
||||
|
||||
// homeView 首頁的模板資料;未登入時 Account 為 nil。
|
||||
type homeView struct {
|
||||
Account *models.Account
|
||||
}
|
||||
|
||||
// HomeHandler 渲染首頁。
|
||||
type HomeHandler struct {
|
||||
deps Dependencies
|
||||
}
|
||||
|
||||
// Show 顯示首頁;持有有效 session 時一併帶出帳號資料。
|
||||
func (h *HomeHandler) Show(w http.ResponseWriter, r *http.Request) {
|
||||
view := homeView{}
|
||||
if uid, err := h.deps.Sessions.UserID(r); err == nil {
|
||||
if acct, err := h.deps.Accounts.FindByID(r.Context(), uid); err == nil {
|
||||
view.Account = acct
|
||||
}
|
||||
}
|
||||
if err := h.deps.Templates.Render(w, http.StatusOK, "home.html", view); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user