Skip to content
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
# Keep environment variables out of version control
.env

/generated/prisma
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prisma.pinToPrisma6": false
}
48 changes: 48 additions & 0 deletions ArticleService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import instance from './api.js'

export async function getArticleList({ page = 1, pageSize = 10, keyword = '' } = {}) {
try {
const params = { page, pageSize, keyword };
const res = await instance.get('/articles', { params });
return res.data;
} catch (e) {
console.log(e.message)
}
}

export async function getArticle(id) {
try {
const res = await instance.get(`/articles/${id}`);
return res.data;
} catch (e) {
console.log(e.message)
}
}

export async function createArticle({ title, content, image }) {
try {
const body = { title, content, image };
const res = await instance.post('/articles', body);
return res.data;
} catch (e) {
console.log(e.message)
}
}

export async function patchArticle(id, articleData) {
try {
const res = await instance.patch(`/articles/${id}`, articleData);
return res.data;
} catch (e) {
console.log(e.message)
}
}

export async function deleteArticle(id) {
try {
const res = await instance.delete(`/articles/${id}`);
return res.data;
} catch (e) {
console.log(e.message)
}
}
48 changes: 48 additions & 0 deletions ProductService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import instance from './api.js'

export async function getProductList({ page = 1, pageSize = 10, keyword = '' }) {
try {
const body = { page, pageSize, keyword };
const res = await instance.get('/products', body);
return res.data
} catch (e) {
console.log(e.message)
}
}

export async function getProduct(id) {
try {
const res = await instance.get(`/products/${id}`);
return res.data;
} catch (e) {
console.log(e.message)
}
}

export async function createProduct({ name, description, price, tags, images }) {
try {
const body = { name, description, price, tags, images }
const res = await instance.post('/products', body)
return res.data;
} catch (e) {
console.log(e.message)
}
}

export async function patchProduct(id, productsData) {
try {
const res = await instance.patch(`/products/${id}`, productsData)
return res.data;
} catch (e) {
console.log(e.message)
}
}

export async function deleteProduct(id) {
try {
const res = await instance.delete(`/products/${id}`)
return res.data;
} catch (e) {
console.log(e.message)
}
}
8 changes: 8 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import axios from 'axios'

const instance = axios.create({
baseURL: 'https://panda-market-api-crud.vercel.app',
timeout : 5000,
})

export default instance;
34 changes: 34 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// app.js
import * as dotenv from "dotenv";
dotenv.config();

import express from "express";
import articlesRouter from "./routes/articles.js";
import articleCommentsRouter from "./routes/articleComments.js";
import marketCommentsRouter from "./routes/marketComments.js";

const app = express();
app.use(express.json());

// 라우터 연결
app.use("/articles", articlesRouter);
app.use("/", articleCommentsRouter); // /articles/:articleId/comments, /article-comments/:id
app.use("/", marketCommentsRouter); // /products/:productId/comments, /market-comments/:id

// 공통 에러 핸들러
app.use((err, req, res, next) => {
console.error(err);

if (err.code === "P2025") {
return res.status(404).send({ message: "리소스를 찾을 수 없습니다." });
}

res.status(500).send({
message: err.message || "서버 에러가 발생했습니다.",
});
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
Empty file added https/request.https
Empty file.
149 changes: 149 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import {
getArticleList,
getArticle,
createArticle,
patchArticle,
deleteArticle,
} from "./ArticleService.js";
import {
getProductList,
getProduct,
createProduct,
patchProduct,
deleteProduct,
} from "./ProductService.js";

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
res.send("Hello World!");
});


getArticleList({ page: 1, pageSize: 10, keyword: "" })
.then((data) => {
console.log(data);
})
.catch((e) => {
console.error("에러발생");
console.log(e.message);
});

getArticle(5124)
.then((data) => {
console.log(data);
})
.catch((e) => {
console.log("에러 발생");
console.log(e.message);
});

const test = {
image: "https://example.com/...",
content: "게시글 내용입니다.",
title: "게시글 제목입니다.",
};

createArticle(test)
.then((data) => {
console.log(data);
})
.catch((e) => {
console.log("에러발생");
console.log(e.message);
});

const patchData = {
image: "https://example.com/...",
content: "수정된 내용입니다.",
title: "수정된 제목입니다.",
};

patchArticle(5124, patchData)
.then((data) => {
console.log(data);
})
.catch((e) => {
console.log(e.message);
});

deleteArticle(5124)
.then((data) => {
console.log("데이터가 삭제되었습니다.", data);
})
.catch((e) => {
console.log(e.message);
});

/* Article 호출 구분 */

/* Products 호출 시작*/

const parametor = {
page: 1,
pageSize: 10,
keyword: "",
};

getProductList({ parametor })
.then((data) => {
console.log(data);
})
.catch((e) => {
console.log(e.message);
console.log(e.response);
});

getProduct(2650)
.then((data) => {
console.log(data);
})
.catch((e) => {
console.log(e.message);
});

const Proparametor = {
name: "선풍기",
description: "샤오미 선풍기가 5천원!",
price: 3000,
tags: [],
images: [],
};

createProduct(Proparametor)
.then((data) => {
console.log(data);
})
.catch((e) => {
console.log(e.message);
});

const patchParametor = {
name: "선풍기",
description: "샤오미 선풍기가 5천원!",
price: 5000,
tags: [],
images: [],
};

patchProduct(2667, patchParametor)
.then((data) => {
console.log("데이터가 정상적으로 수정되었습니다", data);
})
.catch((e) => {
console.log(e.message);
});

deleteProduct(2667)
.then((data) => {
console.log("데이터가 정상적으로 삭제되었습니다", data);
})
.catch((e) => {
console.log(e.message);
});

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Loading