1package bug
2
3import (
4 "github.com/pkg/errors"
5
6 "github.com/MichaelMure/git-bug/entity"
7 "github.com/MichaelMure/git-bug/entity/dag"
8 "github.com/MichaelMure/git-bug/identity"
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 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 item := &SetStatusTimelineItem{
29 id: op.Id(),
30 Author: op.Author(),
31 UnixTime: timestamp.Timestamp(op.UnixTime),
32 Status: op.Status,
33 }
34
35 snapshot.Timeline = append(snapshot.Timeline, item)
36}
37
38func (op *SetStatusOperation) Validate() error {
39 if err := op.OpBase.Validate(op, SetStatusOp); err != nil {
40 return err
41 }
42
43 if err := op.Status.Validate(); err != nil {
44 return errors.Wrap(err, "status")
45 }
46
47 return nil
48}
49
50func NewSetStatusOp(author identity.Interface, unixTime int64, status Status) *SetStatusOperation {
51 return &SetStatusOperation{
52 OpBase: dag.NewOpBase(SetStatusOp, author, unixTime),
53 Status: status,
54 }
55}
56
57type SetStatusTimelineItem struct {
58 id entity.Id
59 Author identity.Interface
60 UnixTime timestamp.Timestamp
61 Status Status
62}
63
64func (s SetStatusTimelineItem) Id() entity.Id {
65 return s.id
66}
67
68// IsAuthored is a sign post method for gqlgen
69func (s *SetStatusTimelineItem) IsAuthored() {}
70
71// Convenience function to apply the operation
72func Open(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
73 op := NewSetStatusOp(author, unixTime, OpenStatus)
74 if err := op.Validate(); err != nil {
75 return nil, err
76 }
77 b.Append(op)
78 return op, nil
79}
80
81// Convenience function to apply the operation
82func Close(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
83 op := NewSetStatusOp(author, unixTime, ClosedStatus)
84 if err := op.Validate(); err != nil {
85 return nil, err
86 }
87 b.Append(op)
88 return op, nil
89}