-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added find and delete artcle from database
- Loading branch information
0 parents
commit f657431
Showing
6 changed files
with
2,038 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
const express = require("express"); | ||
const bodyParser = require("body-parser"); | ||
const mongoose = require("mongoose") | ||
const ejs = require("ejs"); | ||
const _ = require("lodash"); | ||
const dbConnect = require("./db/dbConnect"); | ||
const Article = require("./db/articleModel"); | ||
|
||
|
||
const app = express(); | ||
|
||
|
||
app.use(bodyParser.urlencoded({extended: true})); | ||
app.set("view engine", "ejs"); | ||
app.use(express.static("public")); | ||
|
||
dbConnect(); | ||
Article(); | ||
|
||
|
||
|
||
|
||
// /////////////// Creating a Schema for our date ////////////////// | ||
|
||
// const articlesSchema = { | ||
// title: String, | ||
// content: String | ||
// } | ||
|
||
// //////////////////// Creating a mongoose model for our data ///////////// | ||
// const Article = new mongoose.model("Article", articlesSchema); | ||
|
||
|
||
|
||
|
||
/////////////////// Request targetting 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 the articles 😬."); | ||
} else { | ||
res.send(err); | ||
} | ||
}); | ||
}); | ||
|
||
|
||
//////////////////// Request targetting A specific article ////////////// | ||
|
||
app.route("/articles/:articlesTitle") | ||
|
||
.get(function(req, res) { | ||
|
||
Article.findOne( | ||
{title: req.params.articlesTitle}, | ||
function (err, foundArticles) { | ||
if (!err) { | ||
res.send(foundArticles); | ||
} else { | ||
res.send(err); | ||
} | ||
} | ||
); | ||
|
||
}) | ||
|
||
.put(function(req, res) { | ||
|
||
Article.findOneAndUpdate( | ||
{title: req.params.articlesTitle}, | ||
{title: req.body.title, content: req.body.content}, | ||
{overwrite: true}, | ||
function(err) { | ||
if (!err) { | ||
res.send("Succesfully updated article."); | ||
} else { | ||
res.send("Article could not be Updated") | ||
} | ||
} | ||
); | ||
}) | ||
|
||
.patch(function(req, res) { | ||
|
||
Article.findOneAndUpdate( | ||
{title: req.params.articlesTitle}, | ||
{$set: req.body}, | ||
function(err) { | ||
if(!err) { | ||
res.send("Successfully updated article"); | ||
} else { | ||
res.send("Article could not be Updated"); | ||
} | ||
} | ||
); | ||
}) | ||
|
||
.delete(function(req, res) { | ||
|
||
Article.findOneAndDelete( | ||
{title: req.params.articlesTitle}, | ||
function(err) { | ||
if (!err) { | ||
res.send("Successfully deleted an article.") | ||
} else { | ||
res.send(err); | ||
} | ||
} | ||
); | ||
}); | ||
|
||
|
||
|
||
|
||
//////////////// Our server ///////////////////// | ||
|
||
app.listen(3000, function(req, res) { | ||
console.log("Server is started on port: 3000") | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const articlesSchema = new mongoose.Schema({ | ||
title: String, | ||
content: String, | ||
}); | ||
|
||
|
||
|
||
module.exports = | ||
mongoose.model.Articles || mongoose.model("Articles", articlesSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require("dotenv").config(); | ||
const mongoose = require("mongoose"); | ||
|
||
function dbConnect() { | ||
mongoose | ||
.connect(process.env.DB_URL, { | ||
useNewUrlParser: true, | ||
}) | ||
.then(() => { | ||
console.log("Connection to database is successful"); | ||
}) | ||
.catch((err) => { | ||
console.log("Oh uh, couldn't connect to the database"); | ||
console.log(err); | ||
}); | ||
} | ||
|
||
module.exports = dbConnect; |
Oops, something went wrong.