-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e93068
commit 9459209
Showing
5 changed files
with
220 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,75 @@ | ||
// ArticleService.js | ||
|
||
const BASE_URL = 'https://sprint-mission-api.vercel.app/articles'; | ||
|
||
// Fetch articles list | ||
export function getArticleList(page = 1, pageSize = 10, keyword = '') { | ||
return fetch(`${BASE_URL}?page=${page}&pageSize=${pageSize}&keyword=${keyword}`) | ||
.then((response) => { | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return response.json(); | ||
}) | ||
.catch((error) => console.error('Failed to fetch article list:', error)); | ||
} | ||
|
||
// Fetch a single article by ID | ||
export function getArticle(id) { | ||
return fetch(`${BASE_URL}/${id}`) | ||
.then((response) => { | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return response.json(); | ||
}) | ||
.catch((error) => console.error('Failed to fetch article:', error)); | ||
} | ||
|
||
// Create a new article | ||
export function createArticle(title, content, image) { | ||
return fetch(BASE_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ title, content, image }), | ||
}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return response.json(); | ||
}) | ||
.catch((error) => console.error('Failed to create article:', error)); | ||
} | ||
|
||
// Update an existing article | ||
export function patchArticle(id, data) { | ||
return fetch(`${BASE_URL}/${id}`, { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return response.json(); | ||
}) | ||
.catch((error) => console.error('Failed to update article:', error)); | ||
} | ||
|
||
// Delete an article by ID | ||
export function deleteArticle(id) { | ||
return fetch(`${BASE_URL}/${id}`, { method: 'DELETE' }) | ||
.then((response) => { | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return response.json(); | ||
}) | ||
.catch((error) => console.error('Failed to delete article:', error)); | ||
} |
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,80 @@ | ||
// ProductService.js | ||
|
||
const BASE_URL = 'https://sprint-mission-api.vercel.app/products'; | ||
|
||
// Fetch product list | ||
export async function getProductList(page = 1, pageSize = 10, keyword = '') { | ||
try { | ||
const response = await fetch(`${BASE_URL}?page=${page}&pageSize=${pageSize}&keyword=${keyword}`); | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
console.error('Failed to fetch product list:', error); | ||
} | ||
} | ||
|
||
// Fetch a single product by ID | ||
export async function getProduct(id) { | ||
try { | ||
const response = await fetch(`${BASE_URL}/${id}`); | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
console.error('Failed to fetch product:', error); | ||
} | ||
} | ||
|
||
// Create a new product | ||
export async function createProduct(name, description, price, tags, images) { | ||
try { | ||
const response = await fetch(BASE_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ name, description, price, tags, images }), | ||
}); | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
console.error('Failed to create product:', error); | ||
} | ||
} | ||
|
||
// Update an existing product | ||
export async function patchProduct(id, data) { | ||
try { | ||
const response = await fetch(`${BASE_URL}/${id}`, { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
console.error('Failed to update product:', error); | ||
} | ||
} | ||
|
||
// Delete a product by ID | ||
export async function deleteProduct(id) { | ||
try { | ||
const response = await fetch(`${BASE_URL}/${id}`, { method: 'DELETE' }); | ||
if (!response.ok) { | ||
console.error(`Error: ${response.statusText}`); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
console.error('Failed to delete product:', error); | ||
} | ||
} |
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,59 @@ | ||
import { | ||
getArticleList, | ||
getArticle, | ||
createArticle, | ||
patchArticle, | ||
deleteArticle, | ||
} from './ArticleService.js'; | ||
|
||
import { | ||
getProductList, | ||
getProduct, | ||
createProduct, | ||
patchProduct, | ||
deleteProduct, | ||
} from './ProductService.js'; | ||
|
||
(async () => { | ||
// Test ArticleService functions | ||
console.log('Articles List:', await getArticleList(1, 5, 'test')); | ||
|
||
const articleId = 164; // 유효한 article ID로 수정 | ||
console.log('Single Article:', await getArticle(articleId)); | ||
|
||
console.log( | ||
'Article Created:', | ||
await createArticle('New Title', 'Content of the article', 'image_url') | ||
); | ||
|
||
console.log( | ||
'Article Updated:', | ||
await patchArticle(articleId, { title: 'Updated Title' }) | ||
); | ||
|
||
console.log('Article Deleted:', await deleteArticle(articleId)); | ||
|
||
// Test ProductService functions | ||
console.log('Products List:', await getProductList(1, 5, 'keyword')); | ||
|
||
const productId = 124; // 유효한 product ID로 수정 | ||
console.log('Single Product:', await getProduct(productId)); | ||
|
||
console.log( | ||
'Product Created:', | ||
await createProduct( | ||
'New Product', | ||
'Description', | ||
100, | ||
['tag1', 'tag2'], | ||
['image_url'] | ||
) | ||
); | ||
|
||
console.log( | ||
'Product Updated:', | ||
await patchProduct(productId, { name: 'Updated Product' }) | ||
); | ||
|
||
console.log('Product Deleted:', await deleteProduct(productId)); | ||
})(); |
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,6 @@ | ||
{ | ||
"name": "your-project", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "main.js" | ||
} |