Created
June 17, 2018 04:40
-
-
Save hauxe/d8cfe8006042b57c7b24cd6dd8566926 to your computer and use it in GitHub Desktop.
crud register update
This file contains 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) registerU() gomHTTP.ServerRoute { | |
// build update sql | |
fields := crud.Config.updateFields | |
if len(fields) == 0 { | |
// allow update all fields | |
fields = crud.Config.fields | |
} | |
fieldNames := make([]string, len(fields)) | |
for i, field := range fields { | |
fieldNames[i] = field.name | |
} | |
crud.Config.updatedFields = fields | |
names := make([]string, len(fieldNames)) | |
for i, field := range fieldNames { | |
names[i] = fmt.Sprintf("`%s` = :%s", field, field) | |
} | |
crud.Config.sqlCRUDUpdate = fmt.Sprintf(sqlCRUDUpdate, crud.Config.TableName, | |
strings.Join(names, ","), crud.Config.pk.name, crud.Config.pk.name) | |
// build validator | |
validators := []gomHTTP.ParamValidator{validatePrimaryKey(crud.Config.pk.index)} | |
for _, field := range fieldNames { | |
if validatorName, ok := crud.Config.fieldValidators[field]; ok { | |
if validator, ok := crud.Config.Validators[validatorName]; ok { | |
validators = append(validators, getMethodValidator("update", validator)) | |
} | |
} | |
} | |
return gomHTTP.ServerRoute{ | |
Name: "crud_update_" + crud.Config.TableName, | |
Method: http.MethodPatch, | |
Path: fmt.Sprintf("/%s", crud.Config.TableName), | |
Validators: validators, | |
Handler: crud.handleUpdate, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment