💥 Use realm alias instead of external id

This commit is contained in:
LittleSheep 2024-08-19 00:04:49 +08:00
parent bed420c16d
commit ae269dfb3b
3 changed files with 44 additions and 40 deletions

View File

@ -14,7 +14,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
recommendations := api.Group("/recommendations").Name("Recommendations API")
{
recommendations.Get("/", listRecommendationNews)
recommendations.Get("/featured", listRecommendationFeatured)
recommendations.Get("/friends", listRecommendationFriends)
recommendations.Get("/shuffle", listRecommendationShuffle)
}

View File

@ -52,7 +52,7 @@ func getPost(c *fiber.Ctx) error {
func listPost(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
realm := c.Query("realm")
tx := services.FilterPostDraft(database.C)
@ -62,8 +62,8 @@ func listPost(c *fiber.Ctx) error {
tx = services.FilterPostWithUserContext(tx, nil)
}
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
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))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)
@ -105,7 +105,7 @@ func listPost(c *fiber.Ctx) error {
func listPostMinimal(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
realm := c.Query("realm")
tx := services.FilterPostDraft(database.C)
@ -115,8 +115,8 @@ func listPostMinimal(c *fiber.Ctx) error {
tx = services.FilterPostWithUserContext(tx, nil)
}
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
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))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)

View File

@ -2,18 +2,18 @@ package api
import (
"fmt"
"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/services"
"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)
offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
realm := c.Query("realm")
tx := services.FilterPostDraft(database.C)
@ -23,8 +23,8 @@ func listRecommendationFeatured(c *fiber.Ctx) error {
tx = services.FilterPostWithUserContext(tx, nil)
}
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
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))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)
@ -37,7 +37,12 @@ func listRecommendationFeatured(c *fiber.Ctx) 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 {
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)
offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
realm := c.Query("realm")
tx := services.FilterPostDraft(database.C)
tx = services.FilterPostWithUserContext(tx, &user)
if user, authenticated := c.Locals("user").(models.Account); authenticated {
tx = services.FilterPostWithUserContext(tx, &user)
} else {
tx = services.FilterPostWithUserContext(tx, nil)
}
friends, _ := services.ListAccountFriends(user)
friendList := lo.Map(friends, func(item models.Account, index int) uint {
return item.ID
})
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
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))
} else {
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
count, err := services.CountPost(countTx)
if err != nil {
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 {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -102,7 +106,7 @@ func listRecommendationNews(c *fiber.Ctx) error {
func listRecommendationShuffle(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
realm := c.Query("realm")
tx := services.FilterPostDraft(database.C)
@ -112,8 +116,8 @@ func listRecommendationShuffle(c *fiber.Ctx) error {
tx = services.FilterPostWithUserContext(tx, nil)
}
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
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))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)