create.go

 1package operations
 2
 3import (
 4	"github.com/MichaelMure/git-bug/bug"
 5	"reflect"
 6)
 7
 8// CreateOperation define the initial creation of a bug
 9
10var _ bug.Operation = CreateOperation{}
11
12type CreateOperation struct {
13	bug.OpBase
14	Title   string
15	Message string
16	Author  bug.Person
17}
18
19func NewCreateOp(author bug.Person, title, message string) CreateOperation {
20	return CreateOperation{
21		OpBase:  bug.OpBase{OperationType: bug.CREATE},
22		Title:   title,
23		Message: message,
24		Author:  author,
25	}
26}
27
28func (op CreateOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
29	empty := bug.Snapshot{}
30
31	if !reflect.DeepEqual(snapshot, empty) {
32		panic("Create operation should never be applied on a non-empty snapshot")
33	}
34
35	snapshot.Title = op.Title
36	snapshot.Comments = []bug.Comment{
37		{
38			Message: op.Message,
39			Author:  op.Author,
40		},
41	}
42	return snapshot
43}