Skip to content

Commit

Permalink
Save final result for mediaitem with missing keywords (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhuomkar authored Oct 26, 2023
1 parent d97bd3e commit b971ccc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 10 additions & 0 deletions api/internal/service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
21 changes: 20 additions & 1 deletion api/internal/service/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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"))
Expand Down

0 comments on commit b971ccc

Please sign in to comment.