Browse Source

Update: implemented patch & put.

develop
Mingcai SHEN 8 years ago
parent
commit
1bfa363736
1 changed files with 70 additions and 11 deletions
  1. +70
    -11
      restlet/curd.go

+ 70
- 11
restlet/curd.go View File

@ -194,35 +194,94 @@ func _make_put_or_patch_handle(table *xql.Table, entity interface{}, updatefunc
entityType := reflect.TypeOf(entity) entityType := reflect.TypeOf(entity)
log.Debugln("_make_put_or_patch_handle> ", table.TableName, entityType) log.Debugln("_make_put_or_patch_handle> ", table.TableName, entityType)
if nil == post_data || len(post_data) < 1 { if nil == post_data || len(post_data) < 1 {
log.Warnln("_make_put_or_patch_handle:> Empty DATA.")
return Failure_Response(ERROR_INVALID_DATA, "Empty Data!") return Failure_Response(ERROR_INVALID_DATA, "Empty Data!")
} }
entityObj := reflect.New(entityType) entityObj := reflect.New(entityType)
var entityMap map[string]interface{} = make(map[string]interface{}) var entityMap map[string]interface{} = make(map[string]interface{})
err = json.Unmarshal(post_data, entityObj.Elem().Addr().Interface()) err = json.Unmarshal(post_data, entityObj.Elem().Addr().Interface())
e1 := json.Unmarshal(post_data, entityMap)
e1 := json.Unmarshal(post_data, &entityMap)
if nil != err || nil != e1 { if nil != err || nil != e1 {
log.Warnln("_make_put_or_patch_handle:> Invalid DATA:", err, e1)
return Failure_Response(ERROR_INVALID_DATA, "Invalid Data!") return Failure_Response(ERROR_INVALID_DATA, "Invalid Data!")
} }
for _, k := range pks {
if _, ok := url_params.GetString(k); ok {
//entityMap[table.PrimaryKey[i]] = _build_column_query_value(table, table.PrimaryKey[i], "=", v)
return Failure_Response(ERROR_FORBIDDEN, "Not Allowed to Change Primary Key(s).")
if updatefunc != nil {
cols, err := updatefunc(entityObj.Elem().Addr().Interface())
if nil != err {
log.Warnln("_make_put_or_patch_handle:> Invalid DATA:", err, e1)
return Failure_Response(ERROR_INVALID_DATA, "Invalid Data!")
}
for _, k := range cols {
entityMap[k] = true
} }
} }
var pk_mapping map[string]interface{} = _build_params_map(table, url_params, pks...)
if nil != pk_mapping {
//for i, _ := range pks {
// pk, _ := table.Columns[table.PrimaryKey[i]]
// if _, ok := entityMap[pk.FieldName]; ok {
// return Failure_Response(ERROR_FORBIDDEN, "Not Allowed to Change Primary Key(s).")
// }
// if _, ok := entityMap[pk.JTAG]; ok {
// return Failure_Response(ERROR_FORBIDDEN, "Not Allowed to Change Primary Key(s).")
// }
// if _, ok := entityMap[pk.PropertyName]; ok {
// return Failure_Response(ERROR_FORBIDDEN, "Not Allowed to Change Primary Key(s).")
// }
//}
var updateCols []xql.UpdateColumn
for _, c := range table.Columns {
if _, ok := entityMap[c.FieldName]; ok {
}
session := xql.MakeSession(ctx.SQL(), "postgres", true)
defer session.Close()
result = &RestletResult{}
}else if _, ok := entityMap[c.JTAG]; ok {
}else if _, ok := entityMap[c.PropertyName]; ok {
}else{
continue
}
if c.PrimaryKey {
return Failure_Response(ERROR_FORBIDDEN, "Not Allowed to Change Primary Key(s).")
}
uc := xql.UpdateColumn{Field:c.FieldName, Operator:"="}
val := entityObj.Elem().FieldByName(c.PropertyName)
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
uc.Value = val.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uc.Value = val.Uint()
case reflect.String:
uc.Value = val.String()
case reflect.Bool:
uc.Value = val.Bool()
case reflect.Float32, reflect.Float64:
uc.Value = val.Float()
default:
uc.Value = val.Interface()
}
//uc.Value =
updateCols = append(updateCols, uc)
}
qc, err := Build_QueryControl(queries, table) qc, err := Build_QueryControl(queries, table)
if nil != err { if nil != err {
log.Errorln("_make_put_or_patch_handle:> failure:", err) log.Errorln("_make_put_or_patch_handle:> failure:", err)
return Failure_Response(ERROR_BAD_REQUEST, fmt.Sprintf("%s", err)) return Failure_Response(ERROR_BAD_REQUEST, fmt.Sprintf("%s", err))
} }
log.Debugln("_make_put_or_patch_handle> QueryControl:", qc) log.Debugln("_make_put_or_patch_handle> QueryControl:", qc)
var pk_mapping map[string]interface{} = _build_params_map(table, url_params, pks...)
if nil != pk_mapping {
qc.Filters = append(qc.Filters, pk_mapping)
}
session := xql.MakeSession(ctx.SQL(), "postgres", true)
defer session.Close()
n, e := session.Query(table).Filter(qc.Filters...).Update(updateCols)
if nil != e {
log.Errorln("_make_put_or_patch_handle:> failure:", e)
return Failure_Response(FATAL_DB_WRITE_FAILED, fmt.Sprintf("%s", e))
}
result = &RestletResult{
Code:SUCCESS_OK,
Model:entityType.Name(),
Data: map[string]interface{}{"Updated": n},
}
return result, nil return result, nil
} }
return f return f

Loading…
Cancel
Save