~evanj/cms

ref: afc228cb59683180b3b2957aceb6bf1aa0bf0d0e cms/internal/c/space/space.go -rw-r--r-- 4.9 KiB
afc228cbEvan J Feat(db): Tweaking db.SetMaxOpenConns, db.SetMaxIdleConns, and 1 year, 19 days ago
                                                                                
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package space

import (
	"fmt"
	"log"
	"net/http"
	"strconv"
	"strings"

	"git.sr.ht/~evanj/errors"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/hook"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/v"
)

var (
	spaceHTML = v.MustParse("html/space.html")

	ErrNoSpace = errors.New("failed to find space")
)

type Space struct {
	*c.Controller
	log *log.Logger
	db  dber
}

type dber interface {
	UserGet(username, password string) (user.User, error)
	UserGetFromToken(token string) (user.User, error)
	SpaceNew(user user.User, name, desc string) (space.Space, error)
	SpaceGet(user user.User, spaceID string) (space.Space, error)
	SpaceUpdate(user user.User, space space.Space, name, desc string) (space.Space, error)
	SpaceCopy(user user.User, space space.Space, name, desc string) (space.Space, error)
	SpaceDelete(user user.User, space space.Space) error
	ContentTypesPerSpace(user user.User, space space.Space, before int) (contenttype.ContentTypeList, error)
	HooksPerSpace(user user.User, space space.Space, before int) (hook.HookList, error)
}

func New(c *c.Controller, log *log.Logger, db dber) *Space {
	return &Space{
		c,
		log,
		db,
	}
}

func (s *Space) serve(w http.ResponseWriter, r *http.Request) {
	spaceID := r.FormValue("space")
	parts := strings.Split(r.URL.Path, "/")
	if len(parts) > 2 && spaceID == "" {
		spaceID = parts[2]
	}

	user, err := s.GetCookieUser(w, r)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, err)
		return
	}

	space, err := s.db.SpaceGet(user, spaceID)
	if err != nil {
		s.Error(w, r, http.StatusNotFound, ErrNoSpace)
		return
	}

	beforect, _ := strconv.Atoi(r.URL.Query().Get("beforect"))
	cts, err := s.db.ContentTypesPerSpace(user, space, beforect)
	if err != nil {
		s.ErrorString(w, r, http.StatusInternalServerError, "failed to find contenttypes for space")
		return
	}

	beforehook, _ := strconv.Atoi(r.URL.Query().Get("beforehook"))
	hooks, err := s.db.HooksPerSpace(user, space, beforehook)
	if err != nil {
		s.ErrorString(w, r, http.StatusInternalServerError, "failed to find webhooks for space")
		return
	}

	s.HTML(w, r, spaceHTML, map[string]interface{}{
		"User":         user,
		"Space":        space,
		"ContentTypes": cts,
		"Hooks":        hooks,
	})
}

func (s *Space) create(w http.ResponseWriter, r *http.Request) {
	name := r.FormValue("name")
	desc := r.FormValue("desc")

	user, err := s.GetCookieUser(w, r)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, errors.Wrap(err, "can't create space"))
		return
	}

	space, err := s.db.SpaceNew(user, name, desc)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, err)
		return
	}

	url := fmt.Sprintf("/space/%s", space.ID())
	s.Redirect(w, r, url)
}

func (s *Space) copy(w http.ResponseWriter, r *http.Request) {
	spaceID := r.FormValue("space")
	name := r.FormValue("name")
	desc := r.FormValue("desc")

	user, err := s.GetCookieUser(w, r)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, errors.Wrap(err, "can't copy space"))
		return
	}

	spacePrev, err := s.db.SpaceGet(user, spaceID)
	if err != nil {
		s.Error(w, r, http.StatusNotFound, ErrNoSpace)
		return
	}

	spaceNext, err := s.db.SpaceCopy(user, spacePrev, name, desc)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, err)
		return
	}

	url := fmt.Sprintf("/space/%s", spaceNext.ID())
	s.Redirect(w, r, url)
}

func (s *Space) update(w http.ResponseWriter, r *http.Request) {
	spaceID := r.FormValue("space")
	name := r.FormValue("name")
	desc := r.FormValue("desc")

	user, err := s.GetCookieUser(w, r)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, errors.Wrap(err, "can't update space"))
		return
	}

	prev, err := s.db.SpaceGet(user, spaceID)
	if err != nil {
		s.Error(w, r, http.StatusNotFound, ErrNoSpace)
		return
	}

	next, err := s.db.SpaceUpdate(user, prev, name, desc)
	if err != nil {
		s.ErrorString(w, r, http.StatusInternalServerError, "failed to update space")
		return
	}

	url := fmt.Sprintf("/space/%s", next.ID())
	s.Redirect(w, r, url)
}

func (s *Space) delete(w http.ResponseWriter, r *http.Request) {
	spaceID := r.FormValue("space")

	user, err := s.GetCookieUser(w, r)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, errors.Wrap(err, "can't delete space"))
		return
	}

	space, err := s.db.SpaceGet(user, spaceID)
	if err != nil {
		s.Error(w, r, http.StatusNotFound, ErrNoSpace)
		return
	}

	if err := s.db.SpaceDelete(user, space); err != nil {
		s.ErrorString(w, r, http.StatusInternalServerError, "failed to delete space")
		return
	}

	url := "/"
	s.Redirect(w, r, url)
}

func (s *Space) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	switch s.Method(r) {
	case "GET":
		s.serve(w, r)
		return
	case "POST":
		s.create(w, r)
		return
	case "PUT": // PUT is closest to the idea of COPYing an entity.
		s.copy(w, r)
		return
	case "PATCH":
		s.update(w, r)
		return
	case "DELETE":
		s.delete(w, r)
		return
	}

	http.NotFound(w, r)
}