Realm members api

This commit is contained in:
LittleSheep 2024-03-23 13:05:05 +08:00
parent 2b4ffb6811
commit 311700db04
6 changed files with 117 additions and 88 deletions

View File

@ -25,4 +25,6 @@ type RealmMember struct {
RealmID uint `json:"realm_id"` RealmID uint `json:"realm_id"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
Realm Realm `json:"realm"`
Account Account `json:"account"`
} }

View File

@ -0,0 +1,86 @@
package server
import (
"git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/models"
"git.solsynth.dev/hydrogen/interactive/pkg/services"
"github.com/gofiber/fiber/v2"
)
func listRealmMembers(c *fiber.Ctx) error {
realmId, _ := c.ParamsInt("realmId", 0)
if members, err := services.ListRealmMember(uint(realmId)); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.JSON(members)
}
}
func inviteRealm(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
realmId, _ := c.ParamsInt("realmId", 0)
var data struct {
AccountName string `json:"account_name" validate:"required"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var realm models.Realm
if err := database.C.Where(&models.Realm{
BaseModel: models.BaseModel{ID: uint(realmId)},
AccountID: user.ID,
}).First(&realm).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
var account models.Account
if err := database.C.Where(&models.Account{
Name: data.AccountName,
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := services.InviteRealmMember(account, realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}
func kickRealm(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
realmId, _ := c.ParamsInt("realmId", 0)
var data struct {
AccountName string `json:"account_name" validate:"required"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var realm models.Realm
if err := database.C.Where(&models.Realm{
BaseModel: models.BaseModel{ID: uint(realmId)},
AccountID: user.ID,
}).First(&realm).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
var account models.Account
if err := database.C.Where(&models.Account{
Name: data.AccountName,
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := services.KickRealmMember(account, realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}

View File

@ -75,74 +75,6 @@ func createRealm(c *fiber.Ctx) error {
return c.JSON(realm) return c.JSON(realm)
} }
func inviteRealm(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
realmId, _ := c.ParamsInt("realmId", 0)
var data struct {
AccountName string `json:"account_name" validate:"required"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var realm models.Realm
if err := database.C.Where(&models.Realm{
BaseModel: models.BaseModel{ID: uint(realmId)},
AccountID: user.ID,
}).First(&realm).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
var account models.Account
if err := database.C.Where(&models.Account{
Name: data.AccountName,
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := services.InviteRealmMember(account, realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}
func kickRealm(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
realmId, _ := c.ParamsInt("realmId", 0)
var data struct {
AccountName string `json:"account_name" validate:"required"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var realm models.Realm
if err := database.C.Where(&models.Realm{
BaseModel: models.BaseModel{ID: uint(realmId)},
AccountID: user.ID,
}).First(&realm).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
var account models.Account
if err := database.C.Where(&models.Account{
Name: data.AccountName,
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := services.KickRealmMember(account, realm); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}
func editRealm(c *fiber.Ctx) error { func editRealm(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("principal").(models.Account)
id, _ := c.ParamsInt("realmId", 0) id, _ := c.ParamsInt("realmId", 0)

View File

@ -105,15 +105,19 @@ func NewServer() {
api.Put("/categories/:categoryId", authMiddleware, editCategory) api.Put("/categories/:categoryId", authMiddleware, editCategory)
api.Delete("/categories/:categoryId", authMiddleware, deleteCategory) api.Delete("/categories/:categoryId", authMiddleware, deleteCategory)
api.Get("/realms", listRealm) realms := api.Group("/realms").Name("Realms API")
api.Get("/realms/me", authMiddleware, listOwnedRealm) {
api.Get("/realms/me/available", authMiddleware, listAvailableRealm) realms.Get("/", listRealm)
api.Get("/realms/:realmId", getRealm) realms.Get("/me", authMiddleware, listOwnedRealm)
api.Post("/realms", authMiddleware, createRealm) realms.Get("/me/available", authMiddleware, listAvailableRealm)
api.Post("/realms/:realmId/invite", authMiddleware, inviteRealm) realms.Get("/:realmId", getRealm)
api.Post("/realms/:realmId/kick", authMiddleware, kickRealm) realms.Get("/:realmId/members", listRealmMembers)
api.Put("/realms/:realmId", authMiddleware, editRealm) realms.Post("/", authMiddleware, createRealm)
api.Delete("/realms/:realmId", authMiddleware, deleteRealm) realms.Post("/:realmId/invite", authMiddleware, inviteRealm)
realms.Post("/:realmId/kick", authMiddleware, kickRealm)
realms.Put("/:realmId", authMiddleware, editRealm)
realms.Delete("/:realmId", authMiddleware, deleteRealm)
}
} }
A.Use("/", cache.New(cache.Config{ A.Use("/", cache.New(cache.Config{

View File

@ -62,6 +62,18 @@ func NewRealm(user models.Account, name, description string, realmType int) (mod
return realm, err return realm, err
} }
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
}
func InviteRealmMember(user models.Account, target models.Realm) error { func InviteRealmMember(user models.Account, target models.Realm) error {
member := models.RealmMember{ member := models.RealmMember{
RealmID: target.ID, RealmID: target.ID,

View File

@ -4,15 +4,8 @@
<v-card-text> <v-card-text>
<v-text-field label="Name" variant="outlined" density="comfortable" v-model="data.name" /> <v-text-field label="Name" variant="outlined" density="comfortable" v-model="data.name" />
<v-textarea label="Description" variant="outlined" density="comfortable" v-model="data.description" /> <v-textarea label="Description" variant="outlined" density="comfortable" v-model="data.description" />
<v-select <v-select label="Realm type" item-title="label" item-value="value" variant="outlined" density="comfortable"
label="Realm type" :items="realmTypeOptions" v-model="data.realm_type" />
item-title="label"
item-value="value"
variant="outlined"
density="comfortable"
:items="realmTypeOptions"
v-model="data.realm_type"
/>
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
<v-spacer></v-spacer> <v-spacer></v-spacer>