Skip to content

Commit

Permalink
impl: check validation when edit response
Browse files Browse the repository at this point in the history
  • Loading branch information
kavos113 committed Sep 28, 2024
1 parent 88041f4 commit 206a691
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions controller/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Response struct {
model.IResponse
model.ITarget
model.IQuestion
model.IValidation
model.IScaleLabel
}

func NewResponse() *Response {
Expand Down Expand Up @@ -164,6 +166,77 @@ func (r Response) EditResponse(ctx echo.Context, responseID openapi.ResponseIDIn
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to convert response body into response metas: %w", err))
}

// validationでチェック
questionIDs := make([]int, len(questions))
questionTypes := make(map[int]string, len(questions))
for i, question := range questions {
questionIDs[i] = question.ID
questionTypes[question.ID] = question.Type
}

validations, err := r.IValidation.GetValidations(ctx.Request().Context(), questionIDs)
if err != nil {
ctx.Logger().Errorf("failed to get validations: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get validations: %w", err))
}

for i, validation := range validations {
switch questionTypes[validation.QuestionID] {
case "Text", "TextLong":
err := r.IValidation.CheckTextValidation(validation, responseMetas[i].Data)
if err != nil {
if errors.Is(err, model.ErrTextMatching) {
ctx.Logger().Errorf("invalid text: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid text: %w", err))
}
ctx.Logger().Errorf("invalid text: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid text: %w", err))
}
case "Number":
err := r.IValidation.CheckNumberValidation(validation, responseMetas[i].Data)
if err != nil {
if errors.Is(err, model.ErrInvalidNumber) {
ctx.Logger().Errorf("invalid number: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid number: %w", err))
}
ctx.Logger().Errorf("invalid number: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid number: %w", err))
}
}
}

// scaleのvalidation
scaleLabelIDs := []int{}
for _, question := range questions {
if question.Type == "Scale" {
scaleLabelIDs = append(scaleLabelIDs, question.ID)
}
}

scaleLabels, err := r.IScaleLabel.GetScaleLabels(ctx.Request().Context(), scaleLabelIDs)
if err != nil {
ctx.Logger().Errorf("failed to get scale labels: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get scale labels: %w", err))
}
scaleLabelMap := make(map[int]model.ScaleLabels, len(scaleLabels))
for _, scaleLabel := range scaleLabels {
scaleLabelMap[scaleLabel.QuestionID] = scaleLabel
}

for i, question := range questions {
if question.Type == "Scale" {
label, ok := scaleLabelMap[question.ID]
if !ok {
label = model.ScaleLabels{}
}
err := r.IScaleLabel.CheckScaleLabel(label, responseMetas[i].Data)
if err != nil {
ctx.Logger().Errorf("invalid scale: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid scale: %w", err))
}
}
}

if len(responseMetas) > 0 {
err = r.IResponse.InsertResponses(ctx.Request().Context(), responseID, responseMetas)
if err != nil {
Expand Down

0 comments on commit 206a691

Please sign in to comment.