Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitoyama committed Jun 22, 2024
1 parent a17788f commit b34ce6e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ func (q Questionnaire) GetQuestionnaireResponses(c echo.Context, questionnaireID
return res, nil
}

func (q Questionnaire) PostQuestionnaireResponse(c echo.Context) error {
// todo: PostQuestionnaireResponse
}

Check failure on line 345 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

missing return (typecheck)

Check failure on line 345 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

missing return) (typecheck)

Check failure on line 345 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Build

missing return

Check failure on line 345 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L345

missing return (typecheck)
Raw output
controller/questionnaire.go:345:1: missing return (typecheck)
package controller

Check failure on line 345 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L345

missing return) (typecheck)
Raw output
controller/questionnaire.go:345:1: missing return) (typecheck)
	"github.com/traPtitech/anke-to/controller"
	^

func createQuestionnaireMessage(questionnaireID int, title string, description string, administrators []string, resTimeLimit null.Time, targets []string) string {
var resTimeLimitText string
if resTimeLimit.Valid {
Expand Down
13 changes: 13 additions & 0 deletions handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package handler

import (
"errors"
"fmt"

"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
)

Expand All @@ -28,6 +30,17 @@ func SetUserIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
}
}

// getValidator Validatorを設定する
func getValidator(c echo.Context) (*validator.Validate, error) {
rowValidate := c.Get(validatorKey)
validate, ok := rowValidate.(*validator.Validate)
if !ok {
return nil, fmt.Errorf("failed to get validator")
}

return validate, nil
}

// getUserID ユーザーIDを取得する
func getUserID(c echo.Context) (string, error) {
rowUserID := c.Get(userIDKey)
Expand Down
30 changes: 30 additions & 0 deletions handler/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func (h Handler) PostQuestionnaire(ctx echo.Context) error {
ctx.Logger().Errorf("failed to bind request body: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to bind request body: %w", err))
}
validate, err := getValidator(ctx)
if err != nil {
ctx.Logger().Errorf("failed to get validator: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get validator: %w", err))
}

err = validate.StructCtx(ctx.Request().Context(), params)
if err != nil {
ctx.Logger().Errorf("failed to validate request body: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to validate request body: %w", err))
}

res := openapi.QuestionnaireDetail{}
q := controller.NewQuestionnaire()
Expand Down Expand Up @@ -136,6 +147,25 @@ 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{}
params := openapi.PostQuestionnaireResponseJSONRequestBody{}
if err := ctx.Bind(&params); err != nil {
ctx.Logger().Errorf("failed to bind request body: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to bind request body: %w", err))
}
validate, err := getValidator(ctx)
if err != nil {
ctx.Logger().Errorf("failed to get validator: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get validator: %w", err))
}

err = validate.StructCtx(ctx.Request().Context(), params)
if err != nil {
ctx.Logger().Errorf("failed to validate request body: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to validate request body: %w", err))
}

q := controller.NewQuestionnaire()
res, err = q.PostQuestionnaireResponse(ctx, questionnaireID, params)

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

0 comments on commit b34ce6e

Please sign in to comment.