git-bug/bug/op_set_status.go

128 lines
2.8 KiB
Go
Raw Normal View History

package bug
import (
2019-01-19 21:23:31 +03:00
"encoding/json"
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/entity"
2018-11-21 20:56:12 +03:00
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/util/timestamp"
)
var _ Operation = &SetStatusOperation{}
// SetStatusOperation will change the status of a bug
type SetStatusOperation struct {
2018-10-01 12:37:17 +03:00
OpBase
Status Status `json:"status"`
}
// Sign-post method for gqlgen
func (op *SetStatusOperation) IsOperation() {}
func (op *SetStatusOperation) base() *OpBase {
2018-10-01 12:37:17 +03:00
return &op.OpBase
}
func (op *SetStatusOperation) Id() entity.Id {
return idOperation(op)
}
func (op *SetStatusOperation) Apply(snapshot *Snapshot) {
snapshot.Status = op.Status
snapshot.addActor(op.Author)
item := &SetStatusTimelineItem{
id: op.Id(),
Author: op.Author,
UnixTime: timestamp.Timestamp(op.UnixTime),
Status: op.Status,
}
snapshot.Timeline = append(snapshot.Timeline, item)
}
func (op *SetStatusOperation) Validate() error {
if err := opBaseValidate(op, SetStatusOp); err != nil {
return err
}
if err := op.Status.Validate(); err != nil {
return errors.Wrap(err, "status")
}
return nil
}
// UnmarshalJSON is a two step JSON unmarshaling
// This workaround is necessary to avoid the inner OpBase.MarshalJSON
// overriding the outer op's MarshalJSON
2019-01-19 21:23:31 +03:00
func (op *SetStatusOperation) UnmarshalJSON(data []byte) error {
// Unmarshal OpBase and the op separately
base := OpBase{}
err := json.Unmarshal(data, &base)
if err != nil {
return err
}
aux := struct {
Status Status `json:"status"`
}{}
err = json.Unmarshal(data, &aux)
if err != nil {
return err
}
op.OpBase = base
op.Status = aux.Status
return nil
}
2018-12-23 19:11:37 +03:00
// Sign post method for gqlgen
func (op *SetStatusOperation) IsAuthored() {}
2018-11-21 20:56:12 +03:00
func NewSetStatusOp(author identity.Interface, unixTime int64, status Status) *SetStatusOperation {
return &SetStatusOperation{
OpBase: newOpBase(SetStatusOp, author, unixTime),
Status: status,
}
}
type SetStatusTimelineItem struct {
id entity.Id
2018-11-21 20:56:12 +03:00
Author identity.Interface
UnixTime timestamp.Timestamp
Status Status
}
func (s SetStatusTimelineItem) Id() entity.Id {
return s.id
}
2019-06-23 19:32:22 +03:00
// Sign post method for gqlgen
func (s *SetStatusTimelineItem) IsAuthored() {}
// Convenience function to apply the operation
2018-11-21 20:56:12 +03:00
func Open(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
op := NewSetStatusOp(author, unixTime, OpenStatus)
if err := op.Validate(); err != nil {
return nil, err
}
b.Append(op)
return op, nil
}
// Convenience function to apply the operation
2018-11-21 20:56:12 +03:00
func Close(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
op := NewSetStatusOp(author, unixTime, ClosedStatus)
if err := op.Validate(); err != nil {
return nil, err
}
b.Append(op)
return op, nil
}