Feat: Display deck list in decks

This commit is contained in:
zuma 2025-06-09 22:51:16 +02:00
parent 9014c17a07
commit ae76b3db1e
5 changed files with 203 additions and 59 deletions

View file

@ -6,11 +6,12 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strings"
"os"
"slices"
"strings"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
)
const frontEndDevPort = "5173"
@ -30,6 +31,17 @@ type DeckImport struct {
Cards []DeckImportCard `json:"cards"`
}
type DeckAnswerCard struct {
Carte Carte `json:"carte"`
Amount int `json:"amount"`
}
type DeckAnswer struct {
Name string `json:"name"`
Commander Carte `json:"commander"`
Cartes []DeckAnswerCard `json:"cartes"`
}
func (app *application) SetupRoutes() {
app.pb.OnServe().BindFunc(func(se *core.ServeEvent) error {
mode := os.Getenv("GO_ENV")
@ -57,7 +69,29 @@ func (app *application) SetupRoutes() {
http.Error(w, fmt.Sprintf("proxy error: %v", err), http.StatusBadGateway)
}
// Cache Route
// Cache Route - GET "/json/{...}"
GET_Cache(se, app)
// Search API - GET "/api/search"
GET_Search(se, app)
// Create deck API - POST "api/deck/create"
POST_CreateDeck(se, app)
// Get deck list API - GET "/api/deck"
GET_AllDeck(se, app)
// Proxy to svelte app
se.Router.Any("/{path...}", func(re *core.RequestEvent) error {
proxy.ServeHTTP(re.Response, re.Request)
return nil
})
return se.Next()
})
}
func GET_Cache(se *core.ServeEvent, app *application) {
se.Router.GET("/json/{path...}", func(re *core.RequestEvent) error {
path := fmt.Sprintf("json/%s",re.Request.PathValue("path"))
@ -68,8 +102,9 @@ func (app *application) SetupRoutes() {
return re.JSON(http.StatusNotFound, map[string]string{"message": "json not found in cache"})
}
})
}
// Search API
func GET_Search(se *core.ServeEvent, app *application) {
se.Router.GET("/api/search", func(re *core.RequestEvent) error {
searchData := app.pb.Store().Get("searchData").([]CacheCarteSearch)
search := re.Request.URL.Query().Get("q")
@ -77,8 +112,65 @@ func (app *application) SetupRoutes() {
resultData := filter(searchData, func(card CacheCarteSearch) bool { return strings.Contains(strings.ToLower(card.Name), strings.ToLower(search)) })
return re.JSON(http.StatusOK, resultData)
})
}
// Create deck API
func GET_AllDeck(se *core.ServeEvent, app *application) {
se.Router.GET("/api/deck", func(re *core.RequestEvent) error {
authRecord := re.Auth
deckList := []Deck{}
err := app.pb.DB().
NewQuery(fmt.Sprintf("SELECT name, url, commander, cartes FROM deck WHERE owner = '%v'", authRecord.Id)).
All(&deckList)
if err != nil {
return re.BadRequestError("Problem with " + err.Error(), err)
}
answer := []DeckAnswer{}
cardsIds := []string{}
for _, v := range deckList {
if !slices.Contains(cardsIds, v.Commander) {
cardsIds = append(cardsIds, fmt.Sprintf("id = '%s'",v.Commander))
}
for _, c := range v.Cards {
if !slices.Contains(cardsIds, c.ID) {
cardsIds = append(cardsIds, fmt.Sprintf("id = '%s'",c.ID))
}
}
}
cardsData := []Carte{}
err = app.pb.DB().
NewQuery(fmt.Sprintf("SELECT * FROM carte WHERE %s", strings.Join(cardsIds, " OR "))).
All(&cardsData)
if err != nil {
return re.BadRequestError("Problem with " + err.Error(), err)
}
for _, v := range deckList {
tempDeck := DeckAnswer{
Name: v.Name,
}
tempDeck.Commander = filter(cardsData, func(carte Carte) bool { return carte.ID == v.Commander })[0]
for _, c := range v.Cards {
cardData := filter(cardsData, func(carte Carte) bool { return carte.ID == c.ID})[0]
tempDeck.Cartes = append(tempDeck.Cartes, DeckAnswerCard{
Amount: c.Amount,
Carte: cardData,
})
}
answer = append(answer, tempDeck)
}
return re.JSON(http.StatusOK, answer)
}).Bind(apis.RequireAuth())
}
func POST_CreateDeck(se *core.ServeEvent, app *application) {
se.Router.POST("/api/deck/create", func(re *core.RequestEvent) error {
authRecord := re.Auth
@ -134,6 +226,7 @@ func (app *application) SetupRoutes() {
}
cardInDeck := []DeckCard{}
cardsNotFound := []string{}
for _, v := range data.Cards {
cardId := ""
for _, c := range possibleCards {
@ -143,8 +236,14 @@ func (app *application) SetupRoutes() {
}
if cardId == "" {
log.Println(fmt.Sprintf("Card not found : %s",v.Name))
cardsNotFound = append(cardsNotFound, v.Name)
} else {
cardInDeck = append(cardInDeck, DeckCard{ID: cardId, Amount: v.Amount})
}
cardInDeck = append(cardInDeck, DeckCard{ID: cardId, Amount: v.Amount})
}
if len(cardsNotFound) > 0 {
return re.BadRequestError("Cards not found : " + strings.Join(cardsNotFound, ", "), err)
}
record.Set("cartes", cardInDeck)
@ -153,20 +252,13 @@ func (app *application) SetupRoutes() {
log.Println(err)
re.BadRequestError("Problem with creating deck", err)
}
log.Println("Deck created")
return re.JSON(http.StatusOK, map[string]string{"message": "deck created"})
}).Bind(apis.RequireAuth())
// Proxy to svelte app
se.Router.Any("/{path...}", func(re *core.RequestEvent) error {
proxy.ServeHTTP(re.Response, re.Request)
return nil
})
return se.Next()
})
}
func filter(ss []CacheCarteSearch, test func(CacheCarteSearch) bool) (ret []CacheCarteSearch) {
func filter[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)