70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package routing
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
lishwist "lishwist/core"
|
|
"lishwist/http/resources"
|
|
"lishwist/http/response"
|
|
"lishwist/http/session"
|
|
"lishwist/http/templates"
|
|
|
|
"github.com/Teajey/hmc"
|
|
"github.com/Teajey/rsvp"
|
|
)
|
|
|
|
func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
|
gifts, err := app.GetWishes()
|
|
if err != nil {
|
|
log.Printf("Failed to get gifts: %s\n", err)
|
|
return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError()
|
|
}
|
|
user := app.User()
|
|
todo, err := user.GetTodo()
|
|
if err != nil {
|
|
log.Printf("Failed to get todo: %s\n", err)
|
|
return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError()
|
|
}
|
|
groups, err := app.GetGroups()
|
|
if err != nil {
|
|
log.Printf("Failed to get groups: %s\n", err)
|
|
return response.Error("An error occurred while fetching your wishlist :(").StatusInternalServerError()
|
|
}
|
|
groupLinks := make([]hmc.Link, len(groups))
|
|
for i, g := range groups {
|
|
groupLinks[i] = hmc.Link{
|
|
Label: g.Name,
|
|
Href: fmt.Sprintf("groups/%s", g.Reference),
|
|
}
|
|
}
|
|
p := resources.HomeProps{
|
|
Namespace: hmc.NS(),
|
|
Navbar: templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin),
|
|
Wishlist: resources.InitialWishlist(user.Id, gifts),
|
|
Todo: resources.InitialTodo(todo),
|
|
GroupsList: resources.LinksList{
|
|
Label: "Your groups",
|
|
Links: groupLinks,
|
|
},
|
|
}
|
|
return response.Data("home.gotmpl", p)
|
|
}
|
|
|
|
func HomePost(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return response.Error("Failed to parse form").StatusBadRequest()
|
|
}
|
|
|
|
switch r.Form.Get("intent") {
|
|
case "add_idea":
|
|
return WishlistAdd(app, w, r)
|
|
case "delete_idea":
|
|
return WishlistDelete(app, w, r)
|
|
default:
|
|
return TodoUpdate(app, w, r)
|
|
}
|
|
}
|