This commit is contained in:
eson 2023-09-08 18:56:17 +08:00
parent a4363d0a47
commit f213f1c85a
4 changed files with 140 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"fusenapi/server/fssql"
"fusenapi/utils/auth"
"time"
@ -213,7 +214,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT
Utime: &now,
}
err = txUserInfo.Where("user_id = ?", uinfo.UserId).Take(nil).Error
err = txUserInfo.Where("module = 'profile' and user_id = ?", uinfo.UserId).Take(nil).Error
// txUserInfo.Statement.Table
if err != nil {
@ -224,19 +225,14 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT
return nil
}
}
} else {
updatesql := `UPDATE %s
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
WHERE id = ?;`
updatesql = fmt.Sprintf(updatesql, txUserInfo.Statement.Table)
logx.Error(updatesql)
err = txUserInfo.Raw(updatesql, metadata, metadata, uinfo.UserId).Error
err = fssql.MetadataPATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId)
if err != nil {
return err
}
}
return err
})

View File

@ -111,7 +111,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
return resp.SetStatusWithMessage(basic.CodeOAuthConfirmationTimeoutErr, "Verification links expire after 30 minute.")
}
logx.Error(token.Platform)
// logx.Error(token.Platform)
switch token.Platform {
case string(auth.PLATFORM_GOOGLE):
// 谷歌平台的注册流程

View File

@ -2,12 +2,16 @@ package logic
import (
"bytes"
"encoding/json"
"fmt"
"fusenapi/initalize"
"fusenapi/model/gmodel"
"fusenapi/utils/wevent"
"io"
"log"
"net/http"
"net/smtp"
"reflect"
"testing"
"github.com/474420502/requests"
@ -130,3 +134,41 @@ func TestSender(t *testing.T) {
log.Println(result)
}
func TestSql(t *testing.T) {
type MetaData struct {
Key string `gorm:"key" json:"key"`
Test string `gorm:"test" json:"test"`
}
conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen")
log.Println(conn.Model(&gmodel.FsUser{}))
log.Println(conn.NamingStrategy.TableName(reflect.TypeOf(gmodel.FsUser{}).Name()))
updatesql := `UPDATE %s
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
WHERE id = ?;`
metadata := &MetaData{
Key: "mkey",
Test: "testv",
}
data, err := json.Marshal(metadata)
if err != nil {
logx.Error(err)
panic(err)
}
updatesql = fmt.Sprintf(updatesql, "fusen.fs_change_code")
log.Println(updatesql)
err = conn.Exec(updatesql, data, data, 6).Error
if err != nil {
logx.Error(err)
panic(err)
}
}

91
server/fssql/metadata.go Normal file
View File

@ -0,0 +1,91 @@
package fssql
import (
"encoding/json"
"fmt"
"reflect"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
// func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, rawID int64) error {
// stype := reflect.TypeOf(tableStructPointer)
// if stype.Kind() == reflect.Pointer {
// stype = stype.Elem()
// }
// updatesql := `UPDATE %s
// SET metadata = CASE
// WHEN metadata IS NULL THEN ?
// ELSE JSON_MERGE_PATCH(metadata, ?)
// END
// WHERE module = '%s' and id = ?;`
// 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
// }
// }
// updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module)
// logx.Error(updatesql)
// err = tx.Exec(updatesql, metadata, metadata, rawID).Error
// if err != nil {
// return err
// }
// return nil
// }
func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, value ...any) error {
stype := reflect.TypeOf(tableStructPointer)
if stype.Kind() == reflect.Pointer {
stype = stype.Elem()
}
updatesql := `UPDATE %s
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
WHERE module = '%s' and %s;`
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 values []any
values = append(values, metadata, metadata)
values = append(values, value...)
updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module, WhereKeysCond)
logx.Error(updatesql)
err = tx.Exec(updatesql, values).Error
if err != nil {
return err
}
return nil
}