diff --git a/model/respondents_impl.go b/model/respondents_impl.go index 70c500e9..233dbcfc 100755 --- a/model/respondents_impl.go +++ b/model/respondents_impl.go @@ -396,18 +396,23 @@ func (*Respondent) GetRespondentsUserIDs(ctx context.Context, questionnaireIDs [ } // GetMyResponses 自分のすべての回答を取得 -func (*Respondent) GetMyResponseIDs(ctx context.Context, userID string) ([]int, error) { +func (*Respondent) GetMyResponseIDs(ctx context.Context, sort string, userID string) ([]int, error) { db, err := getTx(ctx) if err != nil { return nil, fmt.Errorf("failed to get transaction: %w", err) } responsesID := []int{} - err = db. - Model(&Respondents{}). + query := db.Model(&Respondents{}). Where("user_traqid = ?", userID). - Select("response_id"). - Find(&responsesID).Error + Select("response_id") + + query, _, err = setRespondentsOrder(query, sort) + if err != nil { + return nil, fmt.Errorf("failed to set respondents order: %w", err) + } + + err = query.Find(&responsesID).Error if err != nil { return nil, fmt.Errorf("failed to get responsesID: %w", err) } diff --git a/model/respondents_test.go b/model/respondents_test.go index 6dde78ca..41078f11 100644 --- a/model/respondents_test.go +++ b/model/respondents_test.go @@ -1025,6 +1025,7 @@ func TestGetMyResponseIDs(t *testing.T) { } type args struct { + sort string userID string } type expect struct { @@ -1042,6 +1043,7 @@ func TestGetMyResponseIDs(t *testing.T) { { description: "valid user with one resonse", args: args{ + sort: "submitted_at", userID: userOne, }, expect: expect{ @@ -1051,6 +1053,7 @@ func TestGetMyResponseIDs(t *testing.T) { { description: "valid user with multiple responses", args: args{ + sort: "submitted_at", userID: userTwo, }, expect: expect{ @@ -1060,6 +1063,7 @@ func TestGetMyResponseIDs(t *testing.T) { { description: "valid user with no response", args: args{ + sort: "submitted_at", userID: userThree, }, expect: expect{ @@ -1069,7 +1073,7 @@ func TestGetMyResponseIDs(t *testing.T) { } for _, testCase := range testCases { - MyResponseIDs, err := respondentImpl.GetMyResponseIDs(ctx, testCase.args.userID) + MyResponseIDs, err := respondentImpl.GetMyResponseIDs(ctx, testCase.args.sort, testCase.args.userID) if !testCase.expect.isErr { assertion.NoError(err, testCase.description, "no error")