Skip to content

Commit

Permalink
스프린트4 제출
Browse files Browse the repository at this point in the history
  • Loading branch information
dlgustj321 committed Nov 23, 2024
1 parent 1e93068 commit 9459209
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
75 changes: 75 additions & 0 deletions SM1-2/ArticleService.js
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));
}
80 changes: 80 additions & 0 deletions SM1-2/ProductService.js
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);
}
}
59 changes: 59 additions & 0 deletions SM1-2/main.js
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));
})();
6 changes: 6 additions & 0 deletions SM1-2/package.json
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"
}
Empty file added {
Empty file.

0 comments on commit 9459209

Please sign in to comment.