Skip to content

Commit

Permalink
Merge pull request #1264 from traPtitech/fix/openapi-#1243
Browse files Browse the repository at this point in the history
PostQuestionnaireResponseの実装
  • Loading branch information
kaitoyama authored Aug 29, 2024
2 parents b34ce6e + 65d79f5 commit 3b6fad2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
48 changes: 46 additions & 2 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,52 @@ func (q Questionnaire) GetQuestionnaireResponses(c echo.Context, questionnaireID
return res, nil
}

func (q Questionnaire) PostQuestionnaireResponse(c echo.Context) error {
// todo: PostQuestionnaireResponse
func (q Questionnaire) PostQuestionnaireResponse(c echo.Context, questionnaireID int, params openapi.PostQuestionnaireResponseJSONRequestBody, userID string) (openapi.Response, error) {
res := openapi.Response{}

limit, err := q.GetQuestionnaireLimit(c.Request().Context(), questionnaireID)
if err != nil {
if errors.Is(err, model.ErrRecordNotFound) {
c.Logger().Info("questionnaire not found")
return res, echo.NewHTTPError(http.StatusNotFound, err)
}
c.Logger().Errorf("failed to get questionnaire limit: %+v", err)
return res, echo.NewHTTPError(http.StatusInternalServerError, err)
}

// 回答期限を過ぎていたらエラー
if limit.Valid && limit.Time.Before(time.Now()) {
c.Logger().Info("expired questionnaire")
return res, echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}

var submittedAt, modifiedAt time.Time
//一時保存のときはnull
if params.IsDraft {
submittedAt = time.Time{}
modifiedAt = time.Time{}
} else {
submittedAt = time.Now()
modifiedAt = time.Now()
}

resopnseID, err := q.InsertRespondent(c.Request().Context(), userID, questionnaireID, null.NewTime(submittedAt, !params.IsDraft))
if err != nil {
c.Logger().Errorf("failed to insert respondant: %+v", err)
return res, echo.NewHTTPError(http.StatusInternalServerError, err)
}

res = openapi.Response{
QuestionnaireId: questionnaireID,
ResponseId: resopnseID,
Respondent: userID,
SubmittedAt: submittedAt,
ModifiedAt: modifiedAt,
IsDraft: params.IsDraft,
Body: params.Body,
}

return res, nil
}

func createQuestionnaireMessage(questionnaireID int, title string, description string, administrators []string, resTimeLimit null.Time, targets []string) string {
Expand Down
12 changes: 11 additions & 1 deletion handler/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ func (h Handler) GetQuestionnaireResponses(ctx echo.Context, questionnaireID ope
// (POST /questionnaires/{questionnaireID}/responses)
func (h Handler) PostQuestionnaireResponse(ctx echo.Context, questionnaireID openapi.QuestionnaireIDInPath) error {
res := openapi.Response{}
userID, err := getUserID(ctx)
if err != nil {
ctx.Logger().Errorf("failed to get userID: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get userID: %w", err))
}

params := openapi.PostQuestionnaireResponseJSONRequestBody{}
if err := ctx.Bind(&params); err != nil {
ctx.Logger().Errorf("failed to bind request body: %+v", err)
Expand All @@ -165,7 +171,11 @@ func (h Handler) PostQuestionnaireResponse(ctx echo.Context, questionnaireID ope
}

q := controller.NewQuestionnaire()
res, err = q.PostQuestionnaireResponse(ctx, questionnaireID, params)
res, err = q.PostQuestionnaireResponse(ctx, questionnaireID, params, userID)
if err != nil {
ctx.Logger().Errorf("failed to post questionnaire response: %+v", err)
return err
}

return ctx.JSON(201, res)
}
Expand Down

0 comments on commit 3b6fad2

Please sign in to comment.