forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_revision_repository.go
67 lines (58 loc) · 2.5 KB
/
link_revision_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package link
import (
"context"
"fmt"
"time"
"github.com/fabric8io/almighty-core/errors"
"github.com/fabric8io/almighty-core/log"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
)
// RevisionRepository encapsulates storage & retrieval of historical versions of work item links
type RevisionRepository interface {
// Create stores a new revision for the given work item link.
Create(ctx context.Context, modifierID uuid.UUID, revisionType RevisionType, l WorkItemLink) error
// List retrieves all revisions for a given work item link
List(ctx context.Context, workitemID uuid.UUID) ([]Revision, error)
}
// NewRevisionRepository creates a GormCommentRevisionRepository
func NewRevisionRepository(db *gorm.DB) *GormWorkItemLinkRevisionRepository {
repository := &GormWorkItemLinkRevisionRepository{db}
return repository
}
// GormCommentRevisionRepository implements CommentRevisionRepository using gorm
type GormWorkItemLinkRevisionRepository struct {
db *gorm.DB
}
// Create stores a new revision for the given work item link.
func (r *GormWorkItemLinkRevisionRepository) Create(ctx context.Context, modifierID uuid.UUID, revisionType RevisionType, l WorkItemLink) error {
log.Info(nil, map[string]interface{}{
"modifier_id": modifierID,
"revision_type": revisionType,
}, "Storing a revision after operation on work item link.")
tx := r.db
revision := &Revision{
ModifierIdentity: modifierID,
Time: time.Now(),
Type: revisionType,
WorkItemLinkID: l.ID,
WorkItemLinkVersion: l.Version,
WorkItemLinkSourceID: l.SourceID,
WorkItemLinkTargetID: l.TargetID,
WorkItemLinkTypeID: l.LinkTypeID,
}
if err := tx.Create(&revision).Error; err != nil {
return errors.NewInternalError(fmt.Sprintf("failed to create new work item link revision: %s", err.Error()))
}
log.Debug(ctx, map[string]interface{}{"wil_id": l.ID}, "work item link revision occurrence created")
return nil
}
// List retrieves all revisions for a given work item link
func (r *GormWorkItemLinkRevisionRepository) List(ctx context.Context, linkID uuid.UUID) ([]Revision, error) {
log.Debug(nil, map[string]interface{}{}, "List all revisions for work item link with ID=%v", linkID.String())
revisions := make([]Revision, 0)
if err := r.db.Where("work_item_link_id = ?", linkID.String()).Order("revision_time asc").Find(&revisions).Error; err != nil {
return nil, errors.NewInternalError(fmt.Sprintf("failed to retrieve work item link revisions: %s", err.Error()))
}
return revisions, nil
}