aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/incoming/web/handlers/group.go
blob: 32014918a0c6b41bf254256b4b277a2c019d9982 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

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
	}
}