snapshot.go

  1package board
  2
  3import (
  4	"fmt"
  5	"time"
  6
  7	"github.com/git-bug/git-bug/entities/identity"
  8	"github.com/git-bug/git-bug/entity"
  9	"github.com/git-bug/git-bug/entity/dag"
 10)
 11
 12type Column struct {
 13	// id is the identifier of the column within the board context
 14	Id entity.Id
 15	// CombinedId is the global identifier of the column
 16	CombinedId entity.CombinedId
 17	Name       string
 18	Items      []Item
 19}
 20
 21// Item is the interface that board item (draft, bug ...) need to implement.
 22type Item interface {
 23	// CombinedId returns the global identifier of the item.
 24	CombinedId() entity.CombinedId
 25
 26	// Author returns the author of the item.
 27	Author() identity.Interface
 28
 29	// Title returns the title of the item.
 30	Title() string
 31
 32	// TODO: add status, show bug's status, draft has no status
 33	// Status() common.Status
 34
 35	// TODO: add labels, show bug's label, draft has no label
 36	// Labels() []common.Label
 37}
 38
 39var _ dag.Snapshot = &Snapshot{}
 40
 41type Snapshot struct {
 42	id entity.Id
 43
 44	CreateTime  time.Time
 45	Title       string
 46	Description string
 47	Columns     []*Column
 48
 49	// Actors are all the identities that have interacted with the board (add items ...)
 50	Actors []identity.Interface
 51
 52	Operations []dag.Operation
 53}
 54
 55// Id returns the Board identifier
 56func (snap *Snapshot) Id() entity.Id {
 57	if snap.id == "" {
 58		// simply panic as it would be a coding error (no id provided at construction)
 59		panic("no id")
 60	}
 61	return snap.id
 62}
 63
 64func (snap *Snapshot) AllOperations() []dag.Operation {
 65	return snap.Operations
 66}
 67
 68func (snap *Snapshot) AppendOperation(op dag.Operation) {
 69	snap.Operations = append(snap.Operations, op)
 70}
 71
 72// EditTime returns the last time the board was modified
 73func (snap *Snapshot) EditTime() time.Time {
 74	if len(snap.Operations) == 0 {
 75		return time.Unix(0, 0)
 76	}
 77
 78	return snap.Operations[len(snap.Operations)-1].Time()
 79}
 80
 81// SearchColumn will search for a column matching the given id
 82func (snap *Snapshot) SearchColumn(id entity.CombinedId) (*Column, error) {
 83	for _, column := range snap.Columns {
 84		if column.CombinedId == id {
 85			return column, nil
 86		}
 87	}
 88
 89	return nil, fmt.Errorf("column not found")
 90}
 91
 92// append the operation author to the actors list
 93func (snap *Snapshot) addActor(actor identity.Interface) {
 94	for _, a := range snap.Actors {
 95		if actor.Id() == a.Id() {
 96			return
 97		}
 98	}
 99
100	snap.Actors = append(snap.Actors, actor)
101}
102
103// HasActor return true if the id is a actor
104func (snap *Snapshot) HasActor(id entity.Id) bool {
105	for _, p := range snap.Actors {
106		if p.Id() == id {
107			return true
108		}
109	}
110	return false
111}
112
113// HasAnyActor return true if one of the ids is a actor
114func (snap *Snapshot) HasAnyActor(ids ...entity.Id) bool {
115	for _, id := range ids {
116		if snap.HasActor(id) {
117			return true
118		}
119	}
120	return false
121}
122
123// ItemCount returns the number of items (draft, entity) in the board.
124func (snap *Snapshot) ItemCount() int {
125	var count int
126	for _, column := range snap.Columns {
127		count += len(column.Items)
128	}
129	return count
130}