From 4758c6eeaf3401621adddf7b83347baa5ebc7d9c Mon Sep 17 00:00:00 2001 From: kavos Date: Thu, 10 Oct 2024 17:53:23 +0900 Subject: [PATCH 1/7] fix: PostQuestionnaire response 200 -> 201 --- handler/questionnaire.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/questionnaire.go b/handler/questionnaire.go index c6fe5dc1..0a424142 100644 --- a/handler/questionnaire.go +++ b/handler/questionnaire.go @@ -61,7 +61,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}) From 511b40c10d464bc8de099d3b0b171c768c73506e Mon Sep 17 00:00:00 2001 From: kavos Date: Fri, 11 Oct 2024 16:34:05 +0900 Subject: [PATCH 2/7] fix: return 404 at GetQuestionnaire --- handler/questionnaire.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/handler/questionnaire.go b/handler/questionnaire.go index 0a424142..065a488b 100644 --- a/handler/questionnaire.go +++ b/handler/questionnaire.go @@ -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" ) @@ -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)) } From 9eccbbffc3209af3bec87f8ff403b40fa2a58bd2 Mon Sep 17 00:00:00 2001 From: kavos Date: Fri, 11 Oct 2024 16:39:38 +0900 Subject: [PATCH 3/7] fix: return 404 at GetQuestionnaireResponses --- controller/questionnaire.go | 3 +++ handler/questionnaire.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/controller/questionnaire.go b/controller/questionnaire.go index 3c583503..44d53764 100644 --- a/controller/questionnaire.go +++ b/controller/questionnaire.go @@ -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") } diff --git a/handler/questionnaire.go b/handler/questionnaire.go index 065a488b..26dc7678 100644 --- a/handler/questionnaire.go +++ b/handler/questionnaire.go @@ -145,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) } From bc1b71ee6e954209c1cd4690986806873f7f7047 Mon Sep 17 00:00:00 2001 From: kavos Date: Fri, 11 Oct 2024 16:43:36 +0900 Subject: [PATCH 4/7] fix: return 404 at GetQuestionnaireResult --- controller/questionnaire.go | 3 +++ handler/questionnaire.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/controller/questionnaire.go b/controller/questionnaire.go index 44d53764..ae85c5eb 100644 --- a/controller/questionnaire.go +++ b/controller/questionnaire.go @@ -425,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)) } diff --git a/handler/questionnaire.go b/handler/questionnaire.go index 26dc7678..7e7c0741 100644 --- a/handler/questionnaire.go +++ b/handler/questionnaire.go @@ -201,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)) } From 674e25a56f578fd41d121b7ff7f031b9126b3891 Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 14 Oct 2024 13:25:14 +0900 Subject: [PATCH 5/7] fix: return 404, 500 at GetResponse --- handler/response.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/response.go b/handler/response.go index a7dfbfa5..bd4fe754 100644 --- a/handler/response.go +++ b/handler/response.go @@ -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) } From dc54c0d216fa6a37c01c5ac5e4be3a47e693ea02 Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 14 Oct 2024 13:26:19 +0900 Subject: [PATCH 6/7] fix: return 404, 405, 500 at EditResponse --- handler/response.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/handler/response.go b/handler/response.go index bd4fe754..1553df4b 100644 --- a/handler/response.go +++ b/handler/response.go @@ -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) } From 06cee267e163436bcbd0e31f0a9c4ef44f2d2e00 Mon Sep 17 00:00:00 2001 From: kavos Date: Mon, 14 Oct 2024 13:27:27 +0900 Subject: [PATCH 7/7] fix: return 404, 405, 500 at DeleteResponse --- handler/response.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/response.go b/handler/response.go index 1553df4b..7db80a56 100644 --- a/handler/response.go +++ b/handler/response.go @@ -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)