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	snapshot.Author = op.Author
27	snapshot.CreatedAt = op.Time()
28	return snapshot
29}
30
31func NewCreateOp(author bug.Person, title, message string) CreateOperation {
32	return CreateOperation{
33		OpBase:  bug.NewOpBase(bug.CreateOp, author),
34		Title:   title,
35		Message: message,
36	}
37}
38
39// Convenience function to apply the operation
40func Create(author bug.Person, title, message string) (*bug.Bug, error) {
41	newBug := bug.NewBug()
42	createOp := NewCreateOp(author, title, message)
43	newBug.Append(createOp)
44
45	return newBug, nil
46}