-
Notifications
You must be signed in to change notification settings - Fork 30
[하성민] sprint3 #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: basic-하성민
Are you sure you want to change the base?
The head ref may contain hidden characters: "basic-\uD558\uC131\uBBFC-sprint3"
[하성민] sprint3 #56
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| const ARTICLE_BASE_URL = "https://panda-market-api-crud.vercel.app/articles"; | ||
|
|
||
| export async function getArticleList(page, pageSize, keyword) { | ||
| try { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
export function getArticleList(page = 1, pageSize = 10, keyword = "") {
|
||
| const response = await fetch( | ||
| `${ARTICLE_BASE_URL}?page=${page}&pageSize=${pageSize}&keyword=${keyword}`, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`게시글 목록 조회 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| return data; | ||
| } catch (error) { | ||
| console.log("getArticleList 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function getArticle(articleId) { | ||
| try { | ||
| const response = await fetch(`${ARTICLE_BASE_URL}/${articleId}`); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`게시글 상세 조회 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("getArticle 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function createArticle(title, content, image) { | ||
| const newArticle = { | ||
| title, | ||
| content, | ||
| image, | ||
| }; | ||
|
|
||
| try { | ||
| const response = await fetch(ARTICLE_BASE_URL, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(newArticle), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`게시글 생성 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("createArticle 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function patchArticle(articleId, title, content, image) { | ||
| const updateArticle = { | ||
| title, | ||
| content, | ||
| image, | ||
| }; | ||
|
|
||
| try { | ||
| const response = await fetch(`${ARTICLE_BASE_URL}/${articleId}`, { | ||
| method: "PATCH", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(updateArticle), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`게시글 수정 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("patchArticle 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function deleteArticle(articleId) { | ||
| try { | ||
| const response = await fetch(`${ARTICLE_BASE_URL}/${articleId}`, { | ||
| method: "DELETE", | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`게시글 삭제 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("deleteArticle 오류:", error); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| const PRODUCT_BASE_URL = "https://panda-market-api-crud.vercel.app/products"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Product API는 이번 미션에서 |
||
|
|
||
| export async function getProductList(page, pageSize, keyword) { | ||
| try { | ||
| const response = await fetch( | ||
| `${PRODUCT_BASE_URL}?page=${page}&pageSize=${pageSize}&keyword=${keyword}`, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`상품 목록 조회 실패: ${response.status}`); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if (!response.ok) {
throw new Error(`상품 목록 조회 실패: ${response.status}`);
}
|
||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("getProductList 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function getProduct(productId) { | ||
| try { | ||
| const response = await fetch(`${PRODUCT_BASE_URL}/${productId}`); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`상품 상세 조회 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("getProduct 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function createProduct(name, description, price, tags, images) { | ||
| const newProduct = { | ||
| name, | ||
| description, | ||
| price, | ||
| tags, | ||
| images, | ||
| }; | ||
|
|
||
| try { | ||
| const response = await fetch(PRODUCT_BASE_URL, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(newProduct), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`상품 생성 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("createProduct 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function patchProduct( | ||
| productId, | ||
| name, | ||
| description, | ||
| price, | ||
| tags, | ||
| images, | ||
| ) { | ||
| const updateProduct = { | ||
| name, | ||
| description, | ||
| price, | ||
| tags, | ||
| images, | ||
| }; | ||
|
|
||
| try { | ||
| const response = await fetch(`${PRODUCT_BASE_URL}/${productId}`, { | ||
| method: "PATCH", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(updateProduct), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`상품 수정 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("patchProduct 오류:", error); | ||
| } | ||
| } | ||
|
|
||
| export async function deleteProduct(productId) { | ||
| try { | ||
| const response = await fetch(`${PRODUCT_BASE_URL}/${productId}`, { | ||
| method: "DELETE", | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| console.log(`상품 삭제 실패: ${response.status}`); | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log("deleteProduct 오류:", error); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { | ||
| getArticleList, | ||
| getArticle, | ||
| createArticle, | ||
| patchArticle, | ||
| deleteArticle, | ||
| } from "./ArticleService.js"; | ||
|
|
||
| import { | ||
| getProductList, | ||
| getProduct, | ||
| createProduct, | ||
| patchProduct, | ||
| deleteProduct, | ||
| } from "./ProductService.js"; | ||
|
|
||
| async function runTest() { | ||
| const articleList = await getArticleList(1, 10, ""); | ||
| console.log("게시글 목록:", articleList); | ||
|
|
||
| const createdArticle = await createArticle( | ||
| "테스트 게시글 제목", | ||
| "테스트 게시글 내용입니다.", | ||
| "https://example.com/article-image.jpg", | ||
| ); | ||
| console.log("생성된 게시글:", createdArticle); | ||
|
|
||
| if (createdArticle) { | ||
| const articleDetail = await getArticle(createdArticle.id); | ||
| console.log("게시글 상세:", articleDetail); | ||
|
|
||
| const updatedArticle = await patchArticle( | ||
| createdArticle.id, | ||
| "수정된 게시글 제목", | ||
| "수정된 게시글 내용입니다.", | ||
| "https://example.com/article-image-2.jpg", | ||
| ); | ||
| console.log("수정된 게시글:", updatedArticle); | ||
|
|
||
| const deletedArticle = await deleteArticle(createdArticle.id); | ||
| console.log("삭제된 게시글:", deletedArticle); | ||
| } | ||
|
|
||
| const productList = await getProductList(1, 10, ""); | ||
| console.log("상품 목록:", productList); | ||
|
|
||
| const createdProduct = await createProduct( | ||
| "핸드폰", | ||
| "최신 핸드폰", | ||
| 129000, | ||
| ["전자제품", "핸드폰"], | ||
| ["https://example.com/product-image.jpg"], | ||
| ); | ||
| console.log("생성된 상품:", createdProduct); | ||
|
|
||
| if (createdProduct) { | ||
| const productDetail = await getProduct(createdProduct.id); | ||
| console.log("상품 상세:", productDetail); | ||
|
|
||
| const updatedProduct = await patchProduct( | ||
| createdProduct.id, | ||
| "수정된 핸드폰", | ||
| "최신최신 핸드폰", | ||
| 139000, | ||
| ["전자제품", "핸드폰", "블루투스"], | ||
| ["https://example.com/product-image-2.jpg"], | ||
| ); | ||
| console.log("수정된 상품:", updatedProduct); | ||
|
|
||
| const deletedProduct = await deleteProduct(createdProduct.id); | ||
| console.log("삭제된 상품:", deletedProduct); | ||
| } | ||
| } | ||
|
|
||
| runTest(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이번 미션에서 Article API는
fetch+.then()/.catch()패턴으로 구현하도록 요구하고 있어요.현재는
async/await+try/catch를 사용하고 계신데, Product와 동일한 패턴이 되어버렸어요.아래처럼
.then()체이닝 방식으로 바꿔보시면 두 패턴을 모두 경험할 수 있어요!