💥 Use realm alias instead of external id
This commit is contained in:
parent
bed420c16d
commit
ae269dfb3b
@ -14,7 +14,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
|||||||
recommendations := api.Group("/recommendations").Name("Recommendations API")
|
recommendations := api.Group("/recommendations").Name("Recommendations API")
|
||||||
{
|
{
|
||||||
recommendations.Get("/", listRecommendationNews)
|
recommendations.Get("/", listRecommendationNews)
|
||||||
recommendations.Get("/featured", listRecommendationFeatured)
|
recommendations.Get("/friends", listRecommendationFriends)
|
||||||
recommendations.Get("/shuffle", listRecommendationShuffle)
|
recommendations.Get("/shuffle", listRecommendationShuffle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ func getPost(c *fiber.Ctx) error {
|
|||||||
func listPost(c *fiber.Ctx) error {
|
func listPost(c *fiber.Ctx) error {
|
||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
realmId := c.QueryInt("realmId", 0)
|
realm := c.Query("realm")
|
||||||
|
|
||||||
tx := services.FilterPostDraft(database.C)
|
tx := services.FilterPostDraft(database.C)
|
||||||
|
|
||||||
@ -62,8 +62,8 @@ func listPost(c *fiber.Ctx) error {
|
|||||||
tx = services.FilterPostWithUserContext(tx, nil)
|
tx = services.FilterPostWithUserContext(tx, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if realmId > 0 {
|
if len(realm) > 0 {
|
||||||
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
|
if realm, err := services.GetRealmWithAlias(realm); err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
||||||
} else {
|
} else {
|
||||||
tx = services.FilterPostWithRealm(tx, realm.ID)
|
tx = services.FilterPostWithRealm(tx, realm.ID)
|
||||||
@ -105,7 +105,7 @@ func listPost(c *fiber.Ctx) error {
|
|||||||
func listPostMinimal(c *fiber.Ctx) error {
|
func listPostMinimal(c *fiber.Ctx) error {
|
||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
realmId := c.QueryInt("realmId", 0)
|
realm := c.Query("realm")
|
||||||
|
|
||||||
tx := services.FilterPostDraft(database.C)
|
tx := services.FilterPostDraft(database.C)
|
||||||
|
|
||||||
@ -115,8 +115,8 @@ func listPostMinimal(c *fiber.Ctx) error {
|
|||||||
tx = services.FilterPostWithUserContext(tx, nil)
|
tx = services.FilterPostWithUserContext(tx, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if realmId > 0 {
|
if len(realm) > 0 {
|
||||||
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
|
if realm, err := services.GetRealmWithAlias(realm); err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
||||||
} else {
|
} else {
|
||||||
tx = services.FilterPostWithRealm(tx, realm.ID)
|
tx = services.FilterPostWithRealm(tx, realm.ID)
|
||||||
|
@ -2,18 +2,18 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||||
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
||||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"gorm.io/gorm"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func listRecommendationFeatured(c *fiber.Ctx) error {
|
func listRecommendationNews(c *fiber.Ctx) error {
|
||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
realmId := c.QueryInt("realmId", 0)
|
realm := c.Query("realm")
|
||||||
|
|
||||||
tx := services.FilterPostDraft(database.C)
|
tx := services.FilterPostDraft(database.C)
|
||||||
|
|
||||||
@ -23,8 +23,8 @@ func listRecommendationFeatured(c *fiber.Ctx) error {
|
|||||||
tx = services.FilterPostWithUserContext(tx, nil)
|
tx = services.FilterPostWithUserContext(tx, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if realmId > 0 {
|
if len(realm) > 0 {
|
||||||
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
|
if realm, err := services.GetRealmWithAlias(realm); err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
||||||
} else {
|
} else {
|
||||||
tx = services.FilterPostWithRealm(tx, realm.ID)
|
tx = services.FilterPostWithRealm(tx, realm.ID)
|
||||||
@ -37,7 +37,12 @@ func listRecommendationFeatured(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
items, err := services.ListPost(tx, take, offset, "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC")
|
order := "published_at DESC"
|
||||||
|
if c.QueryBool("featured", false) {
|
||||||
|
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := services.ListPost(tx, take, offset, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
@ -48,47 +53,46 @@ func listRecommendationFeatured(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func listRecommendationNews(c *fiber.Ctx) error {
|
func listRecommendationFriends(c *fiber.Ctx) error {
|
||||||
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
realmId := c.QueryInt("realmId", 0)
|
realm := c.Query("realm")
|
||||||
|
|
||||||
tx := services.FilterPostDraft(database.C)
|
tx := services.FilterPostDraft(database.C)
|
||||||
|
|
||||||
if user, authenticated := c.Locals("user").(models.Account); authenticated {
|
|
||||||
tx = services.FilterPostWithUserContext(tx, &user)
|
tx = services.FilterPostWithUserContext(tx, &user)
|
||||||
} else {
|
|
||||||
tx = services.FilterPostWithUserContext(tx, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
if realmId > 0 {
|
friends, _ := services.ListAccountFriends(user)
|
||||||
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
|
friendList := lo.Map(friends, func(item models.Account, index int) uint {
|
||||||
|
return item.ID
|
||||||
|
})
|
||||||
|
|
||||||
|
tx = tx.Where("author_id IN ?", friendList)
|
||||||
|
|
||||||
|
if len(realm) > 0 {
|
||||||
|
if realm, err := services.GetRealmWithAlias(realm); err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
||||||
} else {
|
} else {
|
||||||
tx = services.FilterPostWithRealm(tx, realm.ID)
|
tx = services.FilterPostWithRealm(tx, realm.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(database.C.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
|
||||||
tx = services.FilterPostDraft(tx)
|
|
||||||
|
|
||||||
if user, authenticated := c.Locals("user").(models.Account); authenticated {
|
|
||||||
tx = services.FilterPostWithUserContext(tx, &user)
|
|
||||||
} else {
|
|
||||||
tx = services.FilterPostWithUserContext(tx, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
services.CountPost(tx)
|
|
||||||
return tx
|
|
||||||
}))
|
|
||||||
|
|
||||||
countTx := tx
|
countTx := tx
|
||||||
count, err := services.CountPost(countTx)
|
count, err := services.CountPost(countTx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
items, err := services.ListPost(tx, take, offset, "published_at DESC")
|
order := "published_at DESC"
|
||||||
|
if c.QueryBool("featured", false) {
|
||||||
|
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := services.ListPost(tx, take, offset, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
@ -102,7 +106,7 @@ func listRecommendationNews(c *fiber.Ctx) error {
|
|||||||
func listRecommendationShuffle(c *fiber.Ctx) error {
|
func listRecommendationShuffle(c *fiber.Ctx) error {
|
||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
realmId := c.QueryInt("realmId", 0)
|
realm := c.Query("realm")
|
||||||
|
|
||||||
tx := services.FilterPostDraft(database.C)
|
tx := services.FilterPostDraft(database.C)
|
||||||
|
|
||||||
@ -112,8 +116,8 @@ func listRecommendationShuffle(c *fiber.Ctx) error {
|
|||||||
tx = services.FilterPostWithUserContext(tx, nil)
|
tx = services.FilterPostWithUserContext(tx, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if realmId > 0 {
|
if len(realm) > 0 {
|
||||||
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
|
if realm, err := services.GetRealmWithAlias(realm); err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
|
||||||
} else {
|
} else {
|
||||||
tx = services.FilterPostWithRealm(tx, realm.ID)
|
tx = services.FilterPostWithRealm(tx, realm.ID)
|
||||||
|
Loading…
Reference in New Issue
Block a user