Skip to content

Commit

Permalink
fix: add soring parameter for implementation and test of GetMyRespons…
Browse files Browse the repository at this point in the history
…eIDs
  • Loading branch information
Eraxyso authored Dec 10, 2024
1 parent f5708e1 commit a183cb3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 10 additions & 5 deletions model/respondents_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion model/respondents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,7 @@ func TestGetMyResponseIDs(t *testing.T) {
}

type args struct {
sort string
userID string
}
type expect struct {
Expand All @@ -1042,6 +1043,7 @@ func TestGetMyResponseIDs(t *testing.T) {
{
description: "valid user with one resonse",
args: args{
sort: "submitted_at",
userID: userOne,
},
expect: expect{
Expand All @@ -1051,6 +1053,7 @@ func TestGetMyResponseIDs(t *testing.T) {
{
description: "valid user with multiple responses",
args: args{
sort: "submitted_at",
userID: userTwo,
},
expect: expect{
Expand All @@ -1060,6 +1063,7 @@ func TestGetMyResponseIDs(t *testing.T) {
{
description: "valid user with no response",
args: args{
sort: "submitted_at",
userID: userThree,
},
expect: expect{
Expand All @@ -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")
Expand Down

0 comments on commit a183cb3

Please sign in to comment.