Skip to content

Commit

Permalink
Add Goods Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
공태현 committed Feb 23, 2024
1 parent a06e13a commit d07b2a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/lib/component/GoodsComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
alert("등록되었습니다.");
window.location.reload();
} else {
alert("오류가 발생하였습니다.");
window.location.reload();
alert(result.data);
}
};
};
Expand Down
6 changes: 6 additions & 0 deletions src/lib/repository/goodsRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ class GoodsRepository {
}

async upsertGoods({ goods }) {
// Goods 동일 이름이 존재할 경우 =>

if (goods.id !== "undefined") {
const { error } = await supabase
.from("goods")
.update({ name: goods.name, price: goods.price, description: goods.description, imageUrl: goods.imageUrl })
.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);
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/routes/goods/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,7 +43,7 @@ export const actions = {
return "success";
} catch (err) {
console.error(err);
return "fail";
return err.message;
}
},
};

0 comments on commit d07b2a1

Please sign in to comment.