Skip to content

Commit

Permalink
Added find and delete artcle from database
Browse files Browse the repository at this point in the history
  • Loading branch information
Kellsonphilips committed Aug 16, 2022
0 parents commit f657431
Show file tree
Hide file tree
Showing 6 changed files with 2,038 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
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*
148 changes: 148 additions & 0 deletions app.js
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")
});
11 changes: 11 additions & 0 deletions db/articleModel.js
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);
18 changes: 18 additions & 0 deletions db/dbConnect.js
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;
Loading

0 comments on commit f657431

Please sign in to comment.