app.js
// app.js
import express from 'express';
import PostsRouter from './routes/posts.router.js';
const app = express();
const PORT = 3017;
app.use(express.json());
app.use('/api', [PostsRouter]);
app.listen(PORT, () => {
console.log(PORT, '포트로 서버가 열렸어요!');
});
schema
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
// schema.prisma
model Products {
productId Int @id @default(autoincrement()) @map("productId")
productName String @unique @map("productName")
price Int @default(1000) @map("price")
info String? @map("info") @db.Text
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
@@map("Products")
}
model Posts {
postId Int @id @default(autoincrement()) @map("postId")
title String @map("title")
content String @db.Text @map("content")
password String @map("password")
updatedAt DateTime @updatedAt @map("updatedAt")
createdAt DateTime @default(now()) @map("createdAt")
@@map("Post")
}
router
// routes/posts.router.js
import express from 'express';
import { prisma } from '../utils/prisma/index.js';
const router = express.Router();
/* 게시글 생성 */
router.post('/posts', async(req,res,next) =>{
const {title, content , password} = req.body
const post = await prisma.posts.create({
data : {
title,
content,
password,
},
});
return res.status(201).json({data : post})
});
/* 게시글 목록 조회 */
router.get("/posts", async(req,res,next) =>{
// 게시글 내용이 포함되지 않도록 구현
const posts = await prisma.posts.findMany({
select : {
postId: true,
title : true,
createdAt: true,
updatedAt : true,
},
orderBy: {
createdAt: 'desc', // createdAt을 내림차순으로 정렬
},
})
return res.status(201).json({data: posts})
});
/* 게시글 상세 조회 */
router.get("/posts/:postId", async(req,res,next) =>{
const {postId} = req.params;
const post = await prisma.posts.findFirst({
where : {postId : +postId},
select : {
postId : true,
title : true,
content : true,
createdAt: true,
updatedAt : true,
}
});
return res.status(201).json({data: post})
});
/* 게시글 수정 */
router.put("/posts/:postId", async(req,res,next) =>{
const {postId} = req.params;
const {title , content , password} = req.body;
const post = await prisma.posts.findUnique({
where : {postId : +postId},
});
if(!post){
return res.status(404).json({message : "게시글이 존재하지 않습니다."})
}else if(post.password !== password){
return res.status(401).json({message : "비밀번호가 일치하지 않습니다."})
}
await prisma.posts.update({
data : {title , content},
where:{
postId : +postId,
password
}
});
return res.status(200).json({data: "게시글 수정이 완료되었습니다."})
});
/* 게시글 삭제 */
router.delete("/posts/:postId", async(req,res,next) =>{
const {postId} = req.params;
const {password} = req.body;
const post = await prisma.posts.findUnique({
where : {postId : +postId},
});
if(!post){
return res.status(404).json({message : "게시글이 존재하지 않습니다."})
}else if(post.password !== password){
return res.status(401).json({message : "비밀번호가 일치하지 않습니다."})
}
await prisma.posts.delete({
where:{
postId : +postId
}
});
return res.status(200).json({data: "게시글이 삭제되었습니다."})
});
export default router;
사용한 패키지
{
"name": "prismacrud",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type" : "module",
"scripts": {
"dev": "nodemon app.js"
},
"dependencies": {
"@prisma/client": "^5.2.0",
"express": "^4.18.2",
"prisma": "^5.2.0"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}'node.js' 카테고리의 다른 글
| 주특기 프로젝트 시작 (0) | 2023.09.15 |
|---|---|
| 게시판 만들기 몽구스에서 prisma 로 변경하기 (0) | 2023.09.02 |
| 게시판 만들기 (완성버전) (0) | 2023.08.29 |
| 게시판만들기 (2) 댓글창 만들기 (0) | 2023.08.29 |
| 게시판 만들기 (0) | 2023.08.28 |