CRUD : Create(POST), Read(GET), Update(PUT), Delete(DELETE)
controllers - <contactControllers.js> : 데이터 추가
※create() : 데이터베이스에 새로운 도큐먼트를 추가
const asynchHandler = require("express-async-handler");
const Contact = require("../models/contactModel"); //contactModel.js 모델을 가져옴
(...)
//Post all contact, /contact
const createContect = asynchHandler(async (req, res) => {
console.log(req.body);
const { name, email, phone } = req.body;
if (!name || !email || !phone) {
res.status(400).send("No Input Value");
}
const contact = await Contact.create({ name, email, phone }); //도큐먼트 추가
res.status(201).send("Create Contact");
});
(...)
controllers - <contactControllers.js> : 데이터 가져오기
※find() : 지정한 조건의 맞는 도큐먼트를 찾음, 조건을 지정하지 않으면 도큐먼트 전부 가져옴
※findById() : 아이디 값을 기준으로 도큐먼트를 찾음
(...)
//Get all contact, /contact
const getAllContact = asynchHandler(async (req, res) => {
const contact = await Contact.find(); //도큐먼트 가져오기
res.status(200).send(contact);
});
(...)
//Get get contact, /contact/:id
const getContact = asynchHandler(async (req, res) => {
const contact = await Contact.findById(req.params.id); //특정 ID 도큐먼트 가져오기
res.status(200).send(contact);
});
(...)
controllers - <contactControllers.js> : 데이터 수정하기
※save() : 수정된 데이터를 다시 저장
(...)
//Put update contact, /contact/:id
const updateContact = asynchHandler(async (req, res) => {
const { name, email, phone } = req.body; //name, email, phone를 요청 본문에 할당
const contact = await Contact.findById(req.params.id); //특정 ID 도큐먼트 찾기
if (!contact) { //특정 ID 도큐먼트가 없을 경우
res.status(400).send("No Document");
}
(contact.name = name), (contact.email = email), (contact.phone = phone); //req.body에 담긴 새로운 값을 contact에 지정
contact.save(); //수정된 자료를 데이터베이스에 저장
res.status(200).send(contact);
});
(...)
controllers - <contactControllers.js> : 데이터 삭제하기
※deleteOne() : 도큐먼트를 삭제
(...)
//Delete delete contact, /contact/:id
const deleteContact = asynchHandler(async (req, res) => {
const contact = await Contact.findById(req.params.id); //특정 ID 도큐먼트 찾기
if (!contact) {
res.status(400).send("No Document");
}
await Contact.deleteOne(); //도큐먼트 삭제
res.status(200).send(`Delete Contact for ID : ${req.params.id}`);
});
(...)
controllers - <contactControllers.js> 전체 코드
const asynchHandler = require("express-async-handler");
const Contact = require("../models/contactModel");
//Get all contact, /contact
const getAllContact = asynchHandler(async (req, res) => {
const contact = await Contact.find();
res.status(200).send(contact);
});
//Post all contact, /contact
const createContect = asynchHandler(async (req, res) => {
console.log(req.body);
const { name, email, phone } = req.body;
if (!name || !email || !phone) {
res.status(400).send("No Input Value");
}
const contact = await Contact.create({ name, email, phone });
res.status(201).send("Create Contact");
});
//Get get contact, /contact/:id
const getContact = asynchHandler(async (req, res) => {
const contact = await Contact.findById(req.params.id);
res.status(200).send(contact);
});
//Put update contact, /contact/:id
const updateContact = asynchHandler(async (req, res) => {
const { name, email, phone } = req.body;
const contact = await Contact.findById(req.params.id);
if (!contact) {
res.status(400).send("No Document");
}
(contact.name = name), (contact.email = email), (contact.phone = phone);
contact.save();
res.status(200).send(contact);
});
//Delete delete contact, /contact/:id
const deleteContact = asynchHandler(async (req, res) => {
const contact = await Contact.findById(req.params.id);
if (!contact) {
res.status(400).send("No Document");
}
await Contact.deleteOne();
res.status(200).send(`Delete Contact for ID : ${req.params.id}`);
});
module.exports = {
getAllContact,
createContect,
getContact,
updateContact,
deleteContact,
};
'Do it Node.js > EJS BackEnd' 카테고리의 다른 글
16. include, 연락처 표시 (0) | 2024.03.14 |
---|---|
15. EJS (0) | 2024.03.13 |
13. RESTful API (0) | 2024.03.11 |
12. 몽고DB, 스키마 (0) | 2024.03.08 |
11. 미들웨어 (0) | 2024.03.06 |