Compare commits

...

3 Commits

4 changed files with 37 additions and 27 deletions

View File

@ -36,15 +36,14 @@ func main() {
r := router.New(store)
r.Public.HandleFunc("GET /", routing.Login)
r.Public.HandleFunc("GET /group/{groupReference}", routing.PublicGroupPage)
r.Public.HandleFunc("GET /groups/{groupReference}", routing.PublicGroup)
r.Public.HandleFunc("GET /list/{userReference}", routing.PublicWishlist)
r.Public.HandleFunc("GET /register", routing.Register)
r.Public.HandleFunc("POST /", routing.LoginPost)
r.Public.HandleFunc("POST /register", routing.RegisterPost)
r.Private.HandleFunc("GET /", routing.NotFound)
r.Private.HandleFunc("GET /group/{groupReference}", routing.ExpectUser(routing.GroupPage))
r.Private.HandleFunc("GET /groups", routing.ExpectUser(routing.GroupsJson))
r.Private.HandleFunc("GET /groups", routing.ExpectUser(routing.Groups))
r.Private.HandleFunc("GET /groups/{groupReference}", routing.ExpectUser(routing.Group))
r.Private.HandleFunc("GET /list/{userReference}", routing.ExpectUser(routing.ForeignWishlist))
r.Private.HandleFunc("GET /users", routing.ExpectUser(routing.Users))
@ -56,6 +55,10 @@ func main() {
r.Private.HandleFunc("POST /users/{userReference}", routing.ExpectUser(routing.UserPost))
r.Private.HandleFunc("POST /{$}", routing.ExpectUser(routing.HomePost))
// Deprecated
r.Public.HandleFunc("GET /group/{groupReference}", routing.PublicGroup)
r.Private.HandleFunc("GET /group/{groupReference}", routing.ExpectUser(routing.Group))
http.Handle("/", r)
log.Printf("Running at http://127.0.0.1:%s\n", env.ServePort)

View File

@ -13,14 +13,37 @@ type GroupProps struct {
CurrentUsername string
}
func GroupPage(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
func AdminGroup(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
reference := r.PathValue("groupReference")
group, err := db.GetGroupByReference(reference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Couldn't get group: %s", err)
}
if group == nil {
return rsvp.Error(http.StatusNotFound, "Group not found")
}
if !currentUser.IsAdmin {
index := group.MemberIndex(currentUser.Id)
group.Members = slices.Delete(group.Members, index, index+1)
}
p := GroupProps{
Group: group,
CurrentUsername: currentUser.Name,
}
return rsvp.Data("group_page.gotmpl", p)
}
func Group(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
if currentUser.IsAdmin {
return AdminGroup(currentUser, h, r)
}
groupReference := r.PathValue("groupReference")
group, err := currentUser.GetGroupByReference(groupReference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "An error occurred while fetching this group :(").Log("Couldn't get group: %s", err)
}
if group == nil {
return rsvp.Error(http.StatusNotFound, "Group not found. (It might be because you're not a member)").Log("Couldn't get group: %s", err)
return rsvp.Error(http.StatusNotFound, "Group not found. (It might be because you're not a member)")
}
index := group.MemberIndex(currentUser.Id)
group.Members = slices.Delete(group.Members, index, index+1)
@ -31,7 +54,7 @@ func GroupPage(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Respon
return rsvp.Data("group_page.gotmpl", p)
}
func PublicGroupPage(h http.Header, r *rsvp.Request) rsvp.Response {
func PublicGroup(h http.Header, r *rsvp.Request) rsvp.Response {
groupReference := r.PathValue("groupReference")
group, err := db.GetGroupByReference(groupReference)
if err != nil {
@ -59,13 +82,13 @@ func GroupPost(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Respon
if name != "" {
createdGroup, err := db.CreateGroup(name, reference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to create group: %w", err)
return rsvp.Error(http.StatusInternalServerError, "Failed to create group: %s", err)
}
group = createdGroup
} else {
existingGroup, err := db.GetGroupByReference(reference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Failed to get group: %w", err)
return rsvp.Error(http.StatusInternalServerError, "Failed to get group: %s", err)
}
if existingGroup == nil {
return rsvp.Error(http.StatusNotFound, "Group not found", err)
@ -103,7 +126,7 @@ func GroupPost(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Respon
return rsvp.Data("", group)
}
func GroupsJson(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
func Groups(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
if !currentUser.IsAdmin {
return NotFound(h, r)
}
@ -115,20 +138,3 @@ func GroupsJson(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Respo
return rsvp.Data("", groups)
}
func Group(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Response {
if !currentUser.IsAdmin {
return NotFound(h, r)
}
groupReference := r.PathValue("groupReference")
group, err := db.GetGroupByReference(groupReference)
if err != nil {
return rsvp.Error(http.StatusInternalServerError, "Couldn't get group: %s", err)
}
if group == nil {
return rsvp.Error(http.StatusNotFound, "Group not found.")
}
return rsvp.Data("", group)
}

View File

@ -39,6 +39,7 @@ func LoginPost(h http.Header, r *rsvp.Request) rsvp.Response {
props := api.Login(username, password)
if props != nil {
session.FlashSet(&props, "login_props")
return rsvp.SeeOther("/").SaveSession(session)
}

View File

@ -99,7 +99,7 @@
<ul class="list-group">
{{range .}}
<li class="list-group-item">
<a href="/group/{{.Reference}}">{{.Name}}</a>
<a href="/groups/{{.Reference}}">{{.Name}}</a>
</li>
{{end}}
</ul>