|
| 1 | +const Tutorial = require("../models/tutorial.model.js"); |
| 2 | + |
| 3 | +// Create and Save a new Tutorial |
| 4 | +exports.create = (req, res) => { |
| 5 | + // Validate request |
| 6 | + if (!req.body) { |
| 7 | + res.status(400).send({ |
| 8 | + message: "Content can not be empty!" |
| 9 | + }); |
| 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 || false |
| 17 | + }); |
| 18 | + |
| 19 | + // Save Tutorial in the database |
| 20 | + Tutorial.create(tutorial, (err, data) => { |
| 21 | + if (err) |
| 22 | + res.status(500).send({ |
| 23 | + message: |
| 24 | + err.message || "Some error occurred while creating the Tutorial." |
| 25 | + }); |
| 26 | + else res.send(data); |
| 27 | + }); |
| 28 | +}; |
| 29 | + |
| 30 | +// Retrieve all Tutorials from the database (with condition). |
| 31 | +exports.findAll = (req, res) => { |
| 32 | + const title = req.query.title; |
| 33 | + |
| 34 | + Tutorial.getAll(title, (err, data) => { |
| 35 | + if (err) |
| 36 | + res.status(500).send({ |
| 37 | + message: |
| 38 | + err.message || "Some error occurred while retrieving tutorials." |
| 39 | + }); |
| 40 | + else res.send(data); |
| 41 | + }); |
| 42 | +}; |
| 43 | + |
| 44 | +// Find a single Tutorial by Id |
| 45 | +exports.findOne = (req, res) => { |
| 46 | + Tutorial.findById(req.params.id, (err, data) => { |
| 47 | + if (err) { |
| 48 | + if (err.kind === "not_found") { |
| 49 | + res.status(404).send({ |
| 50 | + message: `Not found Tutorial with id ${req.params.id}.` |
| 51 | + }); |
| 52 | + } else { |
| 53 | + res.status(500).send({ |
| 54 | + message: "Error retrieving Tutorial with id " + req.params.id |
| 55 | + }); |
| 56 | + } |
| 57 | + } else res.send(data); |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +// find all published Tutorials |
| 62 | +exports.findAllPublished = (req, res) => { |
| 63 | + Tutorial.getAllPublished((err, data) => { |
| 64 | + if (err) |
| 65 | + res.status(500).send({ |
| 66 | + message: |
| 67 | + err.message || "Some error occurred while retrieving tutorials." |
| 68 | + }); |
| 69 | + else res.send(data); |
| 70 | + }); |
| 71 | +}; |
| 72 | + |
| 73 | +// Update a Tutorial identified by the id in the request |
| 74 | +exports.update = (req, res) => { |
| 75 | + // Validate Request |
| 76 | + if (!req.body) { |
| 77 | + res.status(400).send({ |
| 78 | + message: "Content can not be empty!" |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + console.log(req.body); |
| 83 | + |
| 84 | + Tutorial.updateById( |
| 85 | + req.params.id, |
| 86 | + new Tutorial(req.body), |
| 87 | + (err, data) => { |
| 88 | + if (err) { |
| 89 | + if (err.kind === "not_found") { |
| 90 | + res.status(404).send({ |
| 91 | + message: `Not found Tutorial with id ${req.params.id}.` |
| 92 | + }); |
| 93 | + } else { |
| 94 | + res.status(500).send({ |
| 95 | + message: "Error updating Tutorial with id " + req.params.id |
| 96 | + }); |
| 97 | + } |
| 98 | + } else res.send(data); |
| 99 | + } |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +// Delete a Tutorial with the specified id in the request |
| 104 | +exports.delete = (req, res) => { |
| 105 | + Tutorial.remove(req.params.id, (err, data) => { |
| 106 | + if (err) { |
| 107 | + if (err.kind === "not_found") { |
| 108 | + res.status(404).send({ |
| 109 | + message: `Not found Tutorial with id ${req.params.id}.` |
| 110 | + }); |
| 111 | + } else { |
| 112 | + res.status(500).send({ |
| 113 | + message: "Could not delete Tutorial with id " + req.params.id |
| 114 | + }); |
| 115 | + } |
| 116 | + } else res.send({ message: `Tutorial was deleted successfully!` }); |
| 117 | + }); |
| 118 | +}; |
| 119 | + |
| 120 | +// Delete all Tutorials from the database. |
| 121 | +exports.deleteAll = (req, res) => { |
| 122 | + Tutorial.removeAll((err, data) => { |
| 123 | + if (err) |
| 124 | + res.status(500).send({ |
| 125 | + message: |
| 126 | + err.message || "Some error occurred while removing all tutorials." |
| 127 | + }); |
| 128 | + else res.send({ message: `All Tutorials were deleted successfully!` }); |
| 129 | + }); |
| 130 | +}; |
0 commit comments