Skip to content

Commit

Permalink
database: Move MarkNotificationAsRead logic to dbutil
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyboardNerd committed Mar 4, 2019
1 parent 4fa03d1 commit a31a794
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
15 changes: 3 additions & 12 deletions api/v3/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
pb "github.com/coreos/clair/api/v3/clairpb"
"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/imagefmt"
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/pkg/pagination"
)

Expand Down Expand Up @@ -216,21 +215,13 @@ func (s *NotificationServer) MarkNotificationAsRead(ctx context.Context, req *pb
return nil, status.Error(codes.InvalidArgument, "notification name should not be empty")
}

tx, err := s.Store.Begin()
found, err := database.MarkNotificationAsReadAndCommit(s.Store, req.GetName())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, newRPCErrorWithClairError(codes.Internal, err)
}

defer tx.Rollback()
err = tx.DeleteNotification(req.GetName())
if err == commonerr.ErrNotFound {
if !found {
return nil, status.Error(codes.NotFound, "requested notification \""+req.GetName()+"\" is not found")
} else if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

if err := tx.Commit(); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &pb.MarkNotificationAsReadResponse{}, nil
Expand Down
23 changes: 23 additions & 0 deletions database/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

log "github.com/sirupsen/logrus"

"github.com/coreos/clair/pkg/commonerr"
"github.com/deckarep/golang-set"
)

Expand Down Expand Up @@ -400,3 +401,25 @@ func PersistDetectorsAndCommit(store Datastore, detectors []Detector) error {

return nil
}

// MarkNotificationAsReadAndCommit marks a notification as read.
func MarkNotificationAsReadAndCommit(store Datastore, name string) (bool, error) {
tx, err := store.Begin()
if err != nil {
return false, err
}

defer tx.Rollback()
err = tx.DeleteNotification(name)
if err == commonerr.ErrNotFound {
return false, nil
} else if err != nil {
return false, err
}

if err := tx.Commit(); err != nil {
return false, err
}

return true, nil
}

0 comments on commit a31a794

Please sign in to comment.