Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetQuestionnairesQueryParam.Sortにキーワードを追加 #998

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions model/questionnaires_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,26 @@ func setQuestionnairesOrder(query *gorm.DB, sort string) (*gorm.DB, error) {
query = query.Order("questionnaires.modified_at")
case "-modified_at":
query = query.Order("questionnaires.modified_at desc")
case "res_time_limit":
query = query.Order("questionnaires.res_time_limit IS NULL, questionnaires.res_time_limit")
case "-res_time_limit":
query = query.Order("questionnaires.res_time_limit IS NULL desc, questionnaires.res_time_limit desc")
case "response_count":
query = query.
Joins("LEFT OUTER JOIN respondents ON questionnaires.id = respondents.questionnaire_id").
cp-20 marked this conversation as resolved.
Show resolved Hide resolved
Order("COUNT(respondents.response_id)")
cp-20 marked this conversation as resolved.
Show resolved Hide resolved
case "-response_count":
query = query.
Joins("LEFT OUTER JOIN respondents ON questionnaires.id = respondents.questionnaire_id").
Order("COUNT(respondents.response_id) desc")
case "last_response_at":
query = query.
Joins("LEFT OUTER JOIN respondents ON questionnaires.id = respondents.questionnaire_id").
Order("updated_at")
case "-last_response_at":
query = query.
Joins("LEFT OUTER JOIN respondents ON questionnaires.id = respondents.questionnaire_id").
Order("updated_at desc")
case "":
default:
return nil, ErrInvalidSortParam
Expand Down
2 changes: 1 addition & 1 deletion router/questionnaires.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewQuestionnaire(
}

type GetQuestionnairesQueryParam struct {
Sort string `validate:"omitempty,oneof=created_at -created_at title -title modified_at -modified_at"`
Sort string `validate:"omitempty,oneof=created_at -created_at title -title modified_at -modified_at res_time_limit -res_time_limit response_count -response_count last_response_at -last_response_at"`
Search string `validate:"omitempty"`
Page string `validate:"omitempty,number,min=0"`
Nontargeted string `validate:"omitempty,boolean"`
Expand Down
54 changes: 54 additions & 0 deletions router/questionnaires_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,60 @@ func TestGetQuestionnaireValidate(t *testing.T) {
Nontargeted: "true",
},
},
{
description: "Sortがres_time_limitでもエラーなし",
request: &GetQuestionnairesQueryParam{
Sort: "res_time_limit",
Search: "a",
Page: "2",
Nontargeted: "true",
},
},
{
description: "Sortが-res_time_limitでもエラーなし",
request: &GetQuestionnairesQueryParam{
Sort: "-res_time_limit",
Search: "a",
Page: "2",
Nontargeted: "true",
},
},
{
description: "Sortがresponse_countでもエラーなし",
request: &GetQuestionnairesQueryParam{
Sort: "response_count",
Search: "a",
Page: "2",
Nontargeted: "true",
},
},
{
description: "Sortが-response_countでもエラーなし",
request: &GetQuestionnairesQueryParam{
Sort: "-response_count",
Search: "a",
Page: "2",
Nontargeted: "true",
},
},
{
description: "Sortがlast_response_atでもエラーなし",
request: &GetQuestionnairesQueryParam{
Sort: "last_response_at",
Search: "a",
Page: "2",
Nontargeted: "true",
},
},
{
description: "Sortが-last_response_atでもエラーなし",
request: &GetQuestionnairesQueryParam{
Sort: "-last_response_at",
Search: "a",
Page: "2",
Nontargeted: "true",
},
},
{
description: "Nontargetedをfalseにしてもエラーなし",
request: &GetQuestionnairesQueryParam{
Expand Down