op_add_item_entity.go

  1package board
  2
  3import (
  4	"fmt"
  5
  6	"github.com/git-bug/git-bug/entities/bug"
  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
 12// ItemEntityType indicate the type of entity board item
 13type ItemEntityType string
 14
 15const (
 16	EntityTypeBug ItemEntityType = "bug"
 17)
 18
 19var _ Operation = &AddItemEntityOperation{}
 20
 21type AddItemEntityOperation struct {
 22	dag.OpBase
 23	ColumnId   entity.Id        `json:"column"`
 24	EntityType ItemEntityType   `json:"entity_type"`
 25	EntityId   entity.Id        `json:"entity_id"`
 26	entity     entity.Interface // not serialized
 27}
 28
 29func (op *AddItemEntityOperation) Id() entity.Id {
 30	return dag.IdOperation(op, &op.OpBase)
 31}
 32
 33func (op *AddItemEntityOperation) Validate() error {
 34	if err := op.OpBase.Validate(op, AddItemEntityOp); err != nil {
 35		return err
 36	}
 37
 38	if err := op.ColumnId.Validate(); err != nil {
 39		return err
 40	}
 41
 42	switch op.EntityType {
 43	case EntityTypeBug:
 44	default:
 45		return fmt.Errorf("unknown entity type")
 46	}
 47
 48	if err := op.EntityId.Validate(); err != nil {
 49		return err
 50	}
 51
 52	return nil
 53}
 54
 55func (op *AddItemEntityOperation) Apply(snapshot *Snapshot) {
 56	if op.entity == nil {
 57		return
 58	}
 59
 60	// Recreate the combined Id to match on
 61	combinedId := entity.CombineIds(snapshot.Id(), op.ColumnId)
 62
 63	for _, column := range snapshot.Columns {
 64		if column.CombinedId == combinedId {
 65			switch op.EntityType {
 66			case EntityTypeBug:
 67				column.Items = append(column.Items, &BugItem{
 68					combinedId: entity.CombineIds(snapshot.Id(), op.entity.Id()),
 69					Bug:        op.entity.(bug.Interface),
 70				})
 71			}
 72			snapshot.addParticipant(op.Author())
 73			return
 74		}
 75	}
 76}
 77
 78func NewAddItemEntityOp(author identity.Interface, unixTime int64, columnId entity.Id, entityType ItemEntityType, e entity.Interface) *AddItemEntityOperation {
 79	// Note: due to import cycle we are not able to properly check the type of the entity here;
 80	// proceed with caution!
 81	return &AddItemEntityOperation{
 82		OpBase:     dag.NewOpBase(AddItemEntityOp, author, unixTime),
 83		ColumnId:   columnId,
 84		EntityType: entityType,
 85		EntityId:   e.Id(),
 86		entity:     e,
 87	}
 88}
 89
 90// AddItemEntity is a convenience function to add an entity item to a Board
 91func AddItemEntity(b Interface, author identity.Interface, unixTime int64, columnId entity.Id, entityType ItemEntityType, e entity.Interface, metadata map[string]string) (entity.CombinedId, *AddItemEntityOperation, error) {
 92	op := NewAddItemEntityOp(author, unixTime, columnId, entityType, e)
 93	for key, val := range metadata {
 94		op.SetMetadata(key, val)
 95	}
 96	if err := op.Validate(); err != nil {
 97		return entity.UnsetCombinedId, nil, err
 98	}
 99	b.Append(op)
100	return entity.CombineIds(b.Id(), op.Id()), op, nil
101}