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	Title        string
 45	Description  string
 46	Columns      []*Column
 47	Participants []identity.Interface
 48
 49	CreateTime time.Time
 50	Operations []dag.Operation
 51}
 52
 53// Id returns the Board identifier
 54func (snap *Snapshot) Id() entity.Id {
 55	if snap.id == "" {
 56		// simply panic as it would be a coding error (no id provided at construction)
 57		panic("no id")
 58	}
 59	return snap.id
 60}
 61
 62func (snap *Snapshot) AllOperations() []dag.Operation {
 63	return snap.Operations
 64}
 65
 66func (snap *Snapshot) AppendOperation(op dag.Operation) {
 67	snap.Operations = append(snap.Operations, op)
 68}
 69
 70// EditTime returns the last time the board was modified
 71func (snap *Snapshot) EditTime() time.Time {
 72	if len(snap.Operations) == 0 {
 73		return time.Unix(0, 0)
 74	}
 75
 76	return snap.Operations[len(snap.Operations)-1].Time()
 77}
 78
 79// SearchColumn will search for a column matching the given id
 80func (snap *Snapshot) SearchColumn(id entity.CombinedId) (*Column, error) {
 81	for _, column := range snap.Columns {
 82		if column.CombinedId == id {
 83			return column, nil
 84		}
 85	}
 86
 87	return nil, fmt.Errorf("column not found")
 88}
 89
 90// append the operation author to the participants list
 91func (snap *Snapshot) addParticipant(participant identity.Interface) {
 92	for _, p := range snap.Participants {
 93		if participant.Id() == p.Id() {
 94			return
 95		}
 96	}
 97
 98	snap.Participants = append(snap.Participants, participant)
 99}
100
101// HasParticipant return true if the id is a participant
102func (snap *Snapshot) HasParticipant(id entity.Id) bool {
103	for _, p := range snap.Participants {
104		if p.Id() == id {
105			return true
106		}
107	}
108	return false
109}
110
111// HasAnyParticipant return true if one of the ids is a participant
112func (snap *Snapshot) HasAnyParticipant(ids ...entity.Id) bool {
113	for _, id := range ids {
114		if snap.HasParticipant(id) {
115			return true
116		}
117	}
118	return false
119}
120
121// ItemCount returns the number of items (draft, entity) in the board.
122func (snap *Snapshot) ItemCount() int {
123	var count int
124	for _, column := range snap.Columns {
125		count += len(column.Items)
126	}
127	return count
128}
129
130// IsAuthored is a sign post method for gqlgen
131func (snap *Snapshot) IsAuthored() {}