-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 리뷰 업로드 함수 구현 * feat: 관광지에 따른 리뷰 받아오기
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface ReviewResProps { | ||
rate: number; | ||
description: string; | ||
convenience: string[]; | ||
imgUrls: string[]; | ||
USER: { | ||
name: string; | ||
}; | ||
} |