💥 Pagination fetching realm member api

🐛 Fix remove realm member has no memberId path param
This commit is contained in:
LittleSheep 2024-12-01 11:32:54 +08:00
parent e0c9646c98
commit 20ff541ab4
4 changed files with 29 additions and 8 deletions

8
.idea/workspace.xml generated
View File

@ -4,8 +4,10 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Add realm member support both account name and id"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Prevent user adding a user twice into a realm">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/realm_members_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/realm_members_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/realms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/realms.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/realms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/realms.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -159,7 +161,6 @@
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" /> <option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<MESSAGE value=":sparkles: Authkit can get user" />
<MESSAGE value=":bug: Fix auth cache" /> <MESSAGE value=":bug: Fix auth cache" />
<MESSAGE value=":sparkles: Allow get realm by numeric auto increment id" /> <MESSAGE value=":sparkles: Allow get realm by numeric auto increment id" />
<MESSAGE value=":sparkles: Allow access user info via numeric id" /> <MESSAGE value=":sparkles: Allow access user info via numeric id" />
@ -184,7 +185,8 @@
<MESSAGE value=":boom: Move remove member api arguments from body to querystring just as messaging" /> <MESSAGE value=":boom: Move remove member api arguments from body to querystring just as messaging" />
<MESSAGE value=":bug: Hotfix previous commit compile issue" /> <MESSAGE value=":bug: Hotfix previous commit compile issue" />
<MESSAGE value=":sparkles: Add realm member support both account name and id" /> <MESSAGE value=":sparkles: Add realm member support both account name and id" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Add realm member support both account name and id" /> <MESSAGE value=":bug: Prevent user adding a user twice into a realm" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Prevent user adding a user twice into a realm" />
<option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" /> <option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">

View File

@ -124,13 +124,13 @@ func MapAPIs(app *fiber.App, baseURL string) {
realms.Get("/me", listOwnedRealm) realms.Get("/me", listOwnedRealm)
realms.Get("/me/available", listAvailableRealm) realms.Get("/me/available", listAvailableRealm)
realms.Get("/:realm", getRealm) realms.Get("/:realm", getRealm)
realms.Get("/:realm/members/:memberId", listRealmMembers) realms.Get("/:realm/members", listRealmMembers)
realms.Get("/:realm/members/me", getMyRealmMember) realms.Get("/:realm/members/me", getMyRealmMember)
realms.Post("/", createRealm) realms.Post("/", createRealm)
realms.Put("/:realmId", editRealm) realms.Put("/:realmId", editRealm)
realms.Delete("/:realmId", deleteRealm) realms.Delete("/:realmId", deleteRealm)
realms.Post("/:realm/members", addRealmMember) realms.Post("/:realm/members", addRealmMember)
realms.Delete("/:realm/members", removeRealmMember) realms.Delete("/:realm/members/:memberId", removeRealmMember)
realms.Delete("/:realm/members/me", leaveRealm) realms.Delete("/:realm/members/me", leaveRealm)
} }

View File

@ -11,13 +11,20 @@ import (
func listRealmMembers(c *fiber.Ctx) error { func listRealmMembers(c *fiber.Ctx) error {
alias := c.Params("realm") alias := c.Params("realm")
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
if realm, err := services.GetRealmWithAlias(alias); err != nil { if realm, err := services.GetRealmWithAlias(alias); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if members, err := services.ListRealmMember(realm.ID); err != nil { } else if count, err := services.CountRealmMember(realm.ID); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if members, err := services.ListRealmMember(realm.ID, take, offset); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
return c.JSON(members) return c.JSON(fiber.Map{
"count": count,
"data": members,
})
} }
} }

View File

@ -74,10 +74,22 @@ func NewRealm(realm models.Realm, user models.Account) (models.Realm, error) {
return realm, err return realm, err
} }
func ListRealmMember(realmId uint) ([]models.RealmMember, error) { func CountRealmMember(realmId uint) (int64, error) {
var count int64
if err := database.C.Where(&models.RealmMember{
RealmID: realmId,
}).Model(&models.RealmMember{}).Count(&count).Error; err != nil {
return 0, err
} else {
return count, nil
}
}
func ListRealmMember(realmId uint, take int, offset int) ([]models.RealmMember, error) {
var members []models.RealmMember var members []models.RealmMember
if err := database.C. if err := database.C.
Limit(take).Offset(offset).
Where(&models.RealmMember{RealmID: realmId}). Where(&models.RealmMember{RealmID: realmId}).
Preload("Account"). Preload("Account").
Find(&members).Error; err != nil { Find(&members).Error; err != nil {