Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic 정진호 sprint4 #38

Merged
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
92 changes: 92 additions & 0 deletions sprint_4/ArticleService.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

export 를 하단에 모아서 작성하셔도 되긴 하지만 일반적으로 함수 앞에 선언을 많이 해서 사용합니다.

함수 앞에 선언하면 하단에 다시 함수이름을 선언하지 않아도 되고 어떤 함수가 export 되었는지 유무도 한눈에 보이므로

함수에 같이 선언해주세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
async function getArticleList(page=1, pageSize=10, keyword="") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

javascript 여서 타입체크를 하지 않고 바로 값을 받을 수 있는 상황입니다.

예를들어 page 파라미터 위치에 숫자가 아닌 다른 문자열 값이 넘어와도 그대로 수행됩니다.

추후에 typescript 를 배우면 그런 문제는 ide 에서 바로 경고가 뜨겠지만 지금은 그렇지 않은 상황이니 유효성 체크를 해주는 방식을 익혀두시고 수업 진도에 맞게 유효성 체크 기술을 사용해주시면 좋겠습니다.

여기서는 page, pageSize 가 number 타입인지 양수의 숫자가 제대로 들어왔는지 체크해주는 로직이 들어가면 좋겠습니다.

참고 : [isInteger]

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

Choose a reason for hiding this comment

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

id 가 number 타입이라면 제대로 number 타입인지 양수인지 체크해주는 유효성 로직을 추가해주세요.

const url = `https://sprint-mission-api.vercel.app/articles/${id}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

공통되는 url 은 파일 상단 또는 상수로 따로 관리하는걸 추천드립니다.

https://sprint-mission-api.vercel.app

해당 부분은 api의 엔드포인트이므로 별도로 공통 관리하는 방법을 주로 사용합니다.

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";
Copy link
Collaborator

Choose a reason for hiding this comment

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

해당 부분도 마찬가지로 공통으로 url 을 선언하여 사용하는걸 권장드립니다!


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 = "") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

여기도 유효성 체크 로직 추가!

const url = `https://sprint-mission-api.vercel.app/products?page=${page}&pageSize=${pageSize}&keyword=${keyword}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

url 상수로 분리 필요합니다!

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 };
12 changes: 12 additions & 0 deletions sprint_4/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sprint 4</title>
</head>
<body>
<h1>Sprint 4</h1>
<script type="module" src="main.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions sprint_4/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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"]
// }
Loading