Skip to content

Commit

Permalink
Merge pull request #1274 from traPtitech/fix/openapi-statuscode
Browse files Browse the repository at this point in the history
ステータスコードのチェック
  • Loading branch information
kavos113 authored Oct 17, 2024
2 parents b93eea9 + 06cee26 commit a69191b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ func (q Questionnaire) GetQuestionnaireResponses(c echo.Context, questionnaireID
res := []openapi.Response{}
respondentDetails, err := q.GetRespondentDetails(c.Request().Context(), questionnaireID, string(*params.Sort), *params.OnlyMyResponse, userID)
if err != nil {
if errors.Is(err, model.ErrRecordNotFound) {
return res, echo.NewHTTPError(http.StatusNotFound, "respondent not found")
}
c.Logger().Errorf("failed to get respondent details: %+v", err)
return res, echo.NewHTTPError(http.StatusInternalServerError, "failed to get respondent details")
}
Expand Down Expand Up @@ -422,6 +425,9 @@ func (q Questionnaire) GetQuestionnaireResult(ctx echo.Context, questionnaireID
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))
}
Expand Down
14 changes: 13 additions & 1 deletion handler/questionnaire.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package handler

import (
"errors"
"fmt"
"net/http"

"github.com/labstack/echo/v4"
"github.com/traPtitech/anke-to/controller"
"github.com/traPtitech/anke-to/model"
"github.com/traPtitech/anke-to/openapi"
)

Expand Down Expand Up @@ -61,7 +63,7 @@ func (h Handler) PostQuestionnaire(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to post questionnaire: %w", err))
}

return ctx.JSON(200, res)
return ctx.JSON(201, res)
}

// (GET /questionnaires/{questionnaireID})
Expand All @@ -70,6 +72,9 @@ func (h Handler) GetQuestionnaire(ctx echo.Context, questionnaireID openapi.Ques
q := controller.NewQuestionnaire()
res, err := q.GetQuestionnaire(ctx, questionnaireID)
if err != nil {
if errors.Is(err, model.ErrRecordNotFound) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Errorf("questionnaire not found: %w", err))
}
ctx.Logger().Errorf("failed to get questionnaire: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire: %w", err))
}
Expand Down Expand Up @@ -140,6 +145,10 @@ func (h Handler) GetQuestionnaireResponses(ctx echo.Context, questionnaireID ope
}
q := controller.NewQuestionnaire()
res, err := q.GetQuestionnaireResponses(ctx, questionnaireID, params, userID)
if err != nil {
ctx.Logger().Errorf("failed to get questionnaire responses: %+v", err)
return err
}

return ctx.JSON(200, res)
}
Expand Down Expand Up @@ -192,6 +201,9 @@ func (h Handler) GetQuestionnaireResult(ctx echo.Context, questionnaireID openap
q := controller.NewQuestionnaire()
res, err = q.GetQuestionnaireResult(ctx, questionnaireID, userID)
if err != nil {
if errors.Is(err, echo.ErrNotFound) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Errorf("questionnaire result not found: %w", err))
}
ctx.Logger().Errorf("failed to get questionnaire result: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire result: %w", err))
}
Expand Down
12 changes: 10 additions & 2 deletions handler/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (h Handler) DeleteResponse(ctx echo.Context, responseID openapi.ResponseIDI
err = r.DeleteResponse(ctx, responseID, userID)
if err != nil {
ctx.Logger().Errorf("failed to delete response: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete response: %w", err))
return err
}

return ctx.NoContent(200)
Expand All @@ -53,7 +53,7 @@ func (h Handler) GetResponse(ctx echo.Context, responseID openapi.ResponseIDInPa
res, err := r.GetResponse(ctx, responseID)
if err != nil {
ctx.Logger().Errorf("failed to get response: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get response: %w", err))
return err
}
return ctx.JSON(200, res)
}
Expand All @@ -77,5 +77,13 @@ func (h Handler) EditResponse(ctx echo.Context, responseID openapi.ResponseIDInP
ctx.Logger().Errorf("failed to validate request body: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to validate request body: %w", err))
}

r := controller.NewResponse()
err = r.EditResponse(ctx, responseID, req)
if err != nil {
ctx.Logger().Errorf("failed to edit response: %+v", err)
return err
}

return ctx.NoContent(200)
}

0 comments on commit a69191b

Please sign in to comment.