-
Notifications
You must be signed in to change notification settings - Fork 25
sprint mission3- 최우진 #44
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| const BASE_URL = 'https://panda-market-api-crud.vercel.app'; | ||
|
|
||
| function getArticleList( | ||
| page = 1, | ||
| pageSize = 10, | ||
| orderBy = 'recent', | ||
| keyword = '' | ||
| ) { | ||
| return fetch( | ||
| `${BASE_URL}/products?page=${page}&pageSize=${pageSize}&orderBy=${orderBy}&keyword=${keyword}` | ||
| ) | ||
| .then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text: ${res.statusText}` | ||
| ); | ||
| } | ||
| return res.json(); | ||
| }) | ||
| .then((data) => { | ||
| return data; | ||
| }) | ||
| .catch((error) => { | ||
| console.log(error); | ||
|
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. 파일로 로깅하거나, 네트워크로 로깅을하거나, 디비로 로깅을 하면 좋을거 같아요. 지금은 말고 나중에 한번 해보시면 좋을거 같아요. |
||
| throw error; | ||
| }); | ||
| } | ||
|
|
||
| function getArticle(id) { | ||
| return fetch(`${BASE_URL}/articles/${id}`) | ||
| .then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text: ${res.statusText}` | ||
| ); | ||
| } | ||
| return res.json(); | ||
| }) | ||
| .then((data) => data) | ||
| .catch((error) => { | ||
| console.log(error); | ||
| throw error; | ||
| }); | ||
| } | ||
|
|
||
| function createArticle(articleData) { | ||
|
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. createArticle 데이터 검증 validation을 하면 좋을거같아요. |
||
| return fetch(`${BASE_URL}/articles`, { | ||
| method: 'POST', | ||
| body: JSON.stringify(articleData), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }) | ||
| .then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text: ${res.statusText}` | ||
| ); | ||
| } | ||
| return res.json(); | ||
| }) | ||
| .then((data) => data) | ||
| .catch((error) => { | ||
| console.log(error); | ||
| throw error; | ||
| }); | ||
| } | ||
|
|
||
| function updateArticle(id, articleData) { | ||
|
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. updateArticle 데이터 검증 validation을 하면 좋을거같아요. (2) |
||
| return fetch(`${BASE_URL}/articles/${id}`, { | ||
| method: 'PATCH', | ||
| body: JSON.stringify(articleData), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }) | ||
| .then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text: ${res.statusText}` | ||
| ); | ||
| } | ||
| return res.json(); | ||
| }) | ||
| .then((data) => data) | ||
| .catch((error) => { | ||
| console.log(error); | ||
| throw error; | ||
| }); | ||
| } | ||
|
|
||
| function deleteArticle(id) { | ||
| return fetch(`${BASE_URL}/articles/${id}`, { | ||
| method: 'DELETE', | ||
|
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. 소프트 딜리트에 대해서 고민해보면 좋을거 같습니다. |
||
| }) | ||
| .then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text: ${res.statusText}` | ||
| ); | ||
| } | ||
| return res.json(); | ||
| }) | ||
| .then((data) => data) | ||
| .catch((error) => { | ||
| console.log(error); | ||
| throw error; | ||
| }); | ||
| } | ||
|
|
||
| export const articleService = { | ||
| getArticleList, | ||
| getArticle, | ||
| createArticle, | ||
| updateArticle, | ||
| deleteArticle, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| const BASE_URL = 'https://panda-market-api-crud.vercel.app'; | ||
|
|
||
| const getProductList = async ({ | ||
| page = 1, | ||
| pageSize = 10, | ||
| orderBy = 'recent', | ||
| keyword = '', | ||
| } = {}) => { | ||
| try { | ||
| const res = await fetch( | ||
| `${BASE_URL}/products?page=${page}&pageSize=${pageSize}&orderBy=${orderBy}&keyword=${keyword}` | ||
| ); | ||
|
|
||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text: ${res.statusText}` | ||
| ); | ||
| } | ||
|
|
||
| const data = await res.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.error(error); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const getProduct = async ({ id } = {}) => { | ||
| try { | ||
| const res = await fetch(`${BASE_URL}/products/${id}`); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text: ${res.statusText}` | ||
| ); | ||
| } | ||
| const data = await res.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log(error); | ||
| throw error; | ||
| } | ||
| }; | ||
| const createProduct = async ({ productData } = {}) => { | ||
| try { | ||
| const res = await fetch(`${BASE_URL}/products`, { | ||
| method: 'POST', | ||
| body: JSON.stringify(productData), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text:${res.statusText}` | ||
| ); | ||
| } | ||
| const data = await res.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log(error); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const updateProduct = async ({ id, productData } = {}) => { | ||
| try { | ||
| const res = await fetch(`${BASE_URL}/products/${id}`, { | ||
| method: 'PATCH', | ||
| body: JSON.stringify(productData), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text:${res.statusText}` | ||
| ); | ||
| } | ||
| const data = await res.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log(error); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| const deleteProduct = async ({ id } = {}) => { | ||
| try { | ||
| const res = await fetch(`${BASE_URL}/products/${id}`, { | ||
| method: 'DELETE', | ||
| }); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `HTTP ERROR, status:${res.status}, text:${res.statusText}` | ||
| ); | ||
| } | ||
| const data = await res.json(); | ||
| return data; | ||
| } catch (error) { | ||
| console.log(error); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| export const productService = { | ||
| getProductList, | ||
| getProduct, | ||
| createProduct, | ||
| updateProduct, | ||
| deleteProduct, | ||
| }; | ||
|
|
||
| //getProductList({ page: 1, pageSize: 10, orderBy: 'recent' }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { articleService } from './lib/api/articleService.js'; | ||
| // import { productService } from './lib/api/productService.js'; | ||
|
|
||
| const articleData = { | ||
| image: 'https://example.com/...', | ||
| content: '게시글 내용입니다.', | ||
| title: '게시글 제목입니다.', | ||
| }; | ||
|
|
||
| const productData = { | ||
| images: ['https://example.com/...'], | ||
| tags: ['전자제품'], | ||
| price: 0, | ||
| description: 'string', | ||
| name: '상품 이름', | ||
| }; | ||
|
|
||
| const articles = await articleService.createArticle(articleData); | ||
|
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. test코드 jest, mocha 를 사용해서 실제로 테스트 코드를 짜보고 돌려봐도 좋을거 같아요. |
||
| console.log(articles); | ||
|
|
||
| // const products = await productService.getProductList(); | ||
| // console.log(products); | ||
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.
상태 코드에 대해서 잘 알고 계시나요?
공부해보시면 좋을꺼 같아요.