~evanj/cms

ref: 4aa45cc85249c542c4d80aed47e25c86145bcc3c cms/internal/c/contenttype/contenttype.go -rw-r--r-- 4.2 KiB
4aa45cc8Evan M Jones Feat(mysql): Moving to mysql. Getting ready for alpha release (just for 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
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
package contenttype

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

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/content"
	"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/db"
	"git.sr.ht/~evanj/cms/internal/s/tmpl"
)

var (
	contenttypeHTML = tmpl.MustParse("html/contenttype.html")
)

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

type dber interface {
	UserGet(username, password string) (user.User, error)
	UserGetFromToken(token string) (user.User, error)
	SpaceGet(user user.User, spaceID string) (space.Space, error)
	ContentTypeNew(space space.Space, name string, params []db.ContentTypeNewParam) (contenttype.ContentType, error)
	ContentTypeGet(space space.Space, contenttypeID string) (contenttype.ContentType, error)
	ContentPerContentType(space space.Space, ct contenttype.ContentType, page int) ([]content.Content, error)
}

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

func (c *ContentType) create(w http.ResponseWriter, r *http.Request) {
	name := r.FormValue("name")
	spaceID := r.FormValue("space")

	i := 1
	for key := range r.Form {
		if strings.Contains(key, "field_type_") || strings.Contains(key, "field_name_") {
			i++
		}
	}

	var params []db.ContentTypeNewParam
	for e := 0; e < (i / 2); e++ {
		keyName := fmt.Sprintf("field_name_%d", e+1)
		keyType := fmt.Sprintf("field_type_%d", e+1)
		valName := r.FormValue(keyName)
		valType := r.FormValue(keyType)
		if valName == "" || valType == "" {
			c.Error(w, r, http.StatusBadRequest, "form has malformed data")
			return
		}
		params = append(params, db.ContentTypeNewParam{
			Name: valName,
			Type: valType,
		})
	}

	if len(params) < 1 {
		c.Error(w, r, http.StatusBadRequest, "contenttype must have at least one field")
		return
	}

	user, err := c.GetCookieUser(w, r)
	if err != nil {
		c.Error(w, r, http.StatusBadRequest, "must be logged in to create contenttype")
		return
	}

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

	// Enforce content always has a value for value type of "name"
	hasName := false
	for _, p := range params {
		if p.Name == "name" {
			hasName = true
		}
	}
	if !hasName {
		c.Error(w, r, http.StatusInternalServerError, "must have field of \"name\" for searchability")
		return
	}

	ct, err := c.db.ContentTypeNew(space, name, params)
	if err != nil {
		c.log.Println(err)
		c.Error(w, r, http.StatusInternalServerError, "failed to create contenttype")
		return
	}

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

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

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

	ct, err := c.db.ContentTypeGet(space, contenttypeID)
	if err != nil {
		c.Error(w, r, http.StatusInternalServerError, "failed to find desired contenttype")
		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.

	list, err := c.db.ContentPerContentType(space, ct, page)
	if err != nil {
		c.log.Println(err)
		c.Error(w, r, http.StatusInternalServerError, "failed to find content for contenttype")
		return
	}

	c.HTML(w, r, contenttypeHTML, map[string]interface{}{
		"User":        user,
		"Space":       space,
		"ContentType": ct,
		"ContentList": list,
	})
}

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

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

	http.NotFound(w, r)
}