package handlers import ( "log/slog" "net/http" "strconv" "go.lindenii.runxiyu.org/forge/forged/internal/database/queries" "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates" wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" ) type GroupHTTP struct { r templates.Renderer } func NewGroupHTTP(r templates.Renderer) *GroupHTTP { return &GroupHTTP{ r: r, } } func (h *GroupHTTP) Index(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) { base := wtypes.Base(r) userID, err := strconv.ParseInt(base.UserID, 10, 64) if err != nil { userID = 0 } queryParams := queries.GetGroupByPathParams{ Column1: base.URLSegments, UserID: userID, } p, err := base.Queries.GetGroupByPath(r.Context(), queryParams) if err != nil { slog.Error("failed to get group ID by path", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } subgroups, err := base.Queries.GetSubgroups(r.Context(), &p.ID) if err != nil { slog.Error("failed to get subgroups", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) // TODO: gracefully fail this part of the page } repos, err := base.Queries.GetReposInGroup(r.Context(), p.ID) if err != nil { slog.Error("failed to get repos in group", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) // TODO: gracefully fail this part of the page } err = h.r.Render(w, "group", struct { BaseData *wtypes.BaseData Subgroups []queries.GetSubgroupsRow Repos []queries.GetReposInGroupRow Description string DirectAccess bool }{ BaseData: base, Subgroups: subgroups, Repos: repos, Description: p.Description, DirectAccess: p.HasRole, }) if err != nil { slog.Error("failed to render index page", "error", err) } } func (h *GroupHTTP) Post(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) { base := wtypes.Base(r) userID, err := strconv.ParseInt(base.UserID, 10, 64) if err != nil { userID = 0 } queryParams := queries.GetGroupByPathParams{ Column1: base.URLSegments, UserID: userID, } p, err := base.Queries.GetGroupByPath(r.Context(), queryParams) if err != nil { slog.Error("failed to get group ID by path", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } if !p.HasRole { http.Error(w, "You do not have the necessary permissions to create repositories in this group.", http.StatusForbidden) return } }