添加mongodb使用,添加用户前端配置的上传及获取接口

This commit is contained in:
junleea 2025-05-20 20:01:43 +08:00
parent a265cd459b
commit e983317f5b
2 changed files with 18 additions and 6 deletions

View File

@ -5,8 +5,8 @@ import (
"context" "context"
"fmt" "fmt"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"gorm.io/gorm" "gorm.io/gorm"
"log"
) )
type User struct { type User struct {
@ -297,11 +297,16 @@ func CreateUserUIConfigInfo(config proto.UserUIConfigInfo) (string, error) {
return "", err return "", err
} }
fmt.Println("Inserted a single document:", res.InsertedID) fmt.Println("Inserted a single document:", res.InsertedID)
return res.InsertedID.(string), nil // 类型安全转换
insertedID, ok := res.InsertedID.(primitive.ObjectID)
if !ok {
return primitive.NilObjectID.String(), fmt.Errorf("意外的ID类型: %T", res.InsertedID)
}
return insertedID.String(), nil
} }
func GetUserUIConfigInfo(userID int) (proto.UserUIConfigInfo, error) { func GetUserUIConfigInfo(userID int) (proto.UserUIConfigInfo, error) {
log.Println("get user ui config info database:", proto.Config.MONGO_DATABASE, " collection:", UserUIConfigCollection) //log.Println("get user ui config info database:", proto.Config.MONGO_DATABASE, " collection:", UserUIConfigCollection)
collection := mongoClient.Database(proto.Config.MONGO_DATABASE).Collection(UserUIConfigCollection) collection := mongoClient.Database(proto.Config.MONGO_DATABASE).Collection(UserUIConfigCollection)
var config proto.UserUIConfigInfo var config proto.UserUIConfigInfo
err := collection.FindOne(context.TODO(), bson.M{"user_id": userID}).Decode(&config) err := collection.FindOne(context.TODO(), bson.M{"user_id": userID}).Decode(&config)

View File

@ -464,7 +464,15 @@ func GetUserUIConfigInfo(userID int) proto.UserUIConfigInfo {
userConfig, err := dao.GetUserUIConfigInfo(userID) userConfig, err := dao.GetUserUIConfigInfo(userID)
if err != nil { if err != nil {
log.Println("GetUserUIConfigInfo error:", err) log.Println("GetUserUIConfigInfo error:", err)
return proto.UserUIConfigInfo{} } else {
userConfig.UserID = userID
//没有则插入
id, err2 := dao.CreateUserUIConfigInfo(userConfig)
if err2 != nil {
log.Println("InsertUserUIConfigInfo error:", err.Error())
} else {
log.Println("InsertUserUIConfigInfo success, id:", id, "config:", userConfig)
}
} }
return userConfig return userConfig
} }
@ -475,14 +483,13 @@ func SetUserUIConfigInfo(userID int, config proto.UserUIConfigInfo) error {
userConfig, err := dao.GetUserUIConfigInfo(userID) userConfig, err := dao.GetUserUIConfigInfo(userID)
if err != nil { if err != nil {
log.Println("SetUserUIConfigInfo error:", err) log.Println("SetUserUIConfigInfo error:", err)
return err
} }
config.UserID = userID config.UserID = userID
if userConfig.UserID == 0 { if userConfig.UserID == 0 {
//没有则插入 //没有则插入
id, err2 := dao.CreateUserUIConfigInfo(config) id, err2 := dao.CreateUserUIConfigInfo(config)
if err2 != nil { if err2 != nil {
log.Println("InsertUserUIConfigInfo error:", err) log.Println("InsertUserUIConfigInfo error:", err.Error())
return err return err
} else { } else {
log.Println("InsertUserUIConfigInfo success, id:", id, "config:", config) log.Println("InsertUserUIConfigInfo success, id:", id, "config:", config)