Created
June 3, 2018 06:24
-
-
Save hauxe/391dde9fbbd49f927621a5011e7dfb7c to your computer and use it in GitHub Desktop.
crud register create handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (crud *CRUD) registerC() gomHTTP.ServerRoute { | |
// build create sql | |
fields := crud.Config.createFields | |
if len(fields) == 0 { | |
// allow create all fields | |
fields = crud.Config.fields | |
} | |
fieldNames := make([]string, len(fields)) | |
for i, field := range fields { | |
fieldNames[i] = field.name | |
} | |
crud.Config.createdFields = fields | |
crud.Config.sqlCRUDCreate = fmt.Sprintf(sqlCRUDCreate, crud.Config.TableName, | |
strings.Join(fieldNames, ","), ":"+strings.Join(fieldNames, ",:")) | |
// build validator | |
validators := []gomHTTP.ParamValidator{} | |
for _, field := range fieldNames { | |
if validatorName, ok := crud.Config.fieldValidators[field]; ok { | |
if validator, ok := crud.Config.Validators[validatorName]; ok { | |
validators = append(validators, getMethodValidator("create", validator)) | |
} | |
} | |
} | |
return gomHTTP.ServerRoute{ | |
Name: "crud_create_" + crud.Config.TableName, | |
Method: http.MethodPost, | |
Path: fmt.Sprintf("/%s", crud.Config.TableName), | |
Validators: validators, | |
Handler: crud.handleCreate, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment