132 lines
3.6 KiB
Go
132 lines
3.6 KiB
Go
package routing
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"maps"
|
|
"net/http"
|
|
"slices"
|
|
|
|
lishwist "lishwist/core"
|
|
"lishwist/http/resources"
|
|
"lishwist/http/response"
|
|
"lishwist/http/session"
|
|
"lishwist/http/templates"
|
|
|
|
"github.com/Teajey/hmc"
|
|
"github.com/Teajey/rsvp"
|
|
)
|
|
|
|
func mapKeysIntersect(a, b map[string][]string) []string {
|
|
var out []string
|
|
for k := range a {
|
|
if _, ok := b[k]; ok {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func Home(app *lishwist.Session, session *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
|
|
w.DefaultTemplateName("home.gotmpl")
|
|
|
|
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),
|
|
}
|
|
}
|
|
navbar := templates.UserNavCollapse(user.Name, user.Reference, user.PasswordFromAdmin)
|
|
navbar.Links = nil
|
|
p := resources.HomeProps{
|
|
Namespace: hmc.NS(),
|
|
Navbar: navbar,
|
|
Wishlist: resources.InitialWishlist(user.Id, gifts),
|
|
Todo: resources.InitialTodo(todo),
|
|
GroupsList: resources.LinksList{
|
|
Label: "Your groups",
|
|
Links: groupLinks,
|
|
},
|
|
}
|
|
|
|
if r.Method == http.MethodGet {
|
|
return rsvp.Data(p)
|
|
}
|
|
|
|
err = r.ParseForm()
|
|
if err != nil {
|
|
p.Error = "Failed to parse form"
|
|
return rsvp.Data(p).StatusUnprocessableEntity()
|
|
}
|
|
|
|
if len(r.Form) < 1 {
|
|
p.Error = "No parameters provided"
|
|
return rsvp.Data(p).StatusBadRequest()
|
|
}
|
|
|
|
wishForm := maps.Clone(r.Form)
|
|
p.Wishlist.ExtractFormValues(wishForm)
|
|
p.Todo.ExtractFormValues(r.Form)
|
|
|
|
extra := mapKeysIntersect(wishForm, r.Form)
|
|
if len(extra) > 0 {
|
|
p.Warning = "Extraneous form values"
|
|
}
|
|
|
|
if p.Wishlist.Wishes.Elements.Intent.Value() == "delete" {
|
|
wishesToDelete := slices.Collect(p.Wishlist.Wishes.Elements.Select.Values())
|
|
err := app.RevokeWishes(wishesToDelete...)
|
|
if err != nil {
|
|
log.Printf("Failed to revoke wishes: %s\n", err)
|
|
p.Wishlist.Wishes.Elements.Intent.Error = "Something went wrong"
|
|
return rsvp.Data(p).StatusInternalServerError()
|
|
}
|
|
}
|
|
|
|
if p.Wishlist.NewWish.Elements.Value != "" {
|
|
_, err := app.MakeWish(p.Wishlist.NewWish.Elements.Value)
|
|
if err != nil {
|
|
log.Printf("Failed to make wish: %s\n", err)
|
|
p.Wishlist.NewWish.Elements.Error = "Something went wrong"
|
|
return rsvp.Data(p).StatusInternalServerError()
|
|
}
|
|
}
|
|
|
|
todosToChange := slices.Collect(p.Todo.Wishes.Elements.Select.Values())
|
|
|
|
if p.Todo.Wishes.Elements.Intent.Value() == "unclaim_todo" {
|
|
err := app.ClaimWishes([]string{}, todosToChange)
|
|
if err != nil {
|
|
log.Printf("Failed to unclaim wish: %s\n", err)
|
|
p.Todo.Wishes.Elements.Intent.Error = "Something went wrong"
|
|
return rsvp.Data(p).StatusInternalServerError()
|
|
}
|
|
} else if p.Todo.Wishes.Elements.Intent.Value() == "complete_todo" {
|
|
err := app.CompleteWishes(todosToChange)
|
|
if err != nil {
|
|
log.Printf("Failed to complete wish: %s\n", err)
|
|
p.Todo.Wishes.Elements.Intent.Error = "Something went wrong"
|
|
return rsvp.Data(p).StatusInternalServerError()
|
|
}
|
|
}
|
|
|
|
return rsvp.Data(p).StatusSeeOther(".")
|
|
}
|