Skip to content

Commit

Permalink
Use consistent id type across handlers
Browse files Browse the repository at this point in the history
Investigating `Invalid handler function signature` on hitting
`/api/file/{id}/deals` via the hand-rolled HTTP Client library. The
parameter types across get source file and get source file deals is
inconsistent. consistently use string.
  • Loading branch information
masih committed Sep 7, 2023
1 parent 23ecab5 commit a75758a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *Client) GetFile(ctx context.Context, id uint64) (*model.File, error) {
}

func (c *Client) GetFileDeals(ctx context.Context, id uint64) ([]model.Deal, error) {
return inspect.GetFileDealsHandler(c.db.WithContext(ctx), id)
return inspect.GetFileDealsHandler(c.db.WithContext(ctx), strconv.FormatUint(id, 10))
}

func (c *Client) PushFile(ctx context.Context, sourceID uint32, fileInfo dshandler.FileInfo) (*model.File, error) {
Expand Down
13 changes: 10 additions & 3 deletions handler/datasource/inspect/itemdeals.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package inspect

import (
"strconv"

"github.com/data-preservation-programs/singularity/handler"
"github.com/data-preservation-programs/singularity/model"
"gorm.io/gorm"
)

func GetFileDealsHandler(
db *gorm.DB,
id uint64,
id string,
) ([]model.Deal, error) {
return getFileDealsHandler(db, id)
}
Expand All @@ -22,15 +25,19 @@ func GetFileDealsHandler(
// @Router /file/{id}/deals [get]
func getFileDealsHandler(
db *gorm.DB,
id uint64,
id string,
) ([]model.Deal, error) {
fileID, err := strconv.Atoi(id)
if err != nil {
return nil, handler.NewInvalidParameterErr("invalid file id")
}
var deals []model.Deal
query := db.
Model(&model.FileRange{}).
Select("deals.*").
Joins("JOIN cars ON file_ranges.pack_job_id = cars.pack_job_id").
Joins("JOIN deals ON cars.piece_cid = deals.piece_cid").
Where("file_ranges.file_id = ?", id)
Where("file_ranges.file_id = ?", fileID)
if err := query.Find(&deals).Error; err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions handler/datasource/inspect/itemdeals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path"
"strconv"
"testing"

"github.com/data-preservation-programs/singularity/database"
Expand Down Expand Up @@ -83,11 +84,11 @@ func TestFileDeals(t *testing.T) {
}

// Test file deals
deals, err := inspect.GetFileDealsHandler(db.WithContext(ctx), fileA.ID)
deals, err := inspect.GetFileDealsHandler(db.WithContext(ctx), strconv.FormatUint(fileA.ID, 10))
require.NoError(t, err)
require.Len(t, deals, 1)

deals, err = inspect.GetFileDealsHandler(db.WithContext(ctx), fileB.ID)
deals, err = inspect.GetFileDealsHandler(db.WithContext(ctx), strconv.FormatUint(fileB.ID, 10))
require.NoError(t, err)
require.Len(t, deals, 1)
}

0 comments on commit a75758a

Please sign in to comment.