lishwist/server/routing/home.go

58 lines
1.5 KiB
Go

package routing
import (
"net/http"
"lishwist/db"
"lishwist/env"
"lishwist/error"
"lishwist/templates"
)
type HomeProps struct {
Username string
Gifts []db.Gift
Todo []db.Gift
Reference string
HostUrl string
Groups []db.Group
}
func (ctx *Context) Home(currentUser *db.User, w http.ResponseWriter, r *http.Request) {
gifts, err := currentUser.GetGifts()
if err != nil {
error.Page(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError, err)
return
}
todo, err := currentUser.GetTodo()
if err != nil {
error.Page(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError, err)
return
}
groups, err := currentUser.GetGroups()
if err != nil {
error.Page(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError, err)
return
}
p := HomeProps{Username: currentUser.Name, Gifts: gifts, Todo: todo, Reference: currentUser.Reference, HostUrl: env.HostUrl.String(), Groups: groups}
templates.Execute(w, "home.gotmpl", p)
}
func (ctx *Context) HomePost(currentUser *db.User, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "Couldn't parse form", http.StatusBadRequest)
return
}
switch r.Form.Get("intent") {
case "add_idea":
ctx.WishlistAdd(currentUser, w, r)
return
case "delete_idea":
ctx.WishlistDelete(currentUser, w, r)
return
default:
ctx.TodoUpdate(currentUser, w, r)
return
}
}