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

PostQuestionnaireResponseの実装 #1264

Merged
merged 3 commits into from
Aug 29, 2024
Merged
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
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
Loading