Better relationships stauts query

This commit is contained in:
LittleSheep 2024-11-30 17:31:35 +08:00
parent 312cf820ab
commit 08c639497d
3 changed files with 20 additions and 10 deletions

10
.idea/workspace.xml generated
View File

@ -4,10 +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=":boom: Passing relationship api arguments in body instead of querystring"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Better check in experience random algorithm">
<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/check_in_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/check_in_api.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/http/api/relationships_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/relationships_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/check_in.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/check_in.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/relationships.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/relationships.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -160,7 +160,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=":technologist: Improve DX by extending authkit" />
<MESSAGE value=":sparkles: Add list relative method into authkit" /> <MESSAGE value=":sparkles: Add list relative method into authkit" />
<MESSAGE value=":sparkles: Realm operations now available in authkit" /> <MESSAGE value=":sparkles: Realm operations now available in authkit" />
<MESSAGE value=":bug: Fix grpc namespace conflict" /> <MESSAGE value=":bug: Fix grpc namespace conflict" />
@ -185,7 +184,8 @@
<MESSAGE value=":truck: Rename daily-sign to check-in" /> <MESSAGE value=":truck: Rename daily-sign to check-in" />
<MESSAGE value=":sparkles: Result modifiers in check-in" /> <MESSAGE value=":sparkles: Result modifiers in check-in" />
<MESSAGE value=":boom: Passing relationship api arguments in body instead of querystring" /> <MESSAGE value=":boom: Passing relationship api arguments in body instead of querystring" />
<option name="LAST_COMMIT_MESSAGE" value=":boom: Passing relationship api arguments in body instead of querystring" /> <MESSAGE value=":sparkles: Better check in experience random algorithm" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Better check in experience random algorithm" />
<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

@ -5,7 +5,9 @@ import (
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts" "git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
"git.solsynth.dev/hypernet/passport/pkg/internal/services" "git.solsynth.dev/hypernet/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"strconv" "strconv"
"strings"
) )
func listRelationship(c *fiber.Ctx) error { func listRelationship(c *fiber.Ctx) error {
@ -13,16 +15,24 @@ func listRelationship(c *fiber.Ctx) error {
return err return err
} }
user := c.Locals("user").(models.Account) user := c.Locals("user").(models.Account)
status := c.QueryInt("status", -1) statusQuery := c.Query("status")
status := lo.Map(strings.Split(statusQuery, ","), func(s string, _ int) models.RelationshipStatus {
num, err := strconv.Atoi(s)
if err != nil {
return 0
}
return models.RelationshipStatus(num)
})
var err error var err error
var friends []models.AccountRelationship var friends []models.AccountRelationship
if status < 0 { if len(status) < 0 {
if friends, err = services.ListAllRelationship(user); err != nil { if friends, err = services.ListAllRelationship(user); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }
} else { } else {
if friends, err = services.ListRelationshipWithFilter(user, models.RelationshipStatus(status)); err != nil { if friends, err = services.ListRelationshipWithFilter(user, status...); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }
} }

View File

@ -22,10 +22,10 @@ func ListAllRelationship(user models.Account) ([]models.AccountRelationship, err
return relationships, nil return relationships, nil
} }
func ListRelationshipWithFilter(user models.Account, status models.RelationshipStatus) ([]models.AccountRelationship, error) { func ListRelationshipWithFilter(user models.Account, status ...models.RelationshipStatus) ([]models.AccountRelationship, error) {
var relationships []models.AccountRelationship var relationships []models.AccountRelationship
if err := database.C. if err := database.C.
Where("account_id = ? AND status = ?", user.ID, status). Where("account_id = ? AND status IN ?", user.ID, status).
Preload("Account"). Preload("Account").
Preload("Related"). Preload("Related").
Find(&relationships).Error; err != nil { Find(&relationships).Error; err != nil {