Skip to content

Commit

Permalink
feat: make request to Product - get, post, patch, delete
Browse files Browse the repository at this point in the history
  • Loading branch information
zinojung committed Nov 24, 2024
1 parent bce8088 commit 891587e
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 91 deletions.
92 changes: 92 additions & 0 deletions sprint_4/ArticleService.js
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 };
93 changes: 93 additions & 0 deletions sprint_4/ProductService.js
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 };
2 changes: 1 addition & 1 deletion sprint_4/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</head>
<body>
<h1>Sprint 4</h1>
<script src="main.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>
124 changes: 34 additions & 90 deletions sprint_4/main.js
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"]
// }

0 comments on commit 891587e

Please sign in to comment.