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 all commits
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
3 changes: 2 additions & 1 deletion docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ components:
description:
並び順 (作成日時が新しい "created_at", 作成日時が古い "-created_at", タイトルの昇順 "title",
タイトルの降順 "-title", 更新日時が新しい "modified_at", 更新日時が古い
"-modified_at" )
"-modified_at", 回答数が多い "response_count", 回答数が少ない "-response_count",
最後の回答が新しい "last_response_at", 最後の回答が古い "-last_response_at" )
schema:
$ref: '#/components/schemas/SortType'
responseSortInQuery:
Expand Down
21 changes: 21 additions & 0 deletions model/questionnaires_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ func (*Questionnaire) GetResponseReadPrivilegeInfoByQuestionnaireID(ctx context.
}

func setQuestionnairesOrder(query *gorm.DB, sort string) (*gorm.DB, error) {
// WARNING: sortが"response_count"または"-response_count"の場合、queryは`GROUP BY questionnaires.id`を前提とする
switch sort {
case "created_at":
query = query.Order("questionnaires.created_at")
Expand All @@ -469,6 +470,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 AS sort_respondents ON questionnaires.id = sort_respondents.questionnaire_id").
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 AS sort_respondents ON questionnaires.id = sort_respondents.questionnaire_id").
Order("COUNT(respondents.response_id) desc")
case "last_response_at":
query = query.
Joins("LEFT OUTER JOIN respondents AS sort_respondents ON questionnaires.id = sort_respondents.questionnaire_id").
Order("updated_at")
case "-last_response_at":
query = query.
Joins("LEFT OUTER JOIN respondents AS sort_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