Skip to content

Commit

Permalink
fix: Check if event id exists before deleting it
Browse files Browse the repository at this point in the history
close #4936

Signed-off-by: Ginny Guan <ginny@iotechsys.com>
  • Loading branch information
jinlinGuan committed Oct 8, 2024
1 parent 97aa120 commit d5aa4e6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions internal/core/data/application/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ func (a *CoreDataApp) DeleteEventById(id string, dic *di.Container) errors.EdgeX
}

dbClient := container.DBClientFrom(dic.Get)

err := dbClient.DeleteEventById(id)
_, err := dbClient.EventById(id)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
err = dbClient.DeleteEventById(id)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/core/data/controller/http/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ func TestDeleteEventById(t *testing.T) {
notFoundEventId := NonexistentEventID

dbClientMock := &dbMock.DBClient{}
dbClientMock.On("DeleteEventById", notFoundEventId).Return(errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "event doesn't exist in the database", nil))
dbClientMock.On("EventById", notFoundEventId).Return(models.Event{}, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "event doesn't exist in the database", nil))
dbClientMock.On("EventById", validEventId).Return(persistedEvent, nil)
dbClientMock.On("DeleteEventById", validEventId).Return(nil)

dic := mocks.NewMockDIC()
Expand Down

0 comments on commit d5aa4e6

Please sign in to comment.