Skip to content

Commit

Permalink
Merge pull request #86 from ynufes-tech/shion/file-question-image-con…
Browse files Browse the repository at this point in the history
…straint-new

FileQuestion / ImageConstraintの実装改築
  • Loading branch information
Shion1305 authored Jun 2, 2024
2 parents bf262bf + 5259aa0 commit 742ebe4
Show file tree
Hide file tree
Showing 13 changed files with 645 additions and 177 deletions.
4 changes: 2 additions & 2 deletions svc/pkg/domain/model/question/checkbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func ImportCheckBoxQuestion(q StandardQuestion) (*CheckBoxQuestion, error) {
return NewCheckBoxQuestion(q.ID, q.Text, options, optionsOrder, q.FormID), nil
}

func (q CheckBoxQuestion) Export() StandardQuestion {
func (q CheckBoxQuestion) Export() (*StandardQuestion, error) {
customs := make(map[string]interface{})
options := make(map[string]string, len(q.Options))
for _, option := range q.Options {
Expand All @@ -105,7 +105,7 @@ func (q CheckBoxQuestion) Export() StandardQuestion {
}
customs[CheckBoxOptionsField] = options
customs[CheckBoxOptionsOrderField] = optionsOrder
return NewStandardQuestion(TypeCheckBox, q.ID, q.FormID, q.Text, customs)
return NewStandardQuestion(TypeCheckBox, q.ID, q.FormID, q.Text, customs), nil
}

func (o CheckboxOptionsOrder) GetOrderedIDs() []CheckBoxOptionID {
Expand Down
99 changes: 42 additions & 57 deletions svc/pkg/domain/model/question/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,85 @@ import (
type (
FileQuestion struct {
Basic
FileType FileType
Constraint FileConstraint
FileTypes FileTypes
ImageFileConstraint
}
FileTypes struct {
AcceptAny bool
AcceptImage bool
AcceptPDF bool
}
FileType int
)

const (
Image FileType = 1
PDF FileType = 2
Any FileType = 3
FileQuestionFileTypeField = "fileType"
FileConstraintsCustomsField = "fileConstraint"
FileQuestionFileTypeField = "fileTypes"
FileImageConstraintField = "img_c"
)

func (t FileType) String() string {
switch t {
case Image:
return "image"
case PDF:
return "pdf"
case Any:
return "any"
default:
return "unknown"
}
}

func NewFileQuestion(
id id.QuestionID, text string, fileType FileType, constraint FileConstraint, formID id.FormID,
id id.QuestionID, text string, fileTypes FileTypes,
imgConstraint ImageFileConstraint,
formID id.FormID,
) *FileQuestion {
return &FileQuestion{
Basic: NewBasic(id, text, TypeFile, formID),
FileType: fileType,
Constraint: constraint,
}
}

func NewFileType(v int) (FileType, error) {
switch FileType(v) {
case Image, PDF, Any:
return FileType(v), nil
Basic: NewBasic(id, text, TypeFile, formID),
FileTypes: fileTypes,
ImageFileConstraint: imgConstraint,
}
return 0, errors.New("invalid file type")
}

func ImportFileQuestion(q StandardQuestion) (*FileQuestion, error) {
// check if customs has "fileType" as int, return error if not
// expect custom field has fileTypes as []bool
// [AcceptAny, AcceptImage, AcceptPDF]
fileTypeDataI, has := q.Customs[FileQuestionFileTypeField]
if !has {
return nil, errors.New(
fmt.Sprintf("\"%s\" is required for FileQuestion", FileQuestionFileTypeField))
}
fileTypeData, ok := fileTypeDataI.(int64)
fileTypeData, ok := fileTypeDataI.([]bool)
if !ok {
return nil, errors.New(
fmt.Sprintf("\"%s\" must be int for FileQuestion", FileQuestionFileTypeField))
}
fileType, err := NewFileType(int(fileTypeData))
if err != nil {
return nil, err

// extend length if not enough
if len(fileTypeData) < 3 {
for i := len(fileTypeData); i < 3; i++ {
fileTypeData = append(fileTypeData, false)
}
}

if fileType == Any {
return NewFileQuestion(q.ID, q.Text, fileType, nil, q.FormID), nil
fileTypes := FileTypes{
AcceptAny: fileTypeData[0],
AcceptImage: fileTypeData[1],
AcceptPDF: fileTypeData[2],
}

constraintsCustomsData, has := q.Customs[FileConstraintsCustomsField]
// if FileConstraintsCustomsField is not present, return FileQuestion without constraint
imgConstraintCustomR, has := q.Customs[FileImageConstraintField]
//if FileConstraintsCustomsField is not present, return FileQuestion without constraint
if !has {
return NewFileQuestion(q.ID, q.Text, fileType, nil, q.FormID), nil
return NewFileQuestion(q.ID, q.Text, fileTypes, ImageFileConstraint{}, q.FormID), nil
}

constraintsCustoms, ok := constraintsCustomsData.(map[string]interface{})
// if FileConstraintsCustomsField Found, but it is not map[string]interface{}, return error
imgConstraintCustom, ok := imgConstraintCustomR.(map[string]interface{})
//if FileConstraintsCustomsField Found, but it is not slice, return error
if !ok {
return nil, errors.New(
fmt.Sprintf("\"%s\" must be map[string]interface{} for FileQuestion", FileConstraintsCustomsField))
fmt.Sprintf("\"%s\" must be map[string]interface{} for FileQuestion", FileImageConstraintField))
}

constraint := NewStandardFileConstraint(fileType, constraintsCustoms)
question := NewFileQuestion(q.ID, q.Text, fileType, ImportFileConstraint(constraint), q.FormID)
imgConstraint, err := ImportImageFileConstraint(imgConstraintCustom)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to import ImageFileConstraint: %w", err)
}
question := NewFileQuestion(q.ID, q.Text, fileTypes, *imgConstraint, q.FormID)
return question, nil
}

func (q FileQuestion) Export() StandardQuestion {
func (q FileQuestion) Export() (*StandardQuestion, error) {
customs := make(map[string]interface{})

customs[FileQuestionFileTypeField] = q.FileType

if q.Constraint != nil {
customs[FileConstraintsCustomsField] = q.Constraint.Export().Customs
}
return NewStandardQuestion(TypeFile, q.ID, q.FormID, q.Text, customs)
qt := []bool{q.FileTypes.AcceptAny, q.FileTypes.AcceptImage, q.FileTypes.AcceptPDF}
customs[FileQuestionFileTypeField] = qt
customs[FileImageConstraintField] = q.ImageFileConstraint.Export()
return NewStandardQuestion(TypeFile, q.ID, q.FormID, q.Text, customs), nil
}
23 changes: 7 additions & 16 deletions svc/pkg/domain/model/question/file_constraint.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package question

type (
FileType int
StandardFileConstraint struct {
Type FileType
Customs map[string]interface{}
}
FileConstraint interface {
GetFileType() FileType
GetExtensions() []Extension
Export() StandardFileConstraint
Export() (*StandardFileConstraint, error)
ValidateFiles(file []File) error
}
File struct {
Expand All @@ -18,18 +19,8 @@ type (
Extension string
)

func NewStandardFileConstraint(fileType FileType, customs map[string]interface{}) StandardFileConstraint {
return StandardFileConstraint{
Type: fileType,
Customs: customs,
}
}

func ImportFileConstraint(standard StandardFileConstraint) FileConstraint {
switch standard.Type {
case Image:
return ImportImageFileConstraint(standard)
default:
return nil
}
}
const (
Image FileType = 1
PDF FileType = 2
FileTypeCustomField = "type"
)
Loading

0 comments on commit 742ebe4

Please sign in to comment.