From df5da9e5dbcd290cfafc27d6c1483f0c2c367d78 Mon Sep 17 00:00:00 2001 From: Supreme2580 Date: Wed, 18 Dec 2024 01:04:58 +0100 Subject: [PATCH] backend --- backend/routes/stencils.go | 17 ++++++++++-- backend/routes/worlds.go | 53 ++++++++++++++++++++++++++++++++++---- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/backend/routes/stencils.go b/backend/routes/stencils.go index b8c60e92..2830d3ff 100644 --- a/backend/routes/stencils.go +++ b/backend/routes/stencils.go @@ -6,6 +6,7 @@ import ( "image/color" "image/png" "io" + "log" "net/http" "os" "os/exec" @@ -589,16 +590,28 @@ func addStencilDevnet(w http.ResponseWriter, r *http.Request) { return } + // Add the stencil shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.AddStencilDevnet contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "add_stencil", strconv.Itoa(worldId), hash, strconv.Itoa(width), strconv.Itoa(height), strconv.Itoa(position)) - _, err = cmd.Output() + output, err := cmd.CombinedOutput() if err != nil { + log.Printf("Add stencil command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String()) routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to add stencil to devnet") return } - routeutils.WriteResultJson(w, "Stencil added to devnet") + // Favorite the newly created stencil + shellCmd = core.ArtPeaceBackend.BackendConfig.Scripts.FavoriteStencilDevnet + cmd = exec.Command(shellCmd, contract, "favorite_stencil", strconv.Itoa(worldId), strconv.Itoa(position)) + output, err = cmd.CombinedOutput() + if err != nil { + log.Printf("Favorite stencil command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String()) + routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to favorite newly created stencil") + return + } + + routeutils.WriteResultJson(w, "Stencil added and favorited") } func removeStencilDevnet(w http.ResponseWriter, r *http.Request) { diff --git a/backend/routes/worlds.go b/backend/routes/worlds.go index cc62a700..94e042b5 100644 --- a/backend/routes/worlds.go +++ b/backend/routes/worlds.go @@ -2,6 +2,8 @@ package routes import ( "context" + "fmt" + "log" "net/http" "os" "os/exec" @@ -540,13 +542,24 @@ func createCanvasDevnet(w http.ResponseWriter, r *http.Request) { contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS") cmd := exec.Command(shellCmd, contract, "create_canvas", host, name, uniqueName, strconv.Itoa(width), strconv.Itoa(height), strconv.Itoa(timer), strconv.Itoa(len(palette)), paletteInput, strconv.Itoa(startTime), strconv.Itoa(endTime)) - _, err = cmd.Output() + output, err := cmd.CombinedOutput() + if err != nil { + log.Printf("Create canvas command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String()) + routeutils.WriteErrorJson(w, http.StatusInternalServerError, fmt.Sprintf("Failed to create canvas: %v", err)) + return + } + + // Favorite the newly created canvas + shellCmd = core.ArtPeaceBackend.BackendConfig.Scripts.FavoriteWorldDevnet + cmd = exec.Command(shellCmd, contract, "favorite_canvas", uniqueName) + output, err = cmd.CombinedOutput() if err != nil { - routeutils.WriteErrorJson(w, http.StatusInternalServerError, "Failed to create canvas") + log.Printf("Favorite canvas command failed: %v\nOutput: %s\nCommand: %v", err, string(output), cmd.String()) + routeutils.WriteErrorJson(w, http.StatusInternalServerError, fmt.Sprintf("Failed to favorite newly created canvas: %v", err)) return } - routeutils.WriteResultJson(w, "Canvas created") + routeutils.WriteResultJson(w, "Canvas created and favorited") } func favoriteWorldDevnet(w http.ResponseWriter, r *http.Request) { @@ -561,7 +574,22 @@ func favoriteWorldDevnet(w http.ResponseWriter, r *http.Request) { return } - worldId := (*jsonBody)["worldId"] + // Try to get worldId either directly or from worldName + var worldId string + if (*jsonBody)["worldId"] != "" { + worldId = (*jsonBody)["worldId"] + } else if (*jsonBody)["worldName"] != "" { + // Get worldId from worldName + id, err := core.PostgresQueryOne[int]("SELECT world_id FROM worlds WHERE unique_name = $1", (*jsonBody)["worldName"]) + if err != nil { + routeutils.WriteErrorJson(w, http.StatusBadRequest, "Invalid world name") + return + } + worldId = strconv.Itoa(*id) + } else { + routeutils.WriteErrorJson(w, http.StatusBadRequest, "Must provide either worldId or worldName") + return + } shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.FavoriteWorldDevnet contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS") @@ -588,7 +616,22 @@ func unfavoriteWorldDevnet(w http.ResponseWriter, r *http.Request) { return } - worldId := (*jsonBody)["worldId"] + // Try to get worldId either directly or from worldName + var worldId string + if (*jsonBody)["worldId"] != "" { + worldId = (*jsonBody)["worldId"] + } else if (*jsonBody)["worldName"] != "" { + // Get worldId from worldName + id, err := core.PostgresQueryOne[int]("SELECT world_id FROM worlds WHERE unique_name = $1", (*jsonBody)["worldName"]) + if err != nil { + routeutils.WriteErrorJson(w, http.StatusBadRequest, "Invalid world name") + return + } + worldId = strconv.Itoa(*id) + } else { + routeutils.WriteErrorJson(w, http.StatusBadRequest, "Must provide either worldId or worldName") + return + } shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.UnfavoriteWorldDevnet contract := os.Getenv("CANVAS_FACTORY_CONTRACT_ADDRESS")