Realm devkit

This commit is contained in:
2024-09-11 22:58:28 +08:00
parent 395f97f3b5
commit d734d617bf
5 changed files with 67 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package hyper
import (
"gorm.io/gorm"
"strings"
"time"
@ -51,10 +52,10 @@ func (v *HyperConn) AuthMiddleware(c *fiber.Ctx) error {
return c.Next()
}
func LinkAccountMiddleware[T any](model any, adaptor func(u BaseUser) T) func(c *fiber.Ctx) error {
func LinkAccountMiddleware[T any](tx *gorm.DB, model any, adaptor func(u BaseUser) T) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
if val, ok := c.Locals("p_user").(*proto.UserInfo); ok {
if account, err := LinkAccount(model, val); err != nil {
if account, err := LinkAccount(tx, model, val); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
c.Locals("user", adaptor(account))

View File

@ -3,7 +3,6 @@ package hyper
import (
"errors"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/database"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"github.com/samber/lo"
"gorm.io/gorm"
@ -23,12 +22,14 @@ type BaseUser struct {
AutomatedBy *uint `json:"automated_by"`
}
func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) {
// LinkAccount will help you build a BaseUser model from proto.UserInfo
// And also will help you to update the info in your database, so that this function requires a database context
func LinkAccount(tx *gorm.DB, model any, userinfo *proto.UserInfo) (BaseUser, error) {
var account BaseUser
if userinfo == nil {
return account, fmt.Errorf("remote userinfo was not found")
}
if err := database.C.Where("id = ?", userinfo.GetId()).Model(model).First(&account).Error; err != nil {
if err := tx.Where("id = ?", userinfo.GetId()).Model(model).First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
account = BaseUser{
BaseModel: BaseModel{
@ -47,7 +48,7 @@ func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) {
if userinfo.AutomatedBy != nil {
account.AutomatedBy = lo.ToPtr(uint(*userinfo.AutomatedBy))
}
return account, database.C.Model(model).Save(&account).Error
return account, tx.Model(model).Save(&account).Error
}
return account, err
}
@ -68,7 +69,7 @@ func LinkAccount(model any, userinfo *proto.UserInfo) (BaseUser, error) {
var err error
if !reflect.DeepEqual(prev, account) {
err = database.C.Model(model).Save(&account).Error
err = tx.Model(model).Save(&account).Error
}
return account, err

29
pkg/hyper/realm.go Normal file
View File

@ -0,0 +1,29 @@
package hyper
import "git.solsynth.dev/hydrogen/dealer/pkg/proto"
type BaseRealm struct {
BaseModel
Alias string `json:"alias"`
Name string `json:"name"`
Description string `json:"description"`
Avatar string `json:"avatar"`
Banner string `json:"banner"`
IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"`
}
// LinkRealm will help you build a BaseRealm model from proto.RealmInfo
// WARNING This function doesn't like the LinkAccount, it will not help you deal the database stuff
func LinkRealm(info *proto.RealmInfo) BaseRealm {
return BaseRealm{
Alias: info.Alias,
Name: info.Name,
Description: info.Description,
Avatar: info.Avatar,
Banner: info.Banner,
IsPublic: info.IsPublic,
IsCommunity: info.IsCommunity,
}
}