create.go

 1package operations
 2
 3import (
 4	"github.com/MichaelMure/git-bug/bug"
 5)
 6
 7// CreateOperation define the initial creation of a bug
 8
 9var _ bug.Operation = CreateOperation{}
10
11type CreateOperation struct {
12	bug.OpBase
13	Title   string
14	Message string
15}
16
17func (op CreateOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
18	snapshot.Title = op.Title
19	snapshot.Comments = []bug.Comment{
20		{
21			Message:  op.Message,
22			Author:   op.Author,
23			UnixTime: op.UnixTime,
24		},
25	}
26	return snapshot
27}
28
29func NewCreateOp(author bug.Person, title, message string) CreateOperation {
30	return CreateOperation{
31		OpBase:  bug.NewOpBase(bug.CreateOp, author),
32		Title:   title,
33		Message: message,
34	}
35}
36
37// Convenience function to apply the operation
38func Create(author bug.Person, title, message string) (*bug.Bug, error) {
39	newBug := bug.NewBug()
40	createOp := NewCreateOp(author, title, message)
41	newBug.Append(createOp)
42
43	return newBug, nil
44}