From d07b2a1f874aa4f6944fe1b2d4f2379548ad6ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=A9=E1=86=BC=E1=84=90=E1=85=A2=E1=84=92?= =?UTF-8?q?=E1=85=A7=E1=86=AB?= Date: Fri, 23 Feb 2024 14:08:39 +0900 Subject: [PATCH] Add Goods Validation --- src/lib/component/GoodsComponent.svelte | 3 +-- src/lib/repository/goodsRepository.js | 6 ++++++ src/routes/goods/+page.server.js | 12 +++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/lib/component/GoodsComponent.svelte b/src/lib/component/GoodsComponent.svelte index f427d14..dc18b4f 100644 --- a/src/lib/component/GoodsComponent.svelte +++ b/src/lib/component/GoodsComponent.svelte @@ -31,8 +31,7 @@ alert("등록되었습니다."); window.location.reload(); } else { - alert("오류가 발생하였습니다."); - window.location.reload(); + alert(result.data); } }; }; diff --git a/src/lib/repository/goodsRepository.js b/src/lib/repository/goodsRepository.js index 439db0f..66dae21 100644 --- a/src/lib/repository/goodsRepository.js +++ b/src/lib/repository/goodsRepository.js @@ -36,6 +36,8 @@ class GoodsRepository { } async upsertGoods({ goods }) { + // Goods 동일 이름이 존재할 경우 => + if (goods.id !== "undefined") { const { error } = await supabase .from("goods") @@ -43,9 +45,13 @@ class GoodsRepository { .eq("id", goods.id); if (error) throw new Error(error); } else { + const { data } = await supabase.from("goods").select("*").eq("name", goods.name); + if (data) throw new Error("중복된 굿즈 이름"); + const { error } = await supabase .from("goods") .insert({ name: goods.name, price: goods.price, description: goods.description, imageUrl: goods.imageUrl }); + if (error) throw new Error(error); } } diff --git a/src/routes/goods/+page.server.js b/src/routes/goods/+page.server.js index 823e445..443c5a6 100644 --- a/src/routes/goods/+page.server.js +++ b/src/routes/goods/+page.server.js @@ -29,12 +29,10 @@ export const actions = { try { if (imageFile) { // Upload 할 파일이 있는 경우. - const { data, error } = await supabase.storage - .from("program-images") - .upload(`/img/goods/${goods.name}`, imageFile, { - cacheControl: "3600", - upsert: true, - }); + const { data, error } = await supabase.storage.from("program-images").upload(`/img/goods`, imageFile, { + cacheControl: "3600", + upsert: true, + }); if (error) return "fail"; let { data: imageUrl } = await supabase.storage.from("program-images").getPublicUrl(data.path); newGoods.imageUrl = imageUrl.publicUrl; @@ -45,7 +43,7 @@ export const actions = { return "success"; } catch (err) { console.error(err); - return "fail"; + return err.message; } }, };