From 70b401517351370c4e73ac9f6734c363d355a09b Mon Sep 17 00:00:00 2001 From: rxdn <29165304+rxdn@users.noreply.github.com> Date: Sun, 16 Apr 2023 00:33:20 +0100 Subject: [PATCH] Overrides --- app/http/endpoints/api/panel/panelcreate.go | 12 +++++------ frontend/src/views/Panels.svelte | 22 +++++++++++++-------- utils/utils.go | 6 ++++++ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/http/endpoints/api/panel/panelcreate.go b/app/http/endpoints/api/panel/panelcreate.go index 700396f..bf97317 100644 --- a/app/http/endpoints/api/panel/panelcreate.go +++ b/app/http/endpoints/api/panel/panelcreate.go @@ -92,14 +92,12 @@ func CreatePanel(ctx *gin.Context) { if premiumTier == premium.None { panels, err := dbclient.Client.Panel.GetByGuild(guildId) if err != nil { - ctx.AbortWithStatusJSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) + return } if len(panels) >= freePanelLimit { - ctx.AbortWithStatusJSON(402, utils.ErrorStr("You have exceeded your panel quota. Purchase premium to unlock more panels.")) + ctx.JSON(402, utils.ErrorStr("You have exceeded your panel quota. Purchase premium to unlock more panels.")) return } } @@ -251,7 +249,7 @@ func (p *panelBody) doValidations(ctx *gin.Context, guildId uint64) bool { } if !p.verifyTitle() { - ctx.AbortWithStatusJSON(400, gin.H{ + ctx.JSON(400, gin.H{ "success": false, "error": "Panel titles must be between 1 - 80 characters in length", }) @@ -259,7 +257,7 @@ func (p *panelBody) doValidations(ctx *gin.Context, guildId uint64) bool { } if !p.verifyContent() { - ctx.AbortWithStatusJSON(400, gin.H{ + ctx.JSON(400, gin.H{ "success": false, "error": "Panel content must be between 1 - 4096 characters in length", }) diff --git a/frontend/src/views/Panels.svelte b/frontend/src/views/Panels.svelte index 88355d9..4776b34 100644 --- a/frontend/src/views/Panels.svelte +++ b/frontend/src/views/Panels.svelte @@ -236,15 +236,9 @@ } async function createPanel() { - let mapped = Object.fromEntries(Object.entries(panelCreateData).map(([k, v]) => { - if (v === "null") { - return [k, null]; - } else { - return [k, v]; - } - })); + setBlankStringsToNull(panelCreateData); - const res = await axios.post(`${API_URL}/api/${guildId}/panels`, mapped); + const res = await axios.post(`${API_URL}/api/${guildId}/panels`, panelCreateData); if (res.status !== 200) { notifyError(res.data.error); return; @@ -256,6 +250,7 @@ async function submitEdit(e) { let data = e.detail; + setBlankStringsToNull(data); const res = await axios.patch(`${API_URL}/api/${guildId}/panels/${data.panel_id}`, data); if (res.status !== 200) { @@ -270,6 +265,17 @@ await loadPanels(); } + function setBlankStringsToNull(obj) { + // Set all blank strings in the object, including nested objects, to null + for (const key in obj) { + if (obj[key] === "" || obj[key] === "null") { + obj[key] = null; + } else if (typeof obj[key] === "object") { + setBlankStringsToNull(obj[key]); + } + } + } + async function submitMultiPanelEdit(e) { let data = e.detail; diff --git a/utils/utils.go b/utils/utils.go index 072e6d6..10ac66f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -12,6 +12,12 @@ func ValueOrZero[T any](v *T) T { } } +func SetNilIfZero[T comparable](value **T) { + if value != nil && *value != nil && **value == *new(T) { + *value = nil + } +} + func Slice[T any](v ...T) []T { return v }