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
 21type Item interface {
 22	CombinedId() entity.CombinedId
 23	// TODO: all items have status?
 24	// Status() common.Status
 25}
 26
 27var _ dag.Snapshot = &Snapshot{}
 28
 29type Snapshot struct {
 30	id entity.Id
 31
 32	Title        string
 33	Description  string
 34	Columns      []*Column
 35	Participants []identity.Interface
 36
 37	CreateTime time.Time
 38	Operations []dag.Operation
 39}
 40
 41// Id returns the Board identifier
 42func (snap *Snapshot) Id() entity.Id {
 43	if snap.id == "" {
 44		// simply panic as it would be a coding error (no id provided at construction)
 45		panic("no id")
 46	}
 47	return snap.id
 48}
 49
 50func (snap *Snapshot) AllOperations() []dag.Operation {
 51	return snap.Operations
 52}
 53
 54func (snap *Snapshot) AppendOperation(op dag.Operation) {
 55	snap.Operations = append(snap.Operations, op)
 56}
 57
 58// EditTime returns the last time the board was modified
 59func (snap *Snapshot) EditTime() time.Time {
 60	if len(snap.Operations) == 0 {
 61		return time.Unix(0, 0)
 62	}
 63
 64	return snap.Operations[len(snap.Operations)-1].Time()
 65}
 66
 67// SearchColumn will search for a column matching the given id
 68func (snap *Snapshot) SearchColumn(id entity.CombinedId) (*Column, error) {
 69	for _, column := range snap.Columns {
 70		if column.CombinedId == id {
 71			return column, nil
 72		}
 73	}
 74
 75	return nil, fmt.Errorf("column not found")
 76}
 77
 78// append the operation author to the participants list
 79func (snap *Snapshot) addParticipant(participant identity.Interface) {
 80	for _, p := range snap.Participants {
 81		if participant.Id() == p.Id() {
 82			return
 83		}
 84	}
 85
 86	snap.Participants = append(snap.Participants, participant)
 87}
 88
 89// HasParticipant return true if the id is a participant
 90func (snap *Snapshot) HasParticipant(id entity.Id) bool {
 91	for _, p := range snap.Participants {
 92		if p.Id() == id {
 93			return true
 94		}
 95	}
 96	return false
 97}
 98
 99// HasAnyParticipant return true if one of the ids is a participant
100func (snap *Snapshot) HasAnyParticipant(ids ...entity.Id) bool {
101	for _, id := range ids {
102		if snap.HasParticipant(id) {
103			return true
104		}
105	}
106	return false
107}
108
109func (snap *Snapshot) ItemCount() int {
110	var count int
111	for _, column := range snap.Columns {
112		count += len(column.Items)
113	}
114	return count
115}
116
117// IsAuthored is a sign post method for gqlgen
118func (snap *Snapshot) IsAuthored() {}