From f8f64265592a6cbdf1321703b459f745fed8dcd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=A0=EA=B0=80=ED=98=95?= <101045330+aazkgh@users.noreply.github.com> Date: Wed, 25 Sep 2024 00:45:21 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EB=A6=AC=EB=B7=B0=20=EA=B4=80=EB=A0=A8?= =?UTF-8?q?=20API=20=EC=83=9D=EC=84=B1=20(#51)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 리뷰 업로드 함수 구현 * feat: 관광지에 따른 리뷰 받아오기 --- src/apis/supabase/getReviews.ts | 26 +++++++++++++++++ src/apis/supabase/postImgReview.ts | 35 +++++++++++++++++++++++ src/apis/supabase/postReview.ts | 46 ++++++++++++++++++++++++++++++ src/types/supabaseAPI.ts | 9 ++++++ 4 files changed, 116 insertions(+) create mode 100644 src/apis/supabase/getReviews.ts create mode 100644 src/apis/supabase/postImgReview.ts create mode 100644 src/apis/supabase/postReview.ts create mode 100644 src/types/supabaseAPI.ts diff --git a/src/apis/supabase/getReviews.ts b/src/apis/supabase/getReviews.ts new file mode 100644 index 0000000..7b4dc9c --- /dev/null +++ b/src/apis/supabase/getReviews.ts @@ -0,0 +1,26 @@ +import { useLocation } from 'react-router-dom'; + +import { unitripSupabase } from '@/utils/supabaseClient'; + +const getReviews = async () => { + const location = useLocation(); + const queryParams = new URLSearchParams(location.search); + const place = queryParams.get('contentId'); + + const { data, error } = await unitripSupabase + .from('REVIEW') + .select('*, USER(name)') + .eq('place', place); + + if (error) { + throw new Error('서버에 문제가 있습니다'); + } + + /* + ** data는 리뷰들을 string[]에 저장 + ** 각 리뷰들의 타입은 ReviewResProps 타입을 갖고 있음 + */ + return data; +}; + +export default getReviews; diff --git a/src/apis/supabase/postImgReview.ts b/src/apis/supabase/postImgReview.ts new file mode 100644 index 0000000..578117f --- /dev/null +++ b/src/apis/supabase/postImgReview.ts @@ -0,0 +1,35 @@ +import { unitripSupabase } from '@/utils/supabaseClient'; + +const postImgReview = async (imgs: File[]) => { + const imgUrls: string[] = []; + + // 각 파일을 Supabase Storage에 업로드 + for (const img of imgs) { + const { error: uploadError } = await unitripSupabase.storage + .from('REVIEW_IMAGES') + .upload(`images/${img.name}`, img); + + if (uploadError) { + throw new Error('이미지 업로드 과정에서 에러가 발생했습니다'); + } + + // 업로드한 파일의 URL 생성 + const { data } = unitripSupabase.storage + .from('REVIEW_IMAGES') + .getPublicUrl(`images/${img.name}`); + + if (data) { + const { publicUrl } = data; + imgUrls.push(publicUrl); + } + } + + // 파일 URL을 데이터베이스에 저장 + const { error: dbError } = await unitripSupabase.from('USER').insert(imgUrls); + + if (dbError) { + throw new Error('서버에 문제가 있습니다'); + } +}; + +export default postImgReview; diff --git a/src/apis/supabase/postReview.ts b/src/apis/supabase/postReview.ts new file mode 100644 index 0000000..1e071ca --- /dev/null +++ b/src/apis/supabase/postReview.ts @@ -0,0 +1,46 @@ +import { useLocation } from 'react-router-dom'; + +import { unitripSupabase } from '@/utils/supabaseClient'; + +import postImgReview from './postImgReview'; + +interface postReviewProps { + rate: number; + description: string; + convenience: string[]; + imgs: File[]; +} + +const postReview = async ({ + rate, + description, + convenience, + imgs, +}: postReviewProps) => { + const location = useLocation(); + const queryParams = new URLSearchParams(location.search); + const place = queryParams.get('contentId'); + + const writer = sessionStorage.getItem('kakao_id'); + + const { error } = await unitripSupabase + .from('REVIEW') + .insert([ + { + place, + writer, + rate, + description, + convenience, + }, + ]) + .select(); + + await postImgReview(imgs); + + if (error) { + throw new Error('서버에 문제가 있습니다'); + } +}; + +export default postReview; diff --git a/src/types/supabaseAPI.ts b/src/types/supabaseAPI.ts new file mode 100644 index 0000000..2e7e201 --- /dev/null +++ b/src/types/supabaseAPI.ts @@ -0,0 +1,9 @@ +export interface ReviewResProps { + rate: number; + description: string; + convenience: string[]; + imgUrls: string[]; + USER: { + name: string; + }; +}