Commit 9953a97c authored by Nabhan Abdulla P V's avatar Nabhan Abdulla P V 💬
Browse files

Restructure files and API design

parent 173d4c45
PORT=8082
\ No newline at end of file
PORT=8082
MONGODB_URL=mongodb://127.0.0.1:27017/todoapp
\ No newline at end of file
......@@ -5,7 +5,7 @@
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
"start": "nodemon src/app.js"
},
"author": "Aman Kumar",
"license": "ISC",
......
......@@ -5,9 +5,16 @@ const routes = require('./routes/v1');
const mongoose = require('mongoose');
const captureDate = require('./middleware/middleware');
const cors = require('cors');
const dotenv = require("dotenv")
const config = require("./config/config")
mongoose.connect("mongodb://localhost:27017/TODOAPP",{useCreateIndex:true,useUnifiedTopology:true, useNewUrlParser:true});
mongoose.connect(config.mongoose.url, config.mongoose.options).then(() => {
console.log("Connected to MongoDB");
// Start the Node server
app.listen(config.port, function () {
console.log(`App is running on port ${process.env.PORT}`);
});
});
const app = express();
......@@ -23,10 +30,4 @@ app.use(bodyParser.urlencoded({
app.use('/v1', routes);
// Load config from .env file to `process.env`
dotenv.config()
// Start the Node server
app.listen(process.env.PORT, function(){
console.log(`App is running on port ${process.env.PORT}`);
});
\ No newline at end of file
const dotenv = require("dotenv/types");
const path = require("path");
dotenv.config({ path: path.join(__dirname, "../.env") });
module.exports = {
port: process.env.PORT,
mongoose: {
url: process.env.MONGODB_URL,
options: {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
},
}
};
const mongoose = require('mongoose');
const todoSchema = new mongoose.Schema({
id:Number,
name: String,
startDate: {type : Date},
endDate: {type : Date},
dateCreated: {type : Date},
pending: Boolean
});
module.exports = mongoose.model("Todos",todoSchema);
\ No newline at end of file
const mongoose = require("mongoose");
// FIXME - Add React ME like comments
const todoSchema = new mongoose.Schema(
{
id: Number,
name: {
type: String,
required: true,
},
startDate: {
type: Date,
required: true,
},
endDate: {
type: Date,
required: true,
},
pending: {
type: Boolean,
},
}
);
module.exports = mongoose.model("Todos", todoSchema);
const express = require('express');
const express = require("express");
const router = express.Router();
const Todos = require('../../models/todo');
const mongoose = require('mongoose');
const Todos = require("../../models/todo.model");
const mongoose = require("mongoose");
/* Get all TODOS:
curl http://localhost:3001/todos
curl http://localhost:8082/v1/todos
*/
router.get("/", (req,res) => {
Todos.find({}, (err,allTodos) => {
if(err){
console.log(err);
}
else{
console.log(allTodos);
res.json(allTodos);
// res.send(allTodos);
router.get("/", (req, res) => {
// let nextday = new Date(req.query.startDateMax);
// nextday.setTime(nextday.getTime());
// let prevday = new Date(req.query.startDateMin);
// prevday.setTime(prevday.getTime());
if (req.params.startDate) {
Todos.find(
{
startDate: {
$lte: nextday,
$gte: prevday,
},
},
(err, allTodos) => {
if (err) {
console.log(err);
} else {
console.log(allTodos);
res.send(allTodos);
}
}
);
} else {
Todos.find({}, (err, allTodos) => {
if (err) {
console.log(err);
res.status(500).send();
} else {
console.log(allTodos);
res.send(allTodos);
}
});
}
});
// FIXME - Deside if to keep
/* Get all TODOS: Which are pending
curl http://localhost:3001/todos/pending
curl http://localhost:8082/v1/todos/pending
*/
router.get("/todos/pending", (req,res) => {
Todos.find({pending:true}, (err,allTodos) => {
......@@ -31,16 +54,16 @@ router.get("/todos/pending", (req,res) => {
}
else{
console.log(allTodos);
res.json(allTodos);
res.send(allTodos);
// res.send(allTodos);
}
});
});
/* Get all TODOS: :Last x days( EG: 15days )
curl http://localhost:3001/todos/endDate
*/
// FIXME - Descide if to keep
// /* Get all TODOS: :Last x days( EG: 15days )
// curl http://localhost:8082/v1/todos/endDate
// */
router.get("/todos/endDate", (req,res) => {
var lastday= new Date();
lastday.setTime(lastday.getTime() - (15*24*60*60*1000));
......@@ -57,104 +80,128 @@ router.get("/todos/endDate", (req,res) => {
}
else{
console.log(allTodos);
res.json(allTodos);
res.send(allTodos);
// res.send(allTodos);
}
});
});
/* Get all Todos : Between startDate and endDate
curl -X "GET" http://localhost:3001/todos/search?startDate=2020-11-04&endDate=2020-12-30
curl -X "GET" http://localhost:8082/v1/todos/search?startDateMin=2020-11-04&startDateMax=2020-12-30
*/
router.get("/todos/search",(req,res)=>{
var nextday= new Date(req.query.endDate);
nextday.setTime(nextday.getTime());
var prevday= new Date(req.query.startDate);
prevday.setTime(prevday.getTime() );
Todos.find({
startDate:{
$lte:nextday, $gte:prevday
}
router.get("/search", (req, res) => {
var nextday = new Date(req.query.startDateMax);
nextday.setTime(nextday.getTime());
var prevday = new Date(req.query.startDateMin);
prevday.setTime(prevday.getTime());
Todos.find(
{
startDate: {
$lte: nextday,
$gte: prevday,
},
},
(err,allTodos) => {
if(err){
console.log(err);
}
else{
console.log(allTodos);
res.json(allTodos);
}
});
(err, allTodos) => {
if (err) {
console.log(err);
} else {
console.log(allTodos);
res.send(allTodos);
}
}
);
});
/* Add a TODO to the list
curl -X POST -d 'name=Task-4 &startDate=2020-11-11&endDate=2020-11-21' http://localhost:3001/todos
curl -X POST http://localhost:8082/v1/todos \
-d '{"name": "Learn Nodejs by doing","startDate": "2021-01-07","endDate": "2021-01-09"}' \
-H 'Content-Type: application/json'
Nb: You'll need to change the "id" value to that of one of your todo items
*/
router.post("/todos", (req,res) => {
// console.log(req);
var epochTime = new Date();
var uniqueId = epochTime.valueOf();
let newTodo = {
id:uniqueId,
name: req.body.name,
startDate:req.body.startDate,
endDate:req.body.endDate,
dateCreated:new Date(),
pending:true
router.post("/", (req, res) => {
console.log(req.body);
if (!req.is("application/json")) {
res.status(415);
res.send({ error: "Received non-JSON data" });
}
let epochTime = new Date();
let uniqueId = epochTime.valueOf();
let newTodo = {
id: uniqueId,
name: req.body.name,
startDate: req.body.startDate,
endDate: req.body.endDate,
dateCreated: new Date(),
pending: true,
};
// console.log(newTodo);
Todos.create(newTodo, (err, newlyCreated) => {
if (err) {
// console.log(err);
res.status(500).send();
} else {
res.status(201).send(newlyCreated);
}
console.log(newTodo);
Todos.create(newTodo, (err,newlyCreated)=> {
if (err) {
console.log(err);
} else {
//console.log(newlyCreated);
res.send(newlyCreated);
}
});
});
});
/* Delete a TODO to the list
curl -X "DELETE" -d 'name= ' http://localhost:3001/todos
/* Update an existing TODO
curl -X PUT http://localhost:8082/v1/todos \
-d '{"id": <id-value>, "name": "Play tennis","startDate": "2021-01-07","endDate": "2021-01-09"}' \
-H 'Content-Type: application/json'
Nb: You'll need to change the "id" value to that of one of your todo items
*/
router.delete("/todos", (req,res) => {
console.log(req.body);
let deleteTodo = req.body._id;
console.log("deleteTodo", deleteTodo);
Todos.deleteOne({_id:deleteTodo}, (err,result)=>{
if(err){
console.log(err);
}
else{
console.log("Backend",result);
res.json(result);
}
});
router.put("/", (req, res) => {
// console.log(req.body);
if (!req.is("application/json")) {
res.status(415).send({ error: "Received non-JSON data" });
}
let condition = { id: req.body.id };
Todos.findOne(condition, function (err, doc) {
if (err) {
console.log(err);
res.status(500).send();
} else if (doc == null) {
res.status(400).send({ error: "Resource not found" });
} else {
doc.name = req.body.name;
doc.startDate = req.body.startDate;
doc.endDate = req.body.endDate;
doc.save();
res.status(204).send();
}
});
});
/* Delete a TODO from the list
curl -X "DELETE" http://localhost:8082/v1/todos/<id-value>
/* Update an existing TODO
curl -X PUT -d 'name=Task-1 &startDate=2020-11-03&endDate=2020-11-20&dateCreated=2020-11-01&pending=false' http://localhost:3000/todos
Nb: You'll need to change "<id-value>" to the "id" value of one of your todo items
*/
router.put("/todos", (req,res) => {
console.log(req.body.data);
var condition = { id: req.body.data.id};
Todos.findOne(condition, function (err, doc){
if(err) {
console.log(err);
} else {
doc.name = req.body.data.name;
doc.startDate = req.body.data.startDate;
doc.endDate = req.body.data.endDate;
doc.save();
res.send('Updated');
}
});
router.delete("/:id", (req, res) => {
let deleteTodo = req.params.id;
Todos.deleteOne({ id: deleteTodo }, (err, result) => {
if (err) {
console.log(err);
res.status(500).send();
} else {
console.log(result);
res.status(200).send({ deletedCount: result.deletedCount });
}
});
});
module.exports = router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment