~evanj/cms

ref: 5ea155bcf5f48d777fb2e6b6d66d34e60119d24e cms/internal/c/dynamic/dynamic.go -rw-r--r-- 1015 bytes
5ea155bcEvan J Feat(dynamic): Dynamic content pages have been added (rather than 4 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
package dynamic

import (
	"context"
	"errors"
	"log"
	"net/http"
	"strings"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/content"
	"git.sr.ht/~evanj/cms/internal/v"
)

var (
	fieldToSearchBy = "slug"
	tmpl            = v.MustParse("html/dynamic.html")
)

type Dynamic struct {
	*c.Controller
	log *log.Logger
	api SkipperAPI
	ct  int
}

type SkipperAPI interface {
	QueryContentByField(ctx context.Context, contentTypeID int, field, query string) (content.Content, error)
}

func New(c *c.Controller, log *log.Logger, api SkipperAPI, contenttype int) Dynamic {
	return Dynamic{c, log, api, contenttype}
}

func (d Dynamic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	q := strings.Trim(r.URL.Path, "/")
	c, err := d.api.QueryContentByField(r.Context(), d.ct, fieldToSearchBy, q)
	if err != nil {
		d.log.Println(err)
		d.Error(w, r, http.StatusNotFound, errors.New("failed to find requested content"))
		return
	}

	d.HTML(w, r, tmpl, map[string]interface{}{
		"Content": c,
	})
}