~evanj/cms

ref: 27de4d9c907e2258a828fbfcda8f22b910df0f63 cms/internal/s/cache/content.go -rw-r--r-- 2.0 KiB
27de4d9cEvan M Jones Feat(spaces): Added the ability to copy an entire space. 1 year, 7 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
package cache

import (
	"fmt"

	"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/s/db"
)

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

func (c *Cache) ContentNew(space space.Space, ct contenttype.ContentType, params []db.ContentNewParam) (content.Content, error) {
	thing, err := c.db.ContentNew(space, ct, params)
	if err != nil {
		return nil, err
	}

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

func (c *Cache) ContentGet(space space.Space, ct contenttype.ContentType, contentID string) (content.Content, error) {
	return c.content(
		false,
		fmt.Sprintf("%s::%s::%s::%s", c.baseKey, space.ID(), ct.ID(), contentID),
		func() (content.Content, error) { return c.db.ContentGet(space, ct, contentID) },
	)
}

func (c *Cache) ContentUpdate(space space.Space, ct contenttype.ContentType, item content.Content, newParams []db.ContentNewParam, updateParams []db.ContentUpdateParam) (content.Content, error) {
	return c.content(
		true,
		fmt.Sprintf("%s::%s::%s::%s", c.baseKey, space.ID(), ct.ID(), item.ID()),
		func() (content.Content, error) { return c.db.ContentUpdate(space, ct, item, newParams, updateParams) },
	)
}

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

	var deleteErr error
	_, _ = c.content(
		true,
		key,
		func() (content.Content, error) {
			deleteErr = c.db.ContentDelete(space, ct, item)
			return nil, deleteErr
		},
	)

	if deleteErr != nil {
		return deleteErr
	}

	return c.mc.Delete(key)
}

// TODO: ContentPerContentType