测试接口成功

This commit is contained in:
eson 2023-09-10 02:58:56 +08:00
parent 00be036e59
commit 14f0dd2b22
3 changed files with 93 additions and 6 deletions

View File

@ -228,7 +228,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT
} else {
err = fssql.MetadataPATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId)
err = fssql.MetadataModulePATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId)
if err != nil {
return err
}

View File

@ -47,7 +47,7 @@ import (
// }
// tableStructPointer 表结构 updateMetadata 可以是json的[]byte也可以结构体
func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, value ...any) error {
func MetadataModulePATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, values ...any) error {
stype := reflect.TypeOf(tableStructPointer)
if stype.Kind() == reflect.Pointer {
stype = stype.Elem()
@ -75,14 +75,73 @@ func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMet
}
}
var values []any
var args []any
values = append(values, metadata, metadata)
values = append(values, value...)
args = append(args, metadata, metadata)
args = append(args, values...)
updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module, WhereKeysCond)
// logx.Error(updatesql)
err = tx.Exec(updatesql, values).Error
err = tx.Exec(updatesql, args...).Error
if err != nil {
return err
}
return nil
}
func MetadataResourcePATCH(tx *gorm.DB, rid string, updateMetadata any, values ...any) error {
// stype := reflect.TypeOf(tableStructPointer)
// if stype.Kind() == reflect.Pointer {
// stype = stype.Elem()
// }
var updatesql string
if len(values) == 0 {
updatesql = `UPDATE fs_resource
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
WHERE resource_id = '%s';`
} else {
cond, ok := values[0].(string)
if !ok {
return fmt.Errorf("values[0] must be wherecond")
}
updatesql = `UPDATE fs_resource
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
cond, ok := values[0].(string)
WHERE resource_id = '%s'` + "and " + cond + `;`
}
var err error
var metadata []byte
switch mdata := updateMetadata.(type) {
case []byte:
metadata = mdata
case string:
metadata = []byte(mdata)
default:
metadata, err = json.Marshal(updateMetadata)
if err != nil {
return err
}
}
var args []any
args = append(args, metadata, metadata)
args = append(args, values...)
updatesql = fmt.Sprintf(updatesql, rid)
// logx.Error(updatesql)
err = tx.Exec(updatesql, args...).Error
if err != nil {
return err
}

View File

@ -0,0 +1,28 @@
package fssql_test
import (
"fusenapi/initalize"
"fusenapi/model/gmodel"
"fusenapi/utils/fssql"
"testing"
)
func TestCase1(t *testing.T) {
u := gmodel.UserProfile{
FirstName: "h",
LastName: "sm",
}
conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen")
err := fssql.MetadataModulePATCH(conn, "logo", gmodel.FsChangeCode{}, u, "id = ?", 6)
if err != nil {
panic(err)
}
err = fssql.MetadataResourcePATCH(conn,
"f8932c0379fa5aa3397dc0a963696ca90536cc273ea10119b0137fd15ecfe673", u)
if err != nil {
panic(err)
}
}