op_set_status.go

 1package bug
 2
 3import (
 4	"github.com/pkg/errors"
 5
 6	"github.com/MichaelMure/git-bug/entities/common"
 7	"github.com/MichaelMure/git-bug/entities/identity"
 8	"github.com/MichaelMure/git-bug/entity"
 9	"github.com/MichaelMure/git-bug/entity/dag"
10	"github.com/MichaelMure/git-bug/util/timestamp"
11)
12
13var _ Operation = &SetStatusOperation{}
14
15// SetStatusOperation will change the status of a bug
16type SetStatusOperation struct {
17	dag.OpBase
18	Status common.Status `json:"status"`
19}
20
21func (op *SetStatusOperation) Id() entity.Id {
22	return dag.IdOperation(op, &op.OpBase)
23}
24
25func (op *SetStatusOperation) Apply(snapshot *Snapshot) {
26	snapshot.Status = op.Status
27	snapshot.addActor(op.Author())
28
29	item := &SetStatusTimelineItem{
30		id:       op.Id(),
31		Author:   op.Author(),
32		UnixTime: timestamp.Timestamp(op.UnixTime),
33		Status:   op.Status,
34	}
35
36	snapshot.Timeline = append(snapshot.Timeline, item)
37}
38
39func (op *SetStatusOperation) Validate() error {
40	if err := op.OpBase.Validate(op, SetStatusOp); err != nil {
41		return err
42	}
43
44	if err := op.Status.Validate(); err != nil {
45		return errors.Wrap(err, "status")
46	}
47
48	return nil
49}
50
51func NewSetStatusOp(author identity.Interface, unixTime int64, status common.Status) *SetStatusOperation {
52	return &SetStatusOperation{
53		OpBase: dag.NewOpBase(SetStatusOp, author, unixTime),
54		Status: status,
55	}
56}
57
58type SetStatusTimelineItem struct {
59	id       entity.Id
60	Author   identity.Interface
61	UnixTime timestamp.Timestamp
62	Status   common.Status
63}
64
65func (s SetStatusTimelineItem) Id() entity.Id {
66	return s.id
67}
68
69// IsAuthored is a sign post method for gqlgen
70func (s SetStatusTimelineItem) IsAuthored() {}
71
72// Open is a convenience function to change a bugs state to Open
73func Open(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*SetStatusOperation, error) {
74	op := NewSetStatusOp(author, unixTime, common.OpenStatus)
75	for key, value := range metadata {
76		op.SetMetadata(key, value)
77	}
78	if err := op.Validate(); err != nil {
79		return nil, err
80	}
81	b.Append(op)
82	return op, nil
83}
84
85// Close is a convenience function to change a bugs state to Close
86func Close(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*SetStatusOperation, error) {
87	op := NewSetStatusOp(author, unixTime, common.ClosedStatus)
88	for key, value := range metadata {
89		op.SetMetadata(key, value)
90	}
91	if err := op.Validate(); err != nil {
92		return nil, err
93	}
94	b.Append(op)
95	return op, nil
96}