-
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.
feat: make request to Product - get, post, patch, delete
- Loading branch information
Showing
4 changed files
with
220 additions
and
91 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,92 @@ | ||
async function getArticleList(page=1, pageSize=10, keyword="") { | ||
const url = `https://sprint-mission-api.vercel.app/articles?page=${page}&pageSize=${pageSize}&keyword=${keyword}`; | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
console.log(json); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
async function getArticle(id) { | ||
const url = `https://sprint-mission-api.vercel.app/articles/${id}`; | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
console.log(json); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
async function createArticle(title, content, image) { | ||
const url = "https://sprint-mission-api.vercel.app/articles"; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: {"Content-Type": "application/json",}, | ||
body: JSON.stringify({title, content, image}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to create article: ${response.status}`); | ||
} | ||
|
||
const result = await response.json(); | ||
console.log("Article created successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
async function patchArticle(id,title, content, image) { | ||
const url = `https://sprint-mission-api.vercel.app/articles/${id}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "PATCH", | ||
headers: {"Content-Type": "application/json",}, | ||
body: JSON.stringify({title, content, image}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to patch article: ${response.status}`); | ||
} | ||
|
||
const result = await response.json(); | ||
console.log("Article patched successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
async function deleteArticle(id) { | ||
const url = `https://sprint-mission-api.vercel.app/articles/${id}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "DELETE", | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to delete article: ${response.status}`); | ||
} | ||
|
||
const result = await response.status; | ||
console.log("Article deleted successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
export { getArticleList, getArticle, createArticle, patchArticle, deleteArticle }; |
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,93 @@ | ||
async function getProductList(page = 1, pageSize = 10, keyword = "") { | ||
const url = `https://sprint-mission-api.vercel.app/products?page=${page}&pageSize=${pageSize}&keyword=${keyword}`; | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
console.log(json); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
async function getProduct(id) { | ||
const url = `https://sprint-mission-api.vercel.app/products/${id}`; | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
console.log(json); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
async function createProduct({ name, description, price, manufacturer, tags, images }) { | ||
const url = "https://sprint-mission-api.vercel.app/products"; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: {"Content-Type": "application/json",}, | ||
body: JSON.stringify({ name, description, price, tags, images }), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to create article: ${response.status}`); | ||
} | ||
|
||
const result = await response.json(); | ||
console.log("Product created successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
async function patchProduct({ id, name, description, price, manufacturer, tags, images }) { | ||
const url = `https://sprint-mission-api.vercel.app/products/${id}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "PATCH", | ||
headers: {"Content-Type": "application/json",}, | ||
body: JSON.stringify({ name, description, price, manufacturer, tags, images }), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to patch article: ${response.status}`); | ||
} | ||
|
||
const result = await response.json(); | ||
console.log("Product patched successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
|
||
async function deleteProduct(id) { | ||
const url = `https://sprint-mission-api.vercel.app/products/${id}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "DELETE", | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to delete Product: ${response.status}`); | ||
} | ||
|
||
const result = await response.status; | ||
console.log("Product deleted successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
export { getProductList, getProduct, createProduct, patchProduct, deleteProduct }; |
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
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 |
---|---|---|
@@ -1,90 +1,34 @@ | ||
async function getArticleList(page=1, pageSize=10, keyword="") { | ||
const url = `https://sprint-mission-api.vercel.app/articles?page=${page}&pageSize=${pageSize}&keyword=${keyword}`; | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
console.log(json); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
async function getArticle(id) { | ||
const url = `https://sprint-mission-api.vercel.app/articles/${id}`; | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Response status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
console.log(json); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
} | ||
|
||
async function createArticle(title, content, image) { | ||
const url = "https://sprint-mission-api.vercel.app/articles"; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: {"Content-Type": "application/json",}, | ||
body: JSON.stringify({title, content, image}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to create article: ${response.status}`); | ||
} | ||
|
||
const result = await response.json(); | ||
console.log("Article created successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
async function patchArticle(id,title, content, image) { | ||
const url = `https://sprint-mission-api.vercel.app/articles/${id}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "PATCH", | ||
headers: {"Content-Type": "application/json",}, | ||
body: JSON.stringify({title, content, image}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to patch article: ${response.status}`); | ||
} | ||
|
||
const result = await response.json(); | ||
console.log("Article patched successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
|
||
async function deleteArticle(id) { | ||
const url = `https://sprint-mission-api.vercel.app/articles/${id}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "DELETE", | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to delete article: ${response.status}`); | ||
} | ||
|
||
const result = await response.status; | ||
console.log("Article deleted successfully:", result); | ||
} catch (error) { | ||
console.error("Error:", error.message); | ||
} | ||
} | ||
import { getArticleList, getArticle, createArticle, patchArticle, deleteArticle} from './ArticleService.js'; | ||
import { getProductList, getProduct, createProduct, patchProduct, deleteProduct } from './ProductService.js'; | ||
|
||
// const data = { | ||
// id: 586, | ||
// name: "zino patch test", | ||
// description: "edit test", | ||
// price: 99900, | ||
// manufacturer: "edit zino", | ||
// tags: ["what tag"], | ||
// images: ["image url"] | ||
// } | ||
|
||
// console.log(deleteProduct(586)); | ||
// console.log(getProduct(586)); | ||
|
||
// const data = { | ||
// name: "zino test", | ||
// description: "test", | ||
// price: 1000, | ||
// manufacturer: "zino", | ||
// tags: ["what tag"], | ||
// images: ["image url"] | ||
// } | ||
|
||
// const data = { | ||
// id: 586, | ||
// name: "zino test", | ||
// description: "test", | ||
// price: 1000, | ||
// manufacturer: "zino", | ||
// tags: ["what tag"], | ||
// images: ["image url"] | ||
// } |