167 lines
4.5 KiB
Go
167 lines
4.5 KiB
Go
package routing
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"lishwist/db"
|
|
"lishwist/error"
|
|
"lishwist/templates"
|
|
)
|
|
|
|
type GroupProps struct {
|
|
Name string
|
|
Members []db.User
|
|
CurrentUsername string
|
|
}
|
|
|
|
func (ctx *Context) GroupPage(user *db.User, w http.ResponseWriter, r *http.Request) {
|
|
groupReference := r.PathValue("groupReference")
|
|
group, err := user.GetGroupByReference(groupReference)
|
|
if err != nil {
|
|
error.Page(w, "An error occurred while fetching this group :(", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
if group == nil {
|
|
error.Page(w, "Group not found. (It might be because you're not a member)", http.StatusNotFound, nil)
|
|
return
|
|
}
|
|
peers, err := user.GetPeers(group.Id)
|
|
if err != nil {
|
|
error.Page(w, "An error occurred while fetching this group :(", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
p := GroupProps{
|
|
Name: group.Name,
|
|
Members: peers,
|
|
CurrentUsername: user.Name,
|
|
}
|
|
templates.Execute(w, "group_page.gotmpl", p)
|
|
}
|
|
|
|
func (ctx *Context) PublicGroupPage(w http.ResponseWriter, r *http.Request) {
|
|
groupReference := r.PathValue("groupReference")
|
|
group, err := db.GetGroupByReference(groupReference)
|
|
if err != nil {
|
|
error.Page(w, "An error occurred while fetching this group :(", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
members, err := group.GetMembers()
|
|
if err != nil {
|
|
error.Page(w, "An error occurred while fetching this group :(", http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
p := GroupProps{
|
|
Name: group.Name,
|
|
Members: members,
|
|
}
|
|
templates.Execute(w, "public_group_page.gotmpl", p)
|
|
}
|
|
|
|
func (ctx *Context) GroupPost(currentUser *db.User, w http.ResponseWriter, r *http.Request) {
|
|
if !currentUser.IsAdmin {
|
|
NotFoundJson(w, r)
|
|
return
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Failed to parse form: "+err.Error())
|
|
return
|
|
}
|
|
|
|
var group *db.Group
|
|
|
|
reference := r.PathValue("groupReference")
|
|
name := r.Form.Get("name")
|
|
addUsers := r.Form["addUser"]
|
|
removeUsers := r.Form["removeUser"]
|
|
|
|
if name != "" {
|
|
createdGroup, err := db.CreateGroup(name, reference)
|
|
if err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Failed to create group: "+err.Error())
|
|
return
|
|
}
|
|
group = createdGroup
|
|
} else {
|
|
existingGroup, err := db.GetGroupByReferenceWithMembers(reference)
|
|
if err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Failed to get group: "+err.Error())
|
|
return
|
|
}
|
|
if existingGroup == nil {
|
|
writeGeneralErrorJson(w, http.StatusNotFound, "Group not found")
|
|
return
|
|
}
|
|
group = existingGroup
|
|
|
|
for _, userId := range removeUsers {
|
|
index := group.MemberIndex(userId)
|
|
if index == -1 {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Group %q does not contain a user with id %s", reference, userId)
|
|
return
|
|
}
|
|
err = group.RemoveUser(userId)
|
|
if err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "On group %q failed to remove user with id %s: %s", reference, userId, err)
|
|
return
|
|
}
|
|
group.Users = slices.Delete(group.Users, index, index)
|
|
}
|
|
}
|
|
|
|
for _, userId := range addUsers {
|
|
user, err := db.GetUser(userId)
|
|
if err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Groups exists, but a user with id %s could not be fetched: %s", userId, err)
|
|
return
|
|
}
|
|
if user == nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Groups exists, but a user with id %s does not exist", userId)
|
|
return
|
|
}
|
|
err = group.AddUser(user.Id)
|
|
if err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Groups exists, but failed to add user with id %s: %s", userId, err)
|
|
return
|
|
}
|
|
group.Users = append(group.Users, *currentUser)
|
|
}
|
|
|
|
_ = json.NewEncoder(w).Encode(group)
|
|
}
|
|
|
|
func (ctx *Context) GroupsJson(user *db.User, w http.ResponseWriter, r *http.Request) {
|
|
if !user.IsAdmin {
|
|
NotFoundJson(w, r)
|
|
return
|
|
}
|
|
|
|
groups, err := db.GetAllGroups()
|
|
if err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Failed to get groups: "+err.Error())
|
|
return
|
|
}
|
|
|
|
_ = json.NewEncoder(w).Encode(groups)
|
|
}
|
|
|
|
func (ctx *Context) Group(user *db.User, w http.ResponseWriter, r *http.Request) {
|
|
if !user.IsAdmin {
|
|
NotFoundJson(w, r)
|
|
return
|
|
}
|
|
groupReference := r.PathValue("groupReference")
|
|
group, err := db.GetGroupByReferenceWithMembers(groupReference)
|
|
if err != nil {
|
|
writeGeneralErrorJson(w, http.StatusBadRequest, "Couldn't get group: %s", err)
|
|
return
|
|
}
|
|
if group == nil {
|
|
writeGeneralErrorJson(w, http.StatusNotFound, "Group not found.")
|
|
return
|
|
}
|
|
|
|
_ = json.NewEncoder(w).Encode(group)
|
|
}
|