Compare commits
3 Commits
2940ffa915
...
b471a2e084
| Author | SHA1 | Date |
|---|---|---|
|
|
b471a2e084 | |
|
|
e9a8b7b020 | |
|
|
8175812e48 |
|
|
@ -36,15 +36,14 @@ func main() {
|
||||||
r := router.New(store)
|
r := router.New(store)
|
||||||
|
|
||||||
r.Public.HandleFunc("GET /", routing.Login)
|
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 /list/{userReference}", routing.PublicWishlist)
|
||||||
r.Public.HandleFunc("GET /register", routing.Register)
|
r.Public.HandleFunc("GET /register", routing.Register)
|
||||||
r.Public.HandleFunc("POST /", routing.LoginPost)
|
r.Public.HandleFunc("POST /", routing.LoginPost)
|
||||||
r.Public.HandleFunc("POST /register", routing.RegisterPost)
|
r.Public.HandleFunc("POST /register", routing.RegisterPost)
|
||||||
|
|
||||||
r.Private.HandleFunc("GET /", routing.NotFound)
|
r.Private.HandleFunc("GET /", routing.NotFound)
|
||||||
r.Private.HandleFunc("GET /group/{groupReference}", routing.ExpectUser(routing.GroupPage))
|
r.Private.HandleFunc("GET /groups", routing.ExpectUser(routing.Groups))
|
||||||
r.Private.HandleFunc("GET /groups", routing.ExpectUser(routing.GroupsJson))
|
|
||||||
r.Private.HandleFunc("GET /groups/{groupReference}", routing.ExpectUser(routing.Group))
|
r.Private.HandleFunc("GET /groups/{groupReference}", routing.ExpectUser(routing.Group))
|
||||||
r.Private.HandleFunc("GET /list/{userReference}", routing.ExpectUser(routing.ForeignWishlist))
|
r.Private.HandleFunc("GET /list/{userReference}", routing.ExpectUser(routing.ForeignWishlist))
|
||||||
r.Private.HandleFunc("GET /users", routing.ExpectUser(routing.Users))
|
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 /users/{userReference}", routing.ExpectUser(routing.UserPost))
|
||||||
r.Private.HandleFunc("POST /{$}", routing.ExpectUser(routing.HomePost))
|
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)
|
http.Handle("/", r)
|
||||||
|
|
||||||
log.Printf("Running at http://127.0.0.1:%s\n", env.ServePort)
|
log.Printf("Running at http://127.0.0.1:%s\n", env.ServePort)
|
||||||
|
|
|
||||||
|
|
@ -13,14 +13,37 @@ type GroupProps struct {
|
||||||
CurrentUsername string
|
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")
|
groupReference := r.PathValue("groupReference")
|
||||||
group, err := currentUser.GetGroupByReference(groupReference)
|
group, err := currentUser.GetGroupByReference(groupReference)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rsvp.Error(http.StatusInternalServerError, "An error occurred while fetching this group :(").Log("Couldn't get group: %s", err)
|
return rsvp.Error(http.StatusInternalServerError, "An error occurred while fetching this group :(").Log("Couldn't get group: %s", err)
|
||||||
}
|
}
|
||||||
if group == nil {
|
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)
|
index := group.MemberIndex(currentUser.Id)
|
||||||
group.Members = slices.Delete(group.Members, index, index+1)
|
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)
|
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")
|
groupReference := r.PathValue("groupReference")
|
||||||
group, err := db.GetGroupByReference(groupReference)
|
group, err := db.GetGroupByReference(groupReference)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -59,13 +82,13 @@ func GroupPost(currentUser *db.User, h http.Header, r *rsvp.Request) rsvp.Respon
|
||||||
if name != "" {
|
if name != "" {
|
||||||
createdGroup, err := db.CreateGroup(name, reference)
|
createdGroup, err := db.CreateGroup(name, reference)
|
||||||
if err != nil {
|
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
|
group = createdGroup
|
||||||
} else {
|
} else {
|
||||||
existingGroup, err := db.GetGroupByReference(reference)
|
existingGroup, err := db.GetGroupByReference(reference)
|
||||||
if err != nil {
|
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 {
|
if existingGroup == nil {
|
||||||
return rsvp.Error(http.StatusNotFound, "Group not found", err)
|
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)
|
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 {
|
if !currentUser.IsAdmin {
|
||||||
return NotFound(h, r)
|
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)
|
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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ func LoginPost(h http.Header, r *rsvp.Request) rsvp.Response {
|
||||||
|
|
||||||
props := api.Login(username, password)
|
props := api.Login(username, password)
|
||||||
if props != nil {
|
if props != nil {
|
||||||
|
session.FlashSet(&props, "login_props")
|
||||||
return rsvp.SeeOther("/").SaveSession(session)
|
return rsvp.SeeOther("/").SaveSession(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@
|
||||||
<ul class="list-group">
|
<ul class="list-group">
|
||||||
{{range .}}
|
{{range .}}
|
||||||
<li class="list-group-item">
|
<li class="list-group-item">
|
||||||
<a href="/group/{{.Reference}}">{{.Name}}</a>
|
<a href="/groups/{{.Reference}}">{{.Name}}</a>
|
||||||
</li>
|
</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue