2024-02-05 07:51:31 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-05-04 14:22:58 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2024-04-06 04:05:54 +00:00
|
|
|
"fmt"
|
2024-03-20 12:57:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/database"
|
2024-05-04 14:22:58 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/grpc"
|
2024-03-20 12:57:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/models"
|
2024-05-04 14:22:58 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/grpc/proto"
|
2024-02-11 05:12:37 +00:00
|
|
|
"github.com/samber/lo"
|
2024-05-04 14:22:58 +00:00
|
|
|
"gorm.io/gorm"
|
2024-02-05 07:51:31 +00:00
|
|
|
)
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func GetRealm(id uint) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
|
|
|
response, err := grpc.Realms.GetRealm(context.Background(), &proto.RealmLookupRequest{
|
|
|
|
Id: lo.ToPtr(uint64(id)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
2024-02-05 11:25:56 +00:00
|
|
|
}
|
2024-05-04 14:22:58 +00:00
|
|
|
return LinkRealm(response)
|
2024-02-05 11:25:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func GetRealmWithAlias(alias string) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
|
|
|
response, err := grpc.Realms.GetRealm(context.Background(), &proto.RealmLookupRequest{
|
|
|
|
Alias: &alias,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|
2024-05-04 14:22:58 +00:00
|
|
|
return LinkRealm(response)
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberResponse, error) {
|
|
|
|
response, err := grpc.Realms.GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
|
|
|
RealmId: uint64(realmId),
|
|
|
|
UserId: lo.ToPtr(uint64(userId)),
|
2024-02-11 05:12:37 +00:00
|
|
|
})
|
2024-05-04 14:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return response, nil
|
2024-02-11 05:12:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func ListRealmMember(realmId uint) ([]*proto.RealmMemberResponse, error) {
|
|
|
|
response, err := grpc.Realms.ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
|
|
|
RealmId: uint64(realmId),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return response.Data, nil
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func LinkRealm(info *proto.RealmResponse) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
|
|
|
if info == nil {
|
|
|
|
return realm, fmt.Errorf("remote realm info was not found")
|
2024-03-23 05:05:05 +00:00
|
|
|
}
|
2024-05-04 14:22:58 +00:00
|
|
|
if err := database.C.Where(&models.Realm{
|
|
|
|
ExternalID: uint(info.Id),
|
|
|
|
}).First(&realm).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
realm = models.Realm{
|
|
|
|
Alias: info.Alias,
|
|
|
|
Name: info.Name,
|
|
|
|
Description: info.Description,
|
|
|
|
IsPublic: info.IsPublic,
|
|
|
|
IsCommunity: info.IsCommunity,
|
|
|
|
ExternalID: uint(info.Id),
|
|
|
|
}
|
|
|
|
return realm, database.C.Save(&realm).Error
|
|
|
|
}
|
|
|
|
return realm, err
|
2024-02-09 07:19:43 +00:00
|
|
|
}
|
2024-05-04 14:22:58 +00:00
|
|
|
return realm, nil
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|