Advertisement
Guest User

Untitled

a guest
Nov 19th, 2020
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const db = require("../models");
  2. const Tutorial = db.tutorials;
  3.  
  4. // Create and Save a new Tutorial
  5. exports.create = (req, res) => {
  6.   // Validate request
  7.   if (!req.body.title) {
  8.     res.status(400).send({ message: "Content can not be empty!" });
  9.     return;
  10.   }
  11.  
  12.   // Create a Tutorial
  13.   const tutorial = new Tutorial({
  14.     title: req.body.title,
  15.     description: req.body.description,
  16.     published: req.body.published ? req.body.published : false
  17.   });
  18.  
  19.   // Save Tutorial in the database
  20.   tutorial
  21.     .save(tutorial)
  22.     .then(data => {
  23.       res.send(data);
  24.     })
  25.     .catch(err => {
  26.       res.status(500).send({
  27.         message:
  28.           err.message || "Some error occurred while creating the Tutorial."
  29.       });
  30.     });
  31. };
  32.  
  33. // Retrieve all Tutorials from the database.
  34. exports.findAll = (req, res) => {
  35.   const title = req.query.title;
  36.   var condition = title ? { title: { $regex: new RegExp(title), $options: "i" } } : {};
  37.  
  38.   Tutorial.find(condition)
  39.     .then(data => {
  40.       res.send(data);
  41.     })
  42.     .catch(err => {
  43.       res.status(500).send({
  44.         message:
  45.           err.message || "Some error occurred while retrieving tutorials."
  46.       });
  47.     });
  48. };
  49.  
  50. // Find a single Tutorial with an id
  51. exports.findOne = (req, res) => {
  52.   const id = req.params.id;
  53.  
  54.   Tutorial.findById(id)
  55.     .then(data => {
  56.       if (!data)
  57.         res.status(404).send({ message: "Not found Tutorial with id " + id });
  58.       else res.send(data);
  59.     })
  60.     .catch(err => {
  61.       res
  62.         .status(500)
  63.         .send({ message: "Error retrieving Tutorial with id=" + id });
  64.     });
  65. };
  66.  
  67. // Update a Tutorial by the id in the request
  68. exports.update = (req, res) => {
  69.   if (!req.body) {
  70.     return res.status(400).send({
  71.       message: "Data to update can not be empty!"
  72.     });
  73.   }
  74.  
  75.   const id = req.params.id;
  76.  
  77.   Tutorial.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
  78.     .then(data => {
  79.       if (!data) {
  80.         res.status(404).send({
  81.           message: `Cannot update Tutorial with id=${id}. Maybe Tutorial was not found!`
  82.         });
  83.       } else res.send({ message: "Tutorial was updated successfully." });
  84.     })
  85.     .catch(err => {
  86.       res.status(500).send({
  87.         message: "Error updating Tutorial with id=" + id
  88.       });
  89.     });
  90. };
  91.  
  92. // Delete a Tutorial with the specified id in the request
  93. exports.delete = (req, res) => {
  94.   const id = req.params.id;
  95.  
  96.   Tutorial.findByIdAndRemove(id, { useFindAndModify: false })
  97.     .then(data => {
  98.       if (!data) {
  99.         res.status(404).send({
  100.           message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!`
  101.         });
  102.       } else {
  103.         res.send({
  104.           message: "Tutorial was deleted successfully!"
  105.         });
  106.       }
  107.     })
  108.     .catch(err => {
  109.       res.status(500).send({
  110.         message: "Could not delete Tutorial with id=" + id
  111.       });
  112.     });
  113. };
  114.  
  115. // Delete all Tutorials from the database.
  116. exports.deleteAll = (req, res) => {
  117.   Tutorial.deleteMany({})
  118.     .then(data => {
  119.       res.send({
  120.         message: `${data.deletedCount} Tutorials were deleted successfully!`
  121.       });
  122.     })
  123.     .catch(err => {
  124.       res.status(500).send({
  125.         message:
  126.           err.message || "Some error occurred while removing all tutorials."
  127.       });
  128.     });
  129. };
  130.  
  131. // Find all published Tutorials
  132. exports.findAllPublished = (req, res) => {
  133.   Tutorial.find({ published: true })
  134.     .then(data => {
  135.       res.send(data);
  136.     })
  137.     .catch(err => {
  138.       res.status(500).send({
  139.         message:
  140.           err.message || "Some error occurred while retrieving tutorials."
  141.       });
  142.     });
  143. };
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement