Section 30 – Lessons 391 & 392 – Complete 2020 Web Development Bootcamp

Lesson 391 – PATCH a Specific Article

If you want to change just one part of the content of an item in a database without changing the entire item, you can use the .patch() method.

In the code below, the .patch() method will change whatever content is input by the client.

.patch(function(req, res) {
	Article.updateOne(
		{title: req.params.articleTitle},
		{$set: req.body},
		function(err) {
			if(!err) {
				res.send("Successfully updated article.");
			} else {
				res.send(err);
			}
		}
		);
	
});

Lesson 392 – DELETE a Specific Article

The challenge in this lesson is to add a route to allow a client to delete a specific article in the database.

I was able to complete this challenge using this code –

.delete(function(req, res) {
	Article.deleteOne(
		{title: req.params.articleTitle},
		function(err) {
		  if (!err) {
			  res.send("Successfully deleted article.");
			  } else {
				res.send(err);
			}
		}
		);
	
});

The completed app.js code for the RESTful API is –

const express = require("express");
const bodyParser = require("body-parser");

const mongoose = require("mongoose");
const ejs = require("ejs");

const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

mongoose.connect("mongodb://localhost:27017/wikiDB", {useNewUrlParser: true, useUnifiedTopology: true});

const articleSchema = ({
  title: String,
  content: String
});

const Article = mongoose.model("Article", articleSchema);

///Requests targeting all articles///

app.route("/articles")

.get(function (req, res) {
  Article.find(function(err, foundArticles){
  	if (!err) {
  		res.send(foundArticles);
  	} else {
  		res.send(err);
  	}
  	
  });
})

.post(function(req, res){
	

	const newArticle = new Article({
		title: req.body.title,
		content: req.body.content
	});
	newArticle.save(function(err){
		if(!err) {
			res.send("Successfully added a new article!");
		} else {
			res.send(err);
		}
	});
})

.delete(function(req, res) {
	Article.deleteMany(function(err){
		if (!err) {
			res.send("Successfully deleted all articles.");
		} else {
			res.send(err);
		}
	});
});

///Requests targeting a specific artticle///

app.route("/articles/:articleTitle")

.get(function (req, res) {

	Article.findOne({title: req.params.articleTitle}, function(err, foundArticle){
		if (foundArticle) {
			res.send(foundArticle);
		} else {
			res.send("No article matching that title was found.");
		}
	});

})

.put(function(req, res) {
	Article.replaceOne(
	{title: req.params.articleTitle},
	{title: req.body.title, content: req.body.content},
	{overwrite: true},

	function(err) {
		if (!err) {
			res.send("Successfully updated article.");
		} 
		}
		);
	
})

.patch(function(req, res) {
	Article.updateOne(
		{title: req.params.articleTitle},
		{$set: req.body},
		function(err) {
			if(!err) {
				res.send("Successfully updated article.");
			} else {
				res.send(err);
			}
		}
		);
	
})

.delete(function(req, res) {
	Article.deleteOne(
		{title: req.params.articleTitle},
		function(err) {
			if (!err) {
				res.send("Successfully deleted article.");
			} else {
				res.send(err);
			}
		}
		);
	
});

app.listen(3000, function() {
  console.log("Server has started successfully.");
});