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; + }; +}