From 311700db04181c024d49f5aade311cca3aae28d2 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 23 Mar 2024 13:05:05 +0800 Subject: [PATCH] :sparkles: Realm members api --- pkg/models/realms.go | 6 +- pkg/server/realm_members_api.go | 86 +++++++++++++++++++ pkg/server/realms_api.go | 68 --------------- pkg/server/startup.go | 22 +++-- pkg/services/realms.go | 12 +++ .../src/components/realms/RealmEditor.vue | 11 +-- 6 files changed, 117 insertions(+), 88 deletions(-) create mode 100644 pkg/server/realm_members_api.go diff --git a/pkg/models/realms.go b/pkg/models/realms.go index c5482a4..084a85b 100644 --- a/pkg/models/realms.go +++ b/pkg/models/realms.go @@ -23,6 +23,8 @@ type Realm struct { type RealmMember struct { BaseModel - RealmID uint `json:"realm_id"` - AccountID uint `json:"account_id"` + RealmID uint `json:"realm_id"` + AccountID uint `json:"account_id"` + Realm Realm `json:"realm"` + Account Account `json:"account"` } diff --git a/pkg/server/realm_members_api.go b/pkg/server/realm_members_api.go new file mode 100644 index 0000000..cdb561c --- /dev/null +++ b/pkg/server/realm_members_api.go @@ -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) + } +} diff --git a/pkg/server/realms_api.go b/pkg/server/realms_api.go index 5e1ea8c..4377d19 100644 --- a/pkg/server/realms_api.go +++ b/pkg/server/realms_api.go @@ -75,74 +75,6 @@ func createRealm(c *fiber.Ctx) error { 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 { user := c.Locals("principal").(models.Account) id, _ := c.ParamsInt("realmId", 0) diff --git a/pkg/server/startup.go b/pkg/server/startup.go index f3159af..12bde2b 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -105,15 +105,19 @@ func NewServer() { api.Put("/categories/:categoryId", authMiddleware, editCategory) api.Delete("/categories/:categoryId", authMiddleware, deleteCategory) - api.Get("/realms", listRealm) - api.Get("/realms/me", authMiddleware, listOwnedRealm) - api.Get("/realms/me/available", authMiddleware, listAvailableRealm) - api.Get("/realms/:realmId", getRealm) - api.Post("/realms", authMiddleware, createRealm) - api.Post("/realms/:realmId/invite", authMiddleware, inviteRealm) - api.Post("/realms/:realmId/kick", authMiddleware, kickRealm) - api.Put("/realms/:realmId", authMiddleware, editRealm) - api.Delete("/realms/:realmId", authMiddleware, deleteRealm) + realms := api.Group("/realms").Name("Realms API") + { + realms.Get("/", listRealm) + realms.Get("/me", authMiddleware, listOwnedRealm) + realms.Get("/me/available", authMiddleware, listAvailableRealm) + realms.Get("/:realmId", getRealm) + realms.Get("/:realmId/members", listRealmMembers) + realms.Post("/", authMiddleware, createRealm) + 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{ diff --git a/pkg/services/realms.go b/pkg/services/realms.go index 36559a5..5de70ff 100644 --- a/pkg/services/realms.go +++ b/pkg/services/realms.go @@ -62,6 +62,18 @@ func NewRealm(user models.Account, name, description string, realmType int) (mod 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 { member := models.RealmMember{ RealmID: target.ID, diff --git a/pkg/views/src/components/realms/RealmEditor.vue b/pkg/views/src/components/realms/RealmEditor.vue index 2a60f6a..d20e60d 100644 --- a/pkg/views/src/components/realms/RealmEditor.vue +++ b/pkg/views/src/components/realms/RealmEditor.vue @@ -4,15 +4,8 @@ - +