-
Notifications
You must be signed in to change notification settings - Fork 1
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
アンケート回答者の一括取得api #169
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\u30A2\u30F3\u30B1\u30FC\u30C8\u56DE\u7B54\u8005\u306E\u4E00\u62EC\u53D6\u5F97api"
アンケート回答者の一括取得api #169
Conversation
openapi.yaml
Outdated
schema: | ||
type: array | ||
items: | ||
$ref: "#/components/schemas/GetQuestionAnswers" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これはAnswer
の配列を返すのではダメですか?新しいschemaを定義するのに何か意図があれば教えてほしいです
$ref: "#/components/schemas/GetQuestionAnswers" | |
$ref: "#/components/schemas/Answer" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Answerの配列だとquestion_idが全部についていて余分かな、と思って作ったんですがakimo君的にないほうがいいならそうします。
openapi.yaml
Outdated
@@ -588,6 +588,26 @@ paths: | |||
$ref: "#/components/responses/NotFound" | |||
"500": | |||
$ref: "#/components/responses/InternalServerError" | |||
/api/users/answers/{question_id}: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/api/users
ときたらその後にはユーザーのIDが来てほしい感があるので、/api/answers/{question_id}
とかで良いかもです(元々僕がやったエンドポイントの命名が全体的に下手なので、どこかで直したいな……)
/api/users/answers/{question_id}: | |
/api/answers/{question_id}: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
分かりました。修正します!
openapi.yaml
Outdated
user_traq_id: | ||
type: string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もしAnswer
を使わずにGetQuestionsAnswers
を使う必要があるのであれば、ここのuser_traq_id
は消したほうが良いです(user_traq_id
のようなフィールド名を指定する必要があるのはtype: object
のときで、単純なstring
の配列には必要ないです)。このままだと型が正確に解釈されず、[]string
ではなく[]interface
になってしまいます……
user_traq_id: | |
type: string | |
type: string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうなんですね。いわれてみたら名前を二つ付けるのは無駄でした。ありがとう!!
backend/handler/answers.go
Outdated
res.Answers = new([]interface{}) | ||
|
||
for _, answer := range answers { | ||
answeredUesr, err := s.repo.GetUserTraqID(answer.UserID) | ||
if err != nil { | ||
e.Logger().Errorf("failed to get user: %v", err) | ||
|
||
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error") | ||
} | ||
|
||
*res.Answers = append(*res.Answers, answeredUesr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
スライスのサイズが分かっている場合はmake
で指定したほうが良いです
参考:https://zenn.dev/mkosakana/articles/2867cb1667bfa8
あと、各answer
に対してs.repo.GetUserTraqID
を呼ぶことでN+1問題が起きています。最初からtraQ IDを外部キーにしていればこんなことは起きなかったので原因は僕にあるし、修正にはDBのマイグレーションが必要になりそうなので(別にマイグレーションは必要なさそう)とりあえず今は放置で大丈夫なんですが、一応知っておいてほしいです 🙏
res.Answers = new([]interface{}) | |
for _, answer := range answers { | |
answeredUesr, err := s.repo.GetUserTraqID(answer.UserID) | |
if err != nil { | |
e.Logger().Errorf("failed to get user: %v", err) | |
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error") | |
} | |
*res.Answers = append(*res.Answers, answeredUesr) | |
res.Answers = make([]string, len(answers)) | |
for i, answer := range answers { | |
answeredUser, err := s.repo.GetUserTraqID(answer.UserID) | |
if err != nil { | |
e.Logger().Errorf("failed to get user: %v", err) | |
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error") | |
} | |
res.Answers[i] = answeredUser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ほんとですね、traqid毎回呼び出すこれは良くなさそう。初めて自分でn+1を作成してしまいました...
一括取得できるapiを作成しました。
子質問に対してリクエストをします。子質問のidと回答したユーザーのtraqidの配列を返します。
公開質問でない場合は合宿係じゃないとforbiddenを返しています。