Skip to content

Commit

Permalink
Merge branch 'fix/openapi' into fix/openapi-anonymous-response
Browse files Browse the repository at this point in the history
  • Loading branch information
kavos113 committed Nov 9, 2024
2 parents f39b8ae + 1cc2744 commit 44bda24
Show file tree
Hide file tree
Showing 22 changed files with 592 additions and 260 deletions.
2 changes: 1 addition & 1 deletion controller/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func respondentDetail2Response(ctx echo.Context, respondentDetail model.Responde
IsDraft: respondentDetail.SubmittedAt.Valid,
ModifiedAt: respondentDetail.ModifiedAt,
QuestionnaireId: respondentDetail.QuestionnaireID,
Respondent: respondentDetail.TraqID,
Respondent: &(respondentDetail.TraqID),
ResponseId: respondentDetail.ResponseID,
SubmittedAt: respondentDetail.SubmittedAt.Time,
}
Expand Down
50 changes: 11 additions & 39 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (q Questionnaire) PostQuestionnaire(c echo.Context, userID string, params o
questionnaireID := 0

err := q.ITransaction.Do(c.Request().Context(), nil, func(ctx context.Context) error {
questionnaireID, err := q.InsertQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, convertResponseViewableBy(params.ResponseViewableBy), params.IsAnonymous)
questionnaireID, err := q.InsertQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, convertResponseViewableBy(params.ResponseViewableBy), params.IsPublished, params.IsAnonymous)
if err != nil {
c.Logger().Errorf("failed to insert questionnaire: %+v", err)
return err
Expand Down Expand Up @@ -214,12 +214,12 @@ func (q Questionnaire) PostQuestionnaire(c echo.Context, userID string, params o
c.Logger().Errorf("failed to get question settings: %+v", err)
return openapi.QuestionnaireDetail{}, echo.NewHTTPError(http.StatusInternalServerError, "failed to get question settings")
}
err = q.IScaleLabel.InsertScaleLabel(c.Request().Context(), question.ID,
err = q.IScaleLabel.InsertScaleLabel(c.Request().Context(), question.ID,
model.ScaleLabels{
ScaleLabelLeft: *b.MinLabel,
ScaleLabelLeft: *b.MinLabel,
ScaleLabelRight: *b.MaxLabel,
ScaleMax: b.MaxValue,
ScaleMin: b.MinValue,
ScaleMax: b.MaxValue,
ScaleMin: b.MinValue,
})
if err != nil {
c.Logger().Errorf("failed to insert scale label: %+v", err)
Expand Down Expand Up @@ -302,7 +302,7 @@ func (q Questionnaire) EditQuestionnaire(c echo.Context, questionnaireID int, pa
responseDueDateTime.Time = *params.ResponseDueDateTime
}
err := q.ITransaction.Do(c.Request().Context(), nil, func(ctx context.Context) error {
err := q.UpdateQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, string(params.ResponseViewableBy), questionnaireID, params.IsAnonymous)
err := q.UpdateQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, string(params.ResponseViewableBy), questionnaireID, params.IsPublished, params.IsAnonymous)
if err != nil && !errors.Is(err, model.ErrNoRecordUpdated) {
c.Logger().Errorf("failed to update questionnaire: %+v", err)
return err
Expand Down Expand Up @@ -397,12 +397,12 @@ func (q Questionnaire) EditQuestionnaire(c echo.Context, questionnaireID int, pa
c.Logger().Errorf("failed to get question settings: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get question settings")
}
err = q.IScaleLabel.UpdateScaleLabel(c.Request().Context(), question.ID,
err = q.IScaleLabel.UpdateScaleLabel(c.Request().Context(), question.ID,
model.ScaleLabels{
ScaleLabelLeft: *b.MinLabel,
ScaleLabelLeft: *b.MinLabel,
ScaleLabelRight: *b.MaxLabel,
ScaleMax: b.MaxValue,
ScaleMin: b.MinValue,
ScaleMax: b.MaxValue,
ScaleMin: b.MinValue,
})
if err != nil && !errors.Is(err, model.ErrNoRecordUpdated) {
c.Logger().Errorf("failed to insert scale label: %+v", err)
Expand Down Expand Up @@ -659,7 +659,7 @@ func (q Questionnaire) PostQuestionnaireResponse(c echo.Context, questionnaireID
res = openapi.Response{
QuestionnaireId: questionnaireID,
ResponseId: resopnseID,
Respondent: userID,
Respondent: &userID,
SubmittedAt: submittedAt,
ModifiedAt: modifiedAt,
IsDraft: params.IsDraft,
Expand Down Expand Up @@ -705,31 +705,3 @@ https://anke-to.trap.jp/responses/new/%d`,
questionnaireID,
)
}

func (q Questionnaire) GetQuestionnaireResult(ctx echo.Context, questionnaireID int, userID string) (openapi.Result, error) {
res := openapi.Result{}

params := openapi.GetQuestionnaireResponsesParams{}
responses, err := q.GetQuestionnaireResponses(ctx, questionnaireID, params, userID)
if err != nil {
if errors.Is(echo.ErrNotFound, err) {
return openapi.Result{}, err
}
ctx.Logger().Errorf("failed to get questionnaire responses: %+v", err)
return openapi.Result{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire responses: %w", err))
}

for _, response := range responses {
tmp := openapi.ResultItem{
Body: response.Body,
IsDraft: response.IsDraft,
ModifiedAt: response.ModifiedAt,
QuestionnaireId: response.QuestionnaireId,
ResponseId: response.ResponseId,
SubmittedAt: response.SubmittedAt,
}
res = append(res, tmp)
}

return res, nil
}
2 changes: 1 addition & 1 deletion controller/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r Response) GetMyResponses(ctx echo.Context, params openapi.GetMyResponses
ModifiedAt: response.ModifiedAt,
QuestionnaireId: response.QuestionnaireId,
QuestionnaireInfo: &questionnaireInfo,
Respondent: userID,
Respondent: &userID,
ResponseId: response.ResponseId,
SubmittedAt: response.SubmittedAt,
}
Expand Down
2 changes: 2 additions & 0 deletions docs/db_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
| res_time_limit | timestamp | YES | | _NULL_ | | 回答の締切日時 (締切がない場合は NULL) |
| deleted_at | timestamp | YES | | _NULL_ | | アンケートが削除された日時 (削除されていない場合は NULL) |
| res_shared_to | char(30) | NO | | administrators | | アンケートの結果を, 運営は見られる ("administrators"), 回答済みの人は見られる ("respondents") 誰でも見られる ("public") |
| is_anonymous | boolean | NO | | false | | アンケートが匿名解答かどうか |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが作成された日時 |
| modified_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが更新された日時 |
| is_published | boolean | NO | | false | | アンケートが公開かどうか |

### respondents

Expand Down
58 changes: 4 additions & 54 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ paths: # TODO 変数の命名を確認する
operationId: getQuestionnaireResponses
tags:
- questionnaire
description: アンケートの全ての回答を取得します。アンケートが匿名回答の場合、取得できません
description: アンケートの全ての回答を取得します。匿名回答の場合はRespondentが空文字列になります
parameters:
- $ref: "#/components/parameters/questionnaireIDInPath"
- $ref: "#/components/parameters/responseSortInQuery"
Expand All @@ -183,8 +183,6 @@ paths: # TODO 変数の命名を確認する
$ref: "#/components/schemas/Responses"
"400":
description: アンケートのIDが無効です
"403":
description: アンケートが匿名回答のため回答を取得できません
"404":
description: アンケートが存在しません
"500":
Expand Down Expand Up @@ -217,29 +215,6 @@ paths: # TODO 変数の命名を確認する
description: 回答期限が過ぎたため回答できません
"500":
description: 正常に回答が作成できませんでした
/questionnaires/{questionnaireID}/result:
get:
operationId: getQuestionnaireResult
tags:
- questionnaire
description: アンケートの回答を集計した結果を取得します。回答者の情報は含まれず、アンケートが匿名回答であっても取得できます。
parameters:
- $ref: "#/components/parameters/questionnaireIDInPath"
responses:
"200":
description: 正常に取得できました。
content:
application/json:
schema:
$ref: "#/components/schemas/Result"
"400":
description: アンケートのIDが無効です
"403":
description: 結果を閲覧する権限がありません。
"404":
description: アンケートが存在しません
"500":
description: アンケートの結果を正常に取得できませんでした
/responses/{responseID}:
get:
operationId: getResponse
Expand Down Expand Up @@ -861,6 +836,9 @@ components:
example: 1
respondent:
$ref: "#/components/schemas/TraqId"
is_anonymous:
type: boolean
example: true
submitted_at:
type: string
format: date-time
Expand All @@ -871,7 +849,6 @@ components:
example: 2020-01-01T00:00:00+09:00
required:
- response_id
- respondent
- submitted_at
- modified_at
- $ref: "#/components/schemas/NewResponse"
Expand Down Expand Up @@ -963,33 +940,6 @@ components:
type: integer
required:
- answer
Result:
type: array
items:
$ref: "#/components/schemas/ResultItem"
ResultItem:
type: object
allOf:
- $ref: "#/components/schemas/QuestionnaireID"
- type: object
properties:
response_id:
type: integer
example: 1
submitted_at:
type: string
format: date-time
example: 2020-01-01T00:00:00+09:00
modified_at:
type: string
format: date-time
example: 2020-01-01T00:00:00+09:00
required:
- response_id
- respondent
- submitted_at
- modified_at
- $ref: "#/components/schemas/NewResponse"
UsersAndGroups:
type: object
properties:
Expand Down
Loading

0 comments on commit 44bda24

Please sign in to comment.