59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package routing
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
lishwist "lishwist/core"
|
|
"lishwist/http/env"
|
|
"lishwist/http/response"
|
|
|
|
"github.com/Teajey/rsvp"
|
|
)
|
|
|
|
type HomeProps struct {
|
|
Username string
|
|
Gifts []lishwist.Wish
|
|
Todo []lishwist.Wish
|
|
Reference string
|
|
HostUrl string
|
|
Groups []lishwist.Group
|
|
}
|
|
|
|
func Home(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
|
gifts, err := app.GetWishes()
|
|
if err != nil {
|
|
log.Printf("Failed to get gifts: %s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "An error occurred while fetching your wishlist :(")
|
|
}
|
|
user := app.User()
|
|
todo, err := user.GetTodo()
|
|
if err != nil {
|
|
log.Printf("Failed to get todo: %s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "An error occurred while fetching your wishlist :(")
|
|
}
|
|
groups, err := app.GetGroups()
|
|
if err != nil {
|
|
log.Printf("Failed to get groups: %s\n", err)
|
|
return response.Error(http.StatusInternalServerError, "An error occurred while fetching your wishlist :(")
|
|
}
|
|
p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.Configuration.HostUrl, Groups: groups}
|
|
return response.Data("home.gotmpl", p)
|
|
}
|
|
|
|
func HomePost(app *lishwist.Session, session *response.Session, h http.Header, r *http.Request) rsvp.Response {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return response.Error(http.StatusBadRequest, "Failed to parse form")
|
|
}
|
|
|
|
switch r.Form.Get("intent") {
|
|
case "add_idea":
|
|
return WishlistAdd(app, h, r)
|
|
case "delete_idea":
|
|
return WishlistDelete(app, h, r)
|
|
default:
|
|
return TodoUpdate(app, h, r)
|
|
}
|
|
}
|