on
Angular 12 DB연결해서 게시판 만들기(CRUD)(1)
Angular 12 DB연결해서 게시판 만들기(CRUD)(1)
에 아래 코드를 작성하면됩니다.
const db = require("../models"); const Tutorial = db.tutorials; // 튜토리얼DB 작성과 저장 exports.create = (req, res) => { // Validate request if (!req.body.title) { res.status(400).send({ message: "Content can not be empty!" }); return; } // 샐로운 튜토리얼 작성 const tutorial = new Tutorial({ title: req.body.title, description: req.body.description, published: req.body.published ? req.body.published : false, }); // DB에 저장 tutorial .save(tutorial) .then((data) => { res.send(data); }) .catch((err) => { res.status(500).send({ message: err.message || "Some error occurred while creating the Tutorial.", }); }); }; // DB에서 튜토리얼을 모두 조회 exports.findAll = (req, res) => { const title = req.query.title; var condition = title ? { title: { $regex: new RegExp(title), $options: "i" } } : {}; Tutorial.find(condition) .then((data) => { res.send(data); }) .catch((err) => { res.status(500).send({ message: err.message || "Some error occurred while retrieving tutorials.", }); }); }; // 특정 id로 DB에서 튜토리얼 불러오기 exports.findOne = (req, res) => { const id = req.params.id; Tutorial.findById(id) .then((data) => { if (!data) res.status(404).send({ message: "Not found Tutorial with id " + id }); else res.send(data); }) .catch((err) => { res .status(500) .send({ message: "Error retrieving Tutorial with id=" + id }); }); }; // 요청이 들어온 ID에대한 DB 업데이트 exports.update = (req, res) => { if (!req.body) { return res.status(400).send({ message: "Data to update can not be empty!", }); } const id = req.params.id; Tutorial.findByIdAndUpdate(id, req.body, { useFindAndModify: false }) .then((data) => { if (!data) { res.status(404).send({ message: `Cannot update Tutorial with id=${id}. Maybe Tutorial was not found!`, }); } else res.send({ message: "Tutorial was updated successfully." }); }) .catch((err) => { res.status(500).send({ message: "Error updating Tutorial with id=" + id, }); }); }; // 요청을 받은 ID에대한 DB삭제 exports.delete = (req, res) => { const id = req.params.id; Tutorial.findByIdAndRemove(id) .then((data) => { if (!data) { res.status(404).send({ message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!`, }); } else { res.send({ message: "Tutorial was deleted successfully!", }); } }) .catch((err) => { res.status(500).send({ message: "Could not delete Tutorial with id=" + id, }); }); }; // 모든 DB 삭제 exports.deleteAll = (req, res) => { Tutorial.deleteMany({}) .then((data) => { res.send({ message: `${data.deletedCount} Tutorials were deleted successfully!`, }); }) .catch((err) => { res.status(500).send({ message: err.message || "Some error occurred while removing all tutorials.", }); }); }; // 게시되어있는 모든 DB불러오기 exports.findAllPublished = (req, res) => { Tutorial.find({ published: true }) .then((data) => { res.send(data); }) .catch((err) => { res.status(500).send({ message: err.message || "Some error occurred while retrieving tutorials.", }); }); };
from http://sac4686.tistory.com/47 by ccl(A) rewrite - 2021-08-26 21:00:17