~evanj/cms

ref: 7c487cb4cca6290eae5f854300d6e9c78f81ea25 cms/pkg/skipper/skipper.go -rw-r--r-- 2.4 KiB
7c487cb4Evan J Feat(dynamic): Dynamic content pages have been added (rather than 1 year, 16 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
package skipper

import (
	"context"
	"strings"

	"git.sr.ht/~evanj/cms/internal/m/content"
	"git.sr.ht/~evanj/cms/internal/m/value"
	"git.sr.ht/~evanj/cms/pkg/skipper/api"
)

type Skipper struct {
	api api.API
}

func New(user, pass, baseURL string, space int) Skipper {
	api := api.New(user, pass, baseURL, space)
	return Skipper{
		api: api,
	}
}

func (s Skipper) QueryContentByField(ctx context.Context, contentTypeID int, field, query string) (content.Content, error) {
	item, err := s.api.Find(ctx, contentTypeID, field, query)
	if err != nil {
		return nil, err
	}
	return c{item}, nil
}

///////////////////////////////////////////////////////////////////////////////
//
// CONTENT IMPL
//
///////////////////////////////////////////////////////////////////////////////

type c struct {
	// TODO: Get rid of this pointer.
	api *api.Content
}

func (c c) ID() string   { return c.api.ContentID }
func (c c) Type() string { return c.api.ContentParentTypeID }

func (c c) Values() (ret []value.Value) {
	for _, raw := range c.api.ContentValues {
		val := v{raw}
		ret = append(ret, val)
	}
	return
}

func (c c) ValueByName(name string) (value.Value, bool) {
	for _, val := range c.Values() {
		if val.Name() == name {
			return val, true
		}
	}
	return nil, false
}

func (c c) MustValueByName(name string) value.Value {
	val, _ := c.ValueByName(name)
	return val
}

///////////////////////////////////////////////////////////////////////////////
//
// VALUE IMPL
//
///////////////////////////////////////////////////////////////////////////////

type v struct {
	api api.ContentValue
}

func (v v) ID() string    { return v.api.FieldID }
func (v v) Type() string  { return v.api.FieldType }
func (v v) Name() string  { return v.api.FieldName }
func (v v) Value() string { return v.api.FieldValue }

func (v v) RefID() (ret string) {
	if v.api.FieldReference != nil {
		ret = v.api.FieldReference.ContentID
	}
	return
}

func (v v) RefName() (ret string) {
	if v.api.FieldReference != nil {
		ret = (c{v.api.FieldReference}).MustValueByName("name").Value()
	}
	return
}

func (v v) RefListIDs() (ret []string) {
	for _, val := range v.api.FieldReferenceList {
		ret = append(ret, val.ContentID)
	}
	return ret
}

func (v v) RefListNames() string {
	var ret []string
	for _, val := range v.api.FieldReferenceList {
		ret = append(ret, (c{&val}).MustValueByName("name").Value())
	}
	return strings.Join(ret, ", ")
}