~evanj/cms

ref: 6c96e30e09740267a195200d91604acdb15fbf05 cms/internal/s/cache/contenttype.go -rw-r--r-- 2.2 KiB
6c96e30eEvan M Jones Fix(index.html): Include main.js when user is present (for modals). 1 year, 1 day 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
package cache

import (
	"fmt"

	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/s/db"
)

// TODO: ON DELETE CASCADE
// BIG TODO: ON DELETE CASCADE
// Because of this we need to loop through array keys for content when a
// contenttype is deleted and remove from cache.

func (c *Cache) contenttype(breakCache bool, key string, getter func() (contenttype.ContentType, error)) (contenttype.ContentType, error) {
	var v *db.ContentType
	if err := c.cache(breakCache, key, &v, func() (interface{}, error) { return getter() }); err != nil {
		return nil, err
	}
	return v, nil
}

func (c *Cache) ContentTypeNew(space space.Space, name string, params []db.ContentTypeNewParam) (contenttype.ContentType, error) {
	thing, err := c.db.ContentTypeNew(space, name, params)
	if err != nil {
		return nil, err
	}

	return c.contenttype(
		true,
		fmt.Sprintf("%s::%s::%s", c.baseKey, space.ID(), thing.ID()),
		func() (contenttype.ContentType, error) { return thing, err },
	)
}

func (c *Cache) ContentTypeUpdate(space space.Space, thing contenttype.ContentType, name string, newParams []db.ContentTypeNewParam, updateParams []db.ContentTypeUpdateParam) (contenttype.ContentType, error) {
	thing, err := c.db.ContentTypeUpdate(space, thing, name, newParams, updateParams)
	if err != nil {
		return nil, err
	}

	return c.contenttype(
		true,
		fmt.Sprintf("%s::%s::%s", c.baseKey, space.ID(), thing.ID()),
		func() (contenttype.ContentType, error) { return thing, err },
	)
}

func (c *Cache) ContentTypeGet(space space.Space, thingID string) (contenttype.ContentType, error) {
	return c.contenttype(
		false,
		fmt.Sprintf("%s::%s::%s", c.baseKey, space.ID(), thingID),
		func() (contenttype.ContentType, error) { return c.db.ContentTypeGet(space, thingID) },
	)
}

func (c *Cache) ContentTypeDelete(space space.Space, ct contenttype.ContentType) error {
	key := fmt.Sprintf("%s::%s::%s", c.baseKey, space.ID(), ct.ID())

	var deleteErr error
	_, _ = c.contenttype(
		true,
		key,
		func() (contenttype.ContentType, error) {
			deleteErr = c.db.ContentTypeDelete(space, ct)
			return nil, deleteErr
		},
	)

	if deleteErr != nil {
		return deleteErr
	}

	return c.mc.Delete(key)
}

// TODO: ContentTypeUpdate
// TODO: ContentTypesPerSpace