31 lines
744 B
Go
31 lines
744 B
Go
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)
|
|
}
|
|
}
|