✨ Better relationships stauts query
This commit is contained in:
@ -5,7 +5,9 @@ import (
|
||||
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func listRelationship(c *fiber.Ctx) error {
|
||||
@ -13,16 +15,24 @@ func listRelationship(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
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 friends []models.AccountRelationship
|
||||
if status < 0 {
|
||||
if len(status) < 0 {
|
||||
if friends, err = services.ListAllRelationship(user); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
} 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())
|
||||
}
|
||||
}
|
||||
|
@ -22,10 +22,10 @@ func ListAllRelationship(user models.Account) ([]models.AccountRelationship, err
|
||||
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
|
||||
if err := database.C.
|
||||
Where("account_id = ? AND status = ?", user.ID, status).
|
||||
Where("account_id = ? AND status IN ?", user.ID, status).
|
||||
Preload("Account").
|
||||
Preload("Related").
|
||||
Find(&relationships).Error; err != nil {
|
||||
|
Reference in New Issue
Block a user