Interactive/pkg/internal/services/realms.go

68 lines
1.9 KiB
Go
Raw Normal View History

2024-02-05 07:51:31 +00:00
package services
import (
"context"
2024-07-16 02:53:02 +00:00
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
2024-02-11 05:12:37 +00:00
"github.com/samber/lo"
2024-09-17 08:53:43 +00:00
"github.com/spf13/viper"
2024-02-05 07:51:31 +00:00
)
2024-10-14 13:40:28 +00:00
func GetRealmWithID(id uint) (models.Realm, error) {
var realm models.Realm
2024-07-16 02:53:02 +00:00
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return realm, err
}
2024-07-16 02:53:02 +00:00
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
Id: lo.ToPtr(uint64(id)),
})
if err != nil {
return realm, err
}
2024-09-17 08:53:43 +00:00
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) {
var realm models.Realm
2024-07-16 02:53:02 +00:00
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return realm, err
}
2024-07-16 02:53:02 +00:00
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
Alias: &alias,
})
if err != nil {
return realm, err
2024-02-05 07:51:31 +00:00
}
2024-09-17 08:53:43 +00:00
prefix := viper.GetString("database.prefix")
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
return models.Realm{BaseRealm: rm}, err
2024-02-05 07:51:31 +00:00
}
2024-07-16 02:53:02 +00:00
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
2024-06-22 17:07:03 +00:00
var realm models.Realm
if err := database.C.Where("id = ?", realmId).First(&realm).Error; err != nil {
return nil, err
}
2024-07-16 02:53:02 +00:00
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return nil, err
}
2024-07-16 02:53:02 +00:00
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
2024-09-17 08:53:43 +00:00
RealmId: lo.ToPtr(uint64(realm.ID)),
UserId: lo.ToPtr(uint64(userId)),
2024-02-11 05:12:37 +00:00
})
if err != nil {
return nil, err
} else {
return response, nil
2024-02-11 05:12:37 +00:00
}
}