Node.js 심화 1주차_4_과제
2022.12.28 - [JavaScript] - Node.js 심화 1주차_4
목표 : 여기서 배운 아키텍처대로 게시글 CRUD 구현해보기!
📌 ThunderClient
[POST] http://localhost:4000/api/posts
Body : json형태
{
"nickname": "Developer",
"password": "1234",
"title": "안녕하세요",
"content": "안녕하세요 content 입니다."
}
게시글 상세 조회
📄 routes/posts.routes.js
router.get('/:postId', postsController.getPost);
url 설정
📄 controllers/posts.controller.js
getPost = async(req, res, next) => {
const { postId } = req.params;
const post = await this.postService.findPost(postId);
res.status(200).json({ data: post })
}
클라이언트로 부터 postId를 request
서버로부터 받은 데이터를 다시 클라이언트에게 response
📄 services/posts.services.js
findPost = async (postId) => {
const Post = await this.postRepository.findPost(postId);
return {
postId : Post.postId,
nickname : Post.nickname,
title : Post.title,
createdAt : Post.createdAt,
updatedAt : Post.updatedAt
};
}
DB 저장소에서 원하는 값 가져와 보기 편하게 가공(PK로 가져왔으니 데이터 한 줄(row) 가져온 것)
📄 repositories/posts.repository.js
findPost = async (postId) => {
const post = await Posts.findByPk(postId);
return post;
}
Primary Key 이용해 게시글 상세조회할 특정 데이터 하나 가져와서 서버로 넘겨주기.
📌 ThunderClient
[GET] http://localhost:4000/api/posts/2
게시글 수정
📄 routes/posts.routes.js
router.put('/:postId', postsController.putPost);
📄 controllers/posts.controller.js
putPost = async (req, res, next) => {
const { postId } = req.params;
const { password, title, content } = req.body;
const putPostData = await this.postService.putPost(postId, password, title, content);
res.status(201).json({ data: putPostData });
}
📄 services/posts.services.js
putPost = async(postId, password, title, content) => {
const putPostData = await this.postRepository.putPost(postId, password, title, content);
return {
postId: putPostData.null,
nickname: putPostData.nickname,
title: putPostData.title,
content: putPostData.content,
createdAt: putPostData.createdAt,
updatedAt: putPostData.updatedAt
}
}
↓ 강의 자료 참고해 수정
putPost = async(postId, password, title, content) => {
const Post = await this.postRepository.findPost(postId);
if(!Post) throw new Error("Post doesn't exist");
await this.postRepository.putPost(postId, password, title, content);
const putPostData = await this.postRepository.findPost(postId);
return {
postId: putPostData.null,
nickname: putPostData.nickname,
title: putPostData.title,
content: putPostData.content,
createdAt: putPostData.createdAt,
updatedAt: putPostData.updatedAt
}
}
서버에서 해당하는 포스터 없으면 수정 불가능하도록 error 던져주는 것이 좋음!
repo에서는 원하는 데이터 업데이트만 해주면 됨!
📄 repositories/posts.repository.js
putPost = async(postId, password, title, content) => {
const findPost = await Posts.findByPk(postId);
if(findPost.password === password){
findPost.title = title;
findPost.content = content;
await findPost.save();
}
const putPostData = await Posts.findByPk(postId);
return putPostData;
}
↓ 강의 자료 참고해 수정
putPost = async(postId, password, title, content) => {
const putPostData = await Posts.update(
{ title, content },
{ where: { postId, password } }
);
return putPostData;
}
update명령어가 있다는 걸 알게됨!
📌 ThunderClient
[PUT] http://localhost:4000/api/posts/1
Body : json형태
{
"password": "1234",
"title": "안녕하세요",
"content": "안녕하세요 수정된 content 입니다."
}
코드 수정 후
📌 ThunderClient
[PUT] http://localhost:4000/api/posts/2
Body : json형태
{
"password": "1234",
"title": "수정",
"content": "수정확인"
}
게시글 삭제
📄 routes/posts.routes.js
router.delete('/:postId', postsController.deletePost);
📄 controllers/posts.controller.js
deletePost = async (req, res, next) => {
const { postId } = req.params;
const { password } = req.body;
const deletePostData = await this.postService.deletePost(postId, password);
res.status(200).json({ data: deletePostData });
}
📄 services/posts.services.js
deletePost = async(postId, password) => {
const Post = await this.postRepository.findPost(postId);
if(!Post) throw new Error("Post doesn't exist");
await this.postRepository.deletePost(postId, password);
return {
postId: Post.null,
nickname: Post.nickname,
title: Post.title,
content: Post.content,
createdAt: Post.createdAt,
updatedAt: Post.updatedAt
}
}
삭제할 데이터 Controller에게 리턴해 resposnse 데이터가 될 예정 전달
📄 repositories/posts.repository.js
deletePost = async(postId, password) => {
const deletePostData = await Posts.destroy(
{where: { postId, password }}
);
return deletePostData;
}
📌 ThunderClient
[DELETE] http://localhost:4000/api/posts/2
Body : json형태
{ "password": "1234"}
📌 ThunderClient
[DELETE] http://localhost:4000/api/posts/3
Body : json형태
{ "password": "4321"}
📌 git repo
godee95/layered-architecture-pattern: node.js 내배캠 layered-architecture-pattern (github.com)
GitHub - godee95/layered-architecture-pattern: node.js 내배캠 layered-architecture-pattern
node.js 내배캠 layered-architecture-pattern. Contribute to godee95/layered-architecture-pattern development by creating an account on GitHub.
github.com
'JavaScript' 카테고리의 다른 글
Node.js 심화 1주차_5_Unit Test (1) | 2022.12.29 |
---|---|
Node.js 심화 1주차_5 (0) | 2022.12.29 |
Node.js 숙련주차 숙제 최종 제출 (0) | 2022.12.28 |
Node.js 심화 1주차_4 (0) | 2022.12.28 |
Node.js 심화 1주차_3 (0) | 2022.12.27 |