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