Interactive/pkg/services/realms.go

114 lines
2.5 KiB
Go
Raw Normal View History

2024-02-05 07:51:31 +00:00
package services
import (
2024-03-20 12:57:21 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/models"
2024-02-11 05:12:37 +00:00
"github.com/samber/lo"
2024-02-05 07:51:31 +00:00
)
2024-02-05 11:25:56 +00:00
func ListRealm() ([]models.Realm, error) {
var realms []models.Realm
if err := database.C.Find(&realms).Error; err != nil {
return realms, err
}
return realms, nil
}
func ListRealmWithUser(user models.Account) ([]models.Realm, error) {
2024-02-05 07:51:31 +00:00
var realms []models.Realm
if err := database.C.Where(&models.Realm{AccountID: user.ID}).Find(&realms).Error; err != nil {
return realms, err
}
return realms, nil
}
2024-02-11 05:12:37 +00:00
func ListRealmIsAvailable(user models.Account) ([]models.Realm, error) {
var realms []models.Realm
var members []models.RealmMember
if err := database.C.Where(&models.RealmMember{
AccountID: user.ID,
}).Find(&members).Error; err != nil {
return realms, err
}
idx := lo.Map(members, func(item models.RealmMember, index int) uint {
return item.RealmID
})
if err := database.C.Where(&models.Realm{
2024-03-17 14:08:33 +00:00
RealmType: models.RealmTypePublic,
2024-02-11 05:12:37 +00:00
}).Or("id IN ?", idx).Find(&realms).Error; err != nil {
return realms, err
}
return realms, nil
}
2024-03-17 14:08:33 +00:00
func NewRealm(user models.Account, name, description string, realmType int) (models.Realm, error) {
2024-02-05 07:51:31 +00:00
realm := models.Realm{
Name: name,
Description: description,
AccountID: user.ID,
2024-03-17 14:08:33 +00:00
RealmType: realmType,
2024-02-09 04:36:39 +00:00
Members: []models.RealmMember{
{AccountID: user.ID},
},
2024-02-05 07:51:31 +00:00
}
err := database.C.Save(&realm).Error
return realm, err
}
2024-03-23 05:05:05 +00:00
func ListRealmMember(realmId uint) ([]models.RealmMember, error) {
var members []models.RealmMember
if err := database.C.Where(&models.RealmMember{
RealmID: realmId,
}).Find(&members).Error; err != nil {
return members, err
}
return members, nil
}
2024-02-09 04:36:39 +00:00
func InviteRealmMember(user models.Account, target models.Realm) error {
member := models.RealmMember{
RealmID: target.ID,
AccountID: user.ID,
}
err := database.C.Save(&member).Error
return err
}
2024-02-09 07:19:43 +00:00
func KickRealmMember(user models.Account, target models.Realm) error {
var member models.RealmMember
if err := database.C.Where(&models.RealmMember{
RealmID: target.ID,
AccountID: user.ID,
}).First(&member).Error; err != nil {
return err
}
return database.C.Delete(&member).Error
}
2024-03-17 14:08:33 +00:00
func EditRealm(realm models.Realm, name, description string, realmType int) (models.Realm, error) {
2024-02-05 07:51:31 +00:00
realm.Name = name
realm.Description = description
2024-03-17 14:08:33 +00:00
realm.RealmType = realmType
2024-02-05 07:51:31 +00:00
err := database.C.Save(&realm).Error
return realm, err
}
func DeleteRealm(realm models.Realm) error {
return database.C.Delete(&realm).Error
}