Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions README.md

This file was deleted.

117 changes: 117 additions & 0 deletions lib/api/articleService.js
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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상태 코드에 대해서 잘 알고 계시나요?
공부해보시면 좋을꺼 같아요.

throw new Error(
`HTTP ERROR, status:${res.status}, text: ${res.statusText}`
);
}
return res.json();
})
.then((data) => {
return data;
})
.catch((error) => {
console.log(error);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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,
};
113 changes: 113 additions & 0 deletions lib/api/productService.js
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' });
22 changes: 22 additions & 0 deletions main.js
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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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);