create.go

 1package operations
 2
 3import (
 4	"github.com/MichaelMure/git-bug/bug"
 5	"github.com/MichaelMure/git-bug/util/git"
 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	files   []git.Hash
17}
18
19func (op CreateOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
20	snapshot.Title = op.Title
21	snapshot.Comments = []bug.Comment{
22		{
23			Message:  op.Message,
24			Author:   op.Author,
25			UnixTime: op.UnixTime,
26		},
27	}
28	snapshot.Author = op.Author
29	snapshot.CreatedAt = op.Time()
30	return snapshot
31}
32
33func (op CreateOperation) Files() []git.Hash {
34	return op.files
35}
36
37func NewCreateOp(author bug.Person, title, message string, files []git.Hash) CreateOperation {
38	return CreateOperation{
39		OpBase:  bug.NewOpBase(bug.CreateOp, author),
40		Title:   title,
41		Message: message,
42		files:   files,
43	}
44}
45
46// Convenience function to apply the operation
47func Create(author bug.Person, title, message string) (*bug.Bug, error) {
48	return CreateWithFiles(author, title, message, nil)
49}
50
51func CreateWithFiles(author bug.Person, title, message string, files []git.Hash) (*bug.Bug, error) {
52	newBug := bug.NewBug()
53	createOp := NewCreateOp(author, title, message, files)
54	newBug.Append(createOp)
55
56	return newBug, nil
57}