From 277f1ee05f315444822f239d567369e2e94a05ee Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 17 Sep 2024 16:53:43 +0800 Subject: [PATCH] :arrow_up: Upgrade dealer --- go.mod | 2 +- go.sum | 2 ++ pkg/internal/services/realms.go | 47 ++++++--------------------------- 3 files changed, 11 insertions(+), 40 deletions(-) diff --git a/go.mod b/go.mod index 7e02997..f0fb223 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.solsynth.dev/hydrogen/interactive go 1.21.6 require ( - git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3 + git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 github.com/json-iterator/go v1.1.12 diff --git a/go.sum b/go.sum index 66b1c39..ea71c11 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3 h1:ucko8l9rC/XD/Gfk9lcQftyqb9tvHjL5f+aW3ydf1OI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f h1:3jLpcws4/zmNUA60w1RtAtGNjcQd5NZCcbW5HQcUcvw= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/pkg/internal/services/realms.go b/pkg/internal/services/realms.go index b072d21..b327b18 100644 --- a/pkg/internal/services/realms.go +++ b/pkg/internal/services/realms.go @@ -2,9 +2,6 @@ package services import ( "context" - "errors" - "fmt" - "reflect" "git.solsynth.dev/hydrogen/dealer/pkg/hyper" "git.solsynth.dev/hydrogen/dealer/pkg/proto" @@ -12,7 +9,7 @@ import ( "git.solsynth.dev/hydrogen/interactive/pkg/internal/gap" "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" "github.com/samber/lo" - "gorm.io/gorm" + "github.com/spf13/viper" ) func GetRealmWithExtID(id uint) (models.Realm, error) { @@ -27,7 +24,9 @@ func GetRealmWithExtID(id uint) (models.Realm, error) { if err != nil { return realm, err } - return LinkRealm(response) + prefix := viper.GetString("database.prefix") + rm, err := hyper.LinkRealm(database.C, prefix+"realms", response) + return models.Realm{BaseRealm: rm}, err } func GetRealmWithAlias(alias string) (models.Realm, error) { @@ -42,7 +41,9 @@ func GetRealmWithAlias(alias string) (models.Realm, error) { if err != nil { return realm, err } - return LinkRealm(response) + prefix := viper.GetString("database.prefix") + rm, err := hyper.LinkRealm(database.C, prefix+"realms", response) + return models.Realm{BaseRealm: rm}, err } func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) { @@ -55,7 +56,7 @@ func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) { return nil, err } response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{ - RealmId: uint64(realm.ID), + RealmId: lo.ToPtr(uint64(realm.ID)), UserId: lo.ToPtr(uint64(userId)), }) if err != nil { @@ -64,35 +65,3 @@ func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) { return response, nil } } - -func LinkRealm(info *proto.RealmInfo) (models.Realm, error) { - var realm models.Realm - if info == nil { - return realm, fmt.Errorf("remote realm info was not found") - } - if err := database.C.Where("id = ?", info.Id).First(&realm).Error; err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - realm = models.Realm{ - BaseRealm: hyper.LinkRealm(info), - } - return realm, database.C.Save(&realm).Error - } - return realm, err - } - - prev := realm - realm.Alias = info.Alias - realm.Name = info.Name - realm.Description = info.Description - realm.Avatar = info.Avatar - realm.Banner = info.Banner - realm.IsPublic = info.IsPublic - realm.IsCommunity = info.IsCommunity - - var err error - if !reflect.DeepEqual(prev, realm) { - err = database.C.Save(&realm).Error - } - - return realm, err -}