Skip to content

Commit

Permalink
Feat: 리뷰 관련 API 생성 (#51)
Browse files Browse the repository at this point in the history
* feat: 리뷰 업로드 함수 구현

* feat: 관광지에 따른 리뷰 받아오기
  • Loading branch information
aazkgh authored Sep 24, 2024
1 parent 47db888 commit f8f6426
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/apis/supabase/getReviews.ts
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;
35 changes: 35 additions & 0 deletions src/apis/supabase/postImgReview.ts
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;
46 changes: 46 additions & 0 deletions src/apis/supabase/postReview.ts
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;
9 changes: 9 additions & 0 deletions src/types/supabaseAPI.ts
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;
};
}

0 comments on commit f8f6426

Please sign in to comment.