Skip to content

Commit

Permalink
Merge pull request #1122 from traPtitech/feature/extend_option_chr_limit
Browse files Browse the repository at this point in the history
Feature/extend option chr limit
  • Loading branch information
kaitoyama committed Dec 4, 2023
2 parents 85697d6 + 63c0f82 commit c715fae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 46 deletions.
13 changes: 7 additions & 6 deletions model/responses_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package model
import (
"context"
"fmt"
"time"

"gopkg.in/guregu/null.v4"
"gorm.io/gorm"
"time"
)

// Response ResponseRepositoryの実装
Expand All @@ -16,7 +17,7 @@ func NewResponse() *Response {
return new(Response)
}

//Responses responseテーブルの構造体
// Responses responseテーブルの構造体
type Responses struct {
ResponseID int `json:"-" gorm:"type:int(11);not null"`
QuestionID int `json:"-" gorm:"type:int(11);not null"`
Expand All @@ -25,21 +26,21 @@ type Responses struct {
DeletedAt gorm.DeletedAt `json:"-" gorm:"type:TIMESTAMP NULL;default:NULL"`
}

//BeforeCreate insert時に自動でmodifiedAt更新
// BeforeCreate insert時に自動でmodifiedAt更新
func (r *Responses) BeforeCreate(tx *gorm.DB) error {
r.ModifiedAt = time.Now()

return nil
}

//BeforeUpdate Update時に自動でmodified_atを現在時刻に
// BeforeUpdate Update時に自動でmodified_atを現在時刻に
func (r *Responses) BeforeUpdate(tx *gorm.DB) error {
r.ModifiedAt = time.Now()

return nil
}

//TableName テーブル名が単数形なのでその対応
// TableName テーブル名が単数形なのでその対応
func (*Responses) TableName() string {
return "response"
}
Expand All @@ -49,7 +50,7 @@ type ResponseBody struct {
QuestionID int `json:"questionID" gorm:"column:id" validate:"min=0"`
QuestionType string `json:"question_type" gorm:"column:type" validate:"required,oneof=Text TextArea Number MultipleChoice Checkbox LinearScale"`
Body null.String `json:"response" validate:"required"`
OptionResponse []string `json:"option_response" validate:"required_if=QuestionType Checkbox,required_if=QuestionType MultipleChoice,dive,max=50"`
OptionResponse []string `json:"option_response" validate:"required_if=QuestionType Checkbox,required_if=QuestionType MultipleChoice,dive,max=1000"`
}

// ResponseMeta 質問に対する回答の構造体
Expand Down
17 changes: 15 additions & 2 deletions router/questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package router
import (
"errors"
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"regexp"

"github.com/labstack/echo/v4"

"github.com/traPtitech/anke-to/model"
)

Expand Down Expand Up @@ -35,7 +36,7 @@ type PostAndEditQuestionRequest struct {
PageNum int `json:"page_num" validate:"min=0"`
Body string `json:"body" validate:"required"`
IsRequired bool `json:"is_required"`
Options []string `json:"options" validate:"required_if=QuestionType Checkbox,required_if=QuestionType MultipleChoice,dive,max=50"`
Options []string `json:"options" validate:"required_if=QuestionType Checkbox,required_if=QuestionType MultipleChoice,dive,max=1000"`
ScaleLabelRight string `json:"scale_label_right" validate:"max=50"`
ScaleLabelLeft string `json:"scale_label_left" validate:"max=50"`
ScaleMin int `json:"scale_min"`
Expand All @@ -60,6 +61,18 @@ func (q *Question) EditQuestion(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest)
}

validate, err := getValidator(c)
if err != nil {
c.Logger().Errorf("failed to get validator: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

err = validate.Struct(req)
if err != nil {
c.Logger().Infof("validation failed: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}

switch req.QuestionType {
case "Text":
//正規表現のチェック
Expand Down
25 changes: 13 additions & 12 deletions router/questions_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router

import (
"strings"
"testing"

"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -163,15 +164,15 @@ func TestPostQuestionValidate(t *testing.T) {
},
},
{
description: "Textタイプでoptionが50文字以上でもエラー",
description: "Textタイプでoptionが1000文字以上でもエラー",
request: &PostAndEditQuestionRequest{
QuestionnaireID: 1,
QuestionType: "Text",
QuestionNum: 1,
PageNum: 1,
Body: "発表タイトル",
IsRequired: true,
Options: []string{"012345678901234567890123456789012345678901234567890"},
Options: []string{"0" + strings.Repeat("1234567890", 100)},
ScaleLabelRight: "",
ScaleLabelLeft: "",
ScaleMin: 0,
Expand Down Expand Up @@ -446,15 +447,15 @@ func TestPostQuestionValidate(t *testing.T) {
},
},
{
description: "TextAreaタイプでoptionが50文字以上でもエラー",
description: "TextAreaタイプでoptionが1000文字以上でもエラー",
request: &PostAndEditQuestionRequest{
QuestionnaireID: 1,
QuestionType: "TextArea",
QuestionNum: 1,
PageNum: 1,
Body: "発表タイトル",
IsRequired: true,
Options: []string{"012345678901234567890123456789012345678901234567890"},
Options: []string{"0" + strings.Repeat("1234567890", 100)},
ScaleLabelRight: "",
ScaleLabelLeft: "",
ScaleMin: 0,
Expand Down Expand Up @@ -642,15 +643,15 @@ func TestPostQuestionValidate(t *testing.T) {
},
},
{
description: "Numberタイプでoptionが50文字以上でもエラー",
description: "Numberタイプでoptionが1000文字以上でもエラー",
request: &PostAndEditQuestionRequest{
QuestionnaireID: 1,
QuestionType: "Number",
QuestionNum: 1,
PageNum: 1,
Body: "発表タイトル",
IsRequired: true,
Options: []string{"012345678901234567890123456789012345678901234567890"},
Options: []string{"0" + strings.Repeat("1234567890", 100)},
ScaleLabelRight: "",
ScaleLabelLeft: "",
ScaleMin: 0,
Expand Down Expand Up @@ -839,15 +840,15 @@ func TestPostQuestionValidate(t *testing.T) {
isErr: true,
},
{
description: "Checkboxタイプでoptionが50文字以上でエラー",
description: "Checkboxタイプでoptionが1000文字以上でエラー",
request: &PostAndEditQuestionRequest{
QuestionnaireID: 1,
QuestionType: "Checkbox",
QuestionNum: 1,
PageNum: 1,
Body: "発表タイトル",
IsRequired: true,
Options: []string{"012345678901234567890123456789012345678901234567890"},
Options: []string{"0" + strings.Repeat("1234567890", 100)},
ScaleLabelRight: "",
ScaleLabelLeft: "",
ScaleMin: 0,
Expand Down Expand Up @@ -1036,15 +1037,15 @@ func TestPostQuestionValidate(t *testing.T) {
isErr: true,
},
{
description: "MultipleChoiceタイプでoptionが50文字以上でエラー",
description: "MultipleChoiceタイプでoptionが1000文字以上でエラー",
request: &PostAndEditQuestionRequest{
QuestionnaireID: 1,
QuestionType: "MultipleChoice",
QuestionNum: 1,
PageNum: 1,
Body: "発表タイトル",
IsRequired: true,
Options: []string{"012345678901234567890123456789012345678901234567890"},
Options: []string{"0" + strings.Repeat("1234567890", 100)},
ScaleLabelRight: "",
ScaleLabelLeft: "",
ScaleMin: 0,
Expand Down Expand Up @@ -1232,15 +1233,15 @@ func TestPostQuestionValidate(t *testing.T) {
},
},
{
description: "LinearScaleタイプでoptionが50文字以上でもエラー",
description: "LinearScaleタイプでoptionが1000文字以上でもエラー",
request: &PostAndEditQuestionRequest{
QuestionnaireID: 1,
QuestionType: "LinearScale",
QuestionNum: 1,
PageNum: 1,
Body: "発表タイトル",
IsRequired: true,
Options: []string{"012345678901234567890123456789012345678901234567890"},
Options: []string{"0" + strings.Repeat("1234567890", 100)},
ScaleLabelRight: "右",
ScaleLabelLeft: "左",
ScaleMin: 0,
Expand Down
12 changes: 12 additions & 0 deletions router/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ func (r *Response) EditResponse(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest)
}

validate, err := getValidator(c)
if err != nil {
c.Logger().Errorf("failed to get validator: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

err = validate.Struct(req)
if err != nil {
c.Logger().Infof("validation failed: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}

limit, err := r.GetQuestionnaireLimit(c.Request().Context(), req.ID)
if err != nil {
if errors.Is(err, model.ErrRecordNotFound) {
Expand Down
Loading

0 comments on commit c715fae

Please sign in to comment.