board_cache.go

  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
 12// BoardCache is a wrapper around a Board. It provides multiple functions:
 13//
 14// 1. Provide a higher level API to use than the raw API from Board.
 15// 2. Maintain an up-to-date Snapshot available.
 16// 3. Deal with concurrency.
 17type BoardCache struct {
 18	CachedEntityBase[*board.Snapshot, board.Operation]
 19}
 20
 21func NewBoardCache(b *board.Board, repo repository.ClockedRepo, getUserIdentity getUserIdentityFunc, entityUpdated func(id entity.Id) error) *BoardCache {
 22	return &BoardCache{
 23		CachedEntityBase: CachedEntityBase[*board.Snapshot, board.Operation]{
 24			repo:            repo,
 25			entityUpdated:   entityUpdated,
 26			getUserIdentity: getUserIdentity,
 27			entity:          &withSnapshot[*board.Snapshot, board.Operation]{Interface: b},
 28		},
 29	}
 30}
 31
 32func (c *BoardCache) AddItemDraft(columnId entity.Id, title, message string, files []repository.Hash) (entity.CombinedId, *board.AddItemDraftOperation, error) {
 33	author, err := c.getUserIdentity()
 34	if err != nil {
 35		return entity.UnsetCombinedId, nil, err
 36	}
 37
 38	return c.AddItemDraftRaw(author, time.Now().Unix(), columnId, title, message, files, nil)
 39}
 40
 41func (c *BoardCache) AddItemDraftRaw(author identity.Interface, unixTime int64, columnId entity.Id, title, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *board.AddItemDraftOperation, error) {
 42	c.mu.Lock()
 43	itemId, op, err := board.AddItemDraft(c.entity, author, unixTime, columnId, title, message, files, metadata)
 44	c.mu.Unlock()
 45	if err != nil {
 46		return entity.UnsetCombinedId, nil, err
 47	}
 48	return itemId, op, c.notifyUpdated()
 49}
 50
 51func (c *BoardCache) AddItemEntity(columnId entity.Id, e entity.Interface) (entity.CombinedId, *board.AddItemEntityOperation, error) {
 52	author, err := c.getUserIdentity()
 53	if err != nil {
 54		return entity.UnsetCombinedId, nil, err
 55	}
 56
 57	return c.AddItemEntityRaw(author, time.Now().Unix(), columnId, e, nil)
 58}
 59
 60func (c *BoardCache) AddItemEntityRaw(author identity.Interface, unixTime int64, columnId entity.Id, e entity.Interface, metadata map[string]string) (entity.CombinedId, *board.AddItemEntityOperation, error) {
 61	c.mu.Lock()
 62	itemId, op, err := board.AddItemEntity(c.entity, author, unixTime, columnId, e, metadata)
 63	c.mu.Unlock()
 64	if err != nil {
 65		return entity.UnsetCombinedId, nil, err
 66	}
 67	return itemId, op, c.notifyUpdated()
 68}
 69
 70func (c *BoardCache) SetDescription(description string) (*board.SetDescriptionOperation, error) {
 71	author, err := c.getUserIdentity()
 72	if err != nil {
 73		return nil, err
 74	}
 75
 76	return c.SetDescriptionRaw(author, time.Now().Unix(), description, nil)
 77}
 78
 79func (c *BoardCache) SetDescriptionRaw(author identity.Interface, unixTime int64, description string, metadata map[string]string) (*board.SetDescriptionOperation, error) {
 80	c.mu.Lock()
 81	op, err := board.SetDescription(c.entity, author, unixTime, description, metadata)
 82	c.mu.Unlock()
 83	if err != nil {
 84		return nil, err
 85	}
 86	return op, c.notifyUpdated()
 87}
 88
 89func (c *BoardCache) SetTitle(title string) (*board.SetTitleOperation, error) {
 90	author, err := c.getUserIdentity()
 91	if err != nil {
 92		return nil, err
 93	}
 94
 95	return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
 96}
 97
 98func (c *BoardCache) SetTitleRaw(author identity.Interface, unixTime int64, title string, metadata map[string]string) (*board.SetTitleOperation, error) {
 99	c.mu.Lock()
100	op, err := board.SetTitle(c.entity, author, unixTime, title, metadata)
101	c.mu.Unlock()
102	if err != nil {
103		return nil, err
104	}
105	return op, c.notifyUpdated()
106}