From 91797fab9d9164b915761aa27b103007b3b6eac1 Mon Sep 17 00:00:00 2001 From: qlwlsmwoddl0813-wq Date: Mon, 29 Dec 2025 10:51:51 +0900 Subject: [PATCH] AddSprinsMission3 --- ArticleService.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++ ProductService.js | 57 +++++++++++++++++++++++++++++++++++++++++ main.js | 61 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 ArticleService.js create mode 100644 ProductService.js create mode 100644 main.js diff --git a/ArticleService.js b/ArticleService.js new file mode 100644 index 0000000..3b5392a --- /dev/null +++ b/ArticleService.js @@ -0,0 +1,65 @@ +import axios from "axios"; + +const API_URL = "https://panda-market-api-crud.vercel.app/articles"; + +export function getArticleList(params) { + return axios + .get(API_URL, { + params: params, + }) + .then(function (response) { + console.log("게시글 목록을 성공적으로 불러왔습니다."); + return response.data; + }) + .catch(function (error) { + console.log("목록 조회 중 에러 발생: " + error.message); + }); +} + +export function getArticle(id) { + return axios + .get(API_URL + "/" + id) + .then(function (res) { + return res.data; + }) + .catch(function (err) { + console.log("상세 조회 실패!"); + }); +} + +export function createArticle(data) { + return axios + .post(API_URL, { + title: data.title, + content: data.content, + image: data.image, + }) + .then(function (res) { + return res.data; + }) + .catch(function (err) { + console.log("글 생성 실패"); + }); +} + +export function patchArticle(id, updateData) { + return axios + .patch(API_URL + "/" + id, updateData) + .then(function (res) { + return res.data; + }) + .catch(function (err) { + console.log("글 수정 실패"); + }); +} + +export function deleteArticle(id) { + return axios + .delete(API_URL + "/" + id) + .then(function (res) { + return res.data; + }) + .catch(function (err) { + console.log("글 삭제 실패"); + }); +} diff --git a/ProductService.js b/ProductService.js new file mode 100644 index 0000000..8b1fdc3 --- /dev/null +++ b/ProductService.js @@ -0,0 +1,57 @@ +import axios from "axios"; + +const PRODUCT_URL = "https://panda-market-api-crud.vercel.app/products"; + +export async function getProductList(params) { + try { + const response = await axios.get(PRODUCT_URL, { + params: params, + }); + console.log("상품 목록 조회 완료"); + return response.data; + } catch (error) { + console.log("상품 목록 가져오다 에러 발생함"); + } +} + +export async function getProduct(id) { + try { + const response = await axios.get(PRODUCT_URL + "/" + id); + return response.data; + } catch (error) { + console.log("상품 상세 조회 에러"); + } +} + +export async function createProduct(item) { + try { + const response = await axios.post(PRODUCT_URL, { + name: item.name, + description: item.description, + price: item.price, + tags: item.tags, + images: item.images, + }); + return response.data; + } catch (error) { + console.log("상품 등록 에러"); + } +} + +export async function patchProduct(id, data) { + try { + const response = await axios.patch(PRODUCT_URL + "/" + id, data); + return response.data; + } catch (error) { + console.log("상품 수정 에러"); + } +} + +export async function deleteProduct(id) { + try { + const response = await axios.delete(PRODUCT_URL + "/" + id); + return response.data; + } catch (error) { + console.log("상품 삭제 에러"); + } +} diff --git a/main.js b/main.js new file mode 100644 index 0000000..05b2d4a --- /dev/null +++ b/main.js @@ -0,0 +1,61 @@ +import { + getArticleList, + getArticle, + createArticle, + patchArticle, + deleteArticle, +} from "./ArticleService.js"; + +import { + getProductList, + getProduct, + createProduct, + patchProduct, + deleteProduct, +} from "./ProductService.js"; + +async function main() { + console.log("--- 테스트를 시작합니다 ---"); + + console.log("1. 아티클 목록 가져오는 중..."); + await getArticleList({ page: 1, pageSize: 5, keyword: "" }); + + console.log("2. 새 글 작성 중..."); + const newArt = await createArticle({ + title: "테스트 글 제목 /박가연", + content: "테스트 내용입니다.", + image: "https://panda.com/image.jpg", + }); + + if (newArt) { + console.log("새로 만든 글 ID: " + newArt.id); + await getArticle(newArt.id); + await patchArticle(newArt.id, { title: "수정한 제목" }); + await deleteArticle(newArt.id); + console.log("아티클 테스트 끝!"); + } + + console.log("\n3. 상품 목록 가져오는 중..."); + await getProductList({ page: 1, pageSize: 5, keyword: "" }); + + console.log("4. 새 상품 등록 중..."); + const newProd = await createProduct({ + name: "테스트 상품 /박가연", + description: "설명입니다", + price: 5000, + tags: ["공부"], + images: ["test.jpg"], + }); + + if (newProd) { + console.log("새로 만든 상품 ID: " + newProd.id); + await getProduct(newProd.id); + await patchProduct(newProd.id, { price: 7000 }); + await deleteProduct(newProd.id); + console.log("상품 테스트 끝!"); + } + + console.log("\n--- 모든 테스트가 종료되었습니다 ---"); +} + +main();