1package cache
2
3import (
4 "time"
5
6 "github.com/MichaelMure/git-bug/entities/board"
7 "github.com/MichaelMure/git-bug/entities/identity"
8 "github.com/MichaelMure/git-bug/entity"
9 "github.com/MichaelMure/git-bug/repository"
10)
11
12type RepoCacheBoard struct {
13 *SubCache[*board.Board, *BoardExcerpt, *BoardCache]
14}
15
16func NewRepoCacheBoard(repo repository.ClockedRepo,
17 resolvers func() entity.Resolvers,
18 getUserIdentity getUserIdentityFunc) *RepoCacheBoard {
19
20 makeCached := func(b *board.Board, entityUpdated func(id entity.Id) error) *BoardCache {
21 return NewBoardCache(b, repo, getUserIdentity, entityUpdated)
22 }
23
24 makeIndexData := func(b *BoardCache) []string {
25 // no indexing
26 return nil
27 }
28
29 actions := Actions[*board.Board]{
30 ReadWithResolver: board.ReadWithResolver,
31 ReadAllWithResolver: board.ReadAllWithResolver,
32 Remove: board.Remove,
33 MergeAll: board.MergeAll,
34 }
35
36 sc := NewSubCache[*board.Board, *BoardExcerpt, *BoardCache](
37 repo, resolvers, getUserIdentity,
38 makeCached, NewBoardExcerpt, makeIndexData, actions,
39 board.Typename, board.Namespace,
40 formatVersion, defaultMaxLoadedBugs,
41 )
42
43 return &RepoCacheBoard{SubCache: sc}
44}
45
46func (c *RepoCacheBoard) New(title, description string, columns []string) (*BoardCache, *board.CreateOperation, error) {
47 author, err := c.getUserIdentity()
48 if err != nil {
49 return nil, nil, err
50 }
51
52 return c.NewRaw(author, time.Now().Unix(), title, description, columns, nil)
53}
54
55func (c *RepoCacheBoard) NewDefaultColumns(title, description string) (*BoardCache, *board.CreateOperation, error) {
56 return c.New(title, description, board.DefaultColumns)
57}
58
59// NewRaw create a new board with the given title, description and columns.
60// The new board is written in the repository (commit).
61func (c *RepoCacheBoard) NewRaw(author identity.Interface, unixTime int64, title, description string, columns []string, metadata map[string]string) (*BoardCache, *board.CreateOperation, error) {
62 b, op, err := board.Create(author, unixTime, title, description, columns, metadata)
63 if err != nil {
64 return nil, nil, err
65 }
66
67 err = b.Commit(c.repo)
68 if err != nil {
69 return nil, nil, err
70 }
71
72 cached, err := c.add(b)
73 if err != nil {
74 return nil, nil, err
75 }
76
77 return cached, op, nil
78}