~evanj/cms

ref: 37b75b6fd4e482657f38cb98668b78df49c66971 cms/internal/c/space/space.go -rw-r--r-- 2.5 KiB
37b75b6fEvan M Jones TEST(cache): Attempting to cache at controller level. 8 months 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
package space

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

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

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

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)
	ContentTypesPerSpace(space space.Space, page int) ([]contenttype.ContentType, error)
}

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

func (s *Space) serve(w http.ResponseWriter, r *http.Request, spaceID string) {
	user, err := s.GetCookieUser(w, r)
	if err != nil {
		s.Error(w, r, http.StatusBadRequest, "must be logged in to create space")
		return
	}

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

	page, err := strconv.Atoi(r.URL.Query().Get("page"))
	if err != nil || page < 1 {
		page = 1
	}
	page-- // Show one to user but start counting at zero for us.

	cts, err := s.db.ContentTypesPerSpace(space, page)
	if err != nil {
		s.Error(w, r, http.StatusInternalServerError, "failed to find contenttypes for space")
		return
	}

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

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, "must be logged in to create space")
		return
	}

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

	url := fmt.Sprintf("/space/%s", space.ID())
	s.log.Println("successfully created new space for user", user.Name(), "redirecting to", url)
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func (s *Space) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	switch r.URL.Path {
	case "/space/new":
		s.create(w, r)
		return
	}

	parts := strings.Split(r.URL.Path, "/")
	if len(parts) > 2 {
		s.serve(w, r, parts[2])
		return
	}

	http.NotFound(w, r)
}