Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
laodaming 2023-09-11 12:17:13 +08:00
commit ce394848fd
4 changed files with 98 additions and 15 deletions

View File

@ -204,7 +204,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT
// txUserInfo.Where("user_id = ?", user.Id).Row().Err()
now := time.Now()
now := time.Now().UTC()
uinfo := &FsUserInfo{
Module: FsString("profile"),
UserId: &user.Id,
@ -218,21 +218,17 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT
// txUserInfo.Statement.Table
if err != nil {
if err == gorm.ErrRecordNotFound {
err = txUserInfo.Create(uinfo).Error
if err == gorm.ErrRecordNotFound {
return nil
}
}
} else {
err = fssql.MetadataPATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId)
err = fssql.MetadataModulePATCH(txUserInfo, "profile", FsUserInfo{}, metadata, "user_id = ?", *uinfo.UserId)
if err != nil {
return err
}
}
return err
})

View File

@ -89,10 +89,11 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
logx.Error(err)
return resp.SetStatus(basic.CodeOK)
}
gemail := gresult.Get("email").String()
l.registerInfo = &auth.RegisterToken{
Password: base64.RawURLEncoding.EncodeToString(nonce),
Platform: string(auth.PLATFORM_GOOGLE),
Email: gemail,
OperateType: auth.OpTypeRegister,
TraceId: uuid.NewString(),
CreateAt: time.Now().UTC(),
@ -110,8 +111,7 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
}
l.registerToken = token
l.defaultEmail = gresult.Get("email").String()
l.defaultEmail = gemail
return resp.SetStatus(basic.CodeOK)
}
@ -142,7 +142,7 @@ func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request
l.token,
l.isRegistered,
l.registerToken,
l.registerInfo.Email,
l.defaultEmail,
)
html := fmt.Sprintf(`

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)
}
}