From b971cccb815c79ced0a31d153750c8e5e88fbeba Mon Sep 17 00:00:00 2001 From: Omkar Prabhu Date: Thu, 26 Oct 2023 11:56:27 +0530 Subject: [PATCH] Save final result for mediaitem with missing keywords (#57) --- api/internal/service/api.go | 10 ++++++++++ api/internal/service/api_test.go | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/api/internal/service/api.go b/api/internal/service/api.go index db2f938..a187339 100644 --- a/api/internal/service/api.go +++ b/api/internal/service/api.go @@ -403,6 +403,16 @@ func (s *Service) SaveMediaItemFinalResult(_ context.Context, req *api.MediaItem } slog.Info("saving final mediaitem result", slog.Any("userId", req.UserId), slog.Any("mediaitem", req.Id)) + mediaItem := models.MediaItem{} + mediaItem.ID = uid + mediaItem.UserID = userID + mediaItem.Keywords = &req.Keywords + result := s.DB.Model(&mediaItem).Updates(mediaItem) + if result.Error != nil || result.RowsAffected != 1 { + slog.Error("error saving mediaitem keywords", slog.Any("error", result.Error)) + return &emptypb.Empty{}, status.Errorf(codes.Internal, "error saving mediaitem final result: %s", result.Error.Error()) + } + if len(req.GetEmbeddings()) > 0 { mediaItemEmbeddings := make([]models.MediaitemEmbedding, len(req.GetEmbeddings())) for idx, reqEmbedding := range req.GetEmbeddings() { diff --git a/api/internal/service/api_test.go b/api/internal/service/api_test.go index 5c05456..b060025 100644 --- a/api/internal/service/api_test.go +++ b/api/internal/service/api_test.go @@ -1069,6 +1069,10 @@ func TestSaveMediaItemFinalResult(t *testing.T) { "save mediaitem final result with success", &mediaItemFinalResultRequest, func(mock sqlmock.Sqlmock) { + mock.ExpectBegin() + mock.ExpectExec(regexp.QuoteMeta(`UPDATE "mediaitems"`)). + WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectCommit() mock.ExpectBegin() mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO "mediaitem_embeddings"`)). WillReturnResult(sqlmock.NewResult(1, 1)) @@ -1077,9 +1081,24 @@ func TestSaveMediaItemFinalResult(t *testing.T) { nil, }, { - "save mediaitem final result with error", + "save mediaitem final result with error saving keywords", + &mediaItemFinalResultRequest, + func(mock sqlmock.Sqlmock) { + mock.ExpectBegin() + mock.ExpectExec(regexp.QuoteMeta(`UPDATE "mediaitems"`)). + WillReturnError(errors.New("some db error")) + mock.ExpectRollback() + }, + status.Error(codes.Internal, "error saving mediaitem final result: some db error"), + }, + { + "save mediaitem final result with error saving embeddings", &mediaItemFinalResultRequest, func(mock sqlmock.Sqlmock) { + mock.ExpectBegin() + mock.ExpectExec(regexp.QuoteMeta(`UPDATE "mediaitems"`)). + WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectCommit() mock.ExpectBegin() mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO "mediaitem_embeddings"`)). WillReturnError(errors.New("some db error"))