Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CRIO_BYTES
ME_NODEJS_MONGODB
Commits
9953a97c
Commit
9953a97c
authored
4 years ago
by
Nabhan Abdulla P V
💬
Browse files
Options
Download
Email Patches
Plain Diff
Restructure files and API design
parent
173d4c45
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
197 additions
and
117 deletions
+197
-117
Backend/.env
Backend/.env
+2
-1
Backend/package.json
Backend/package.json
+1
-1
Backend/src/app.js
Backend/src/app.js
+9
-8
Backend/src/config/config.js
Backend/src/config/config.js
+16
-0
Backend/src/models/todo.js
Backend/src/models/todo.js
+0
-10
Backend/src/models/todo.model.js
Backend/src/models/todo.model.js
+25
-0
Backend/src/routes/v1/todo.route.js
Backend/src/routes/v1/todo.route.js
+144
-97
No files found.
Backend/.env
View file @
9953a97c
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
This diff is collapsed.
Click to expand it.
Backend/package.json
View file @
9953a97c
...
...
@@ -5,7 +5,7 @@
"main"
:
"app.js"
,
"scripts"
:
{
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
,
"start"
:
"node
app.js"
"start"
:
"node
mon src/
app.js"
},
"author"
:
"Aman Kumar"
,
"license"
:
"ISC"
,
...
...
This diff is collapsed.
Click to expand it.
Backend/src/app.js
View file @
9953a97c
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Backend/src/config/config.js
0 → 100644
View file @
9953a97c
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
,
},
}
};
This diff is collapsed.
Click to expand it.
Backend/src/models/todo.js
deleted
100644 → 0
View file @
173d4c45
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
This diff is collapsed.
Click to expand it.
Backend/src/models/todo.model.js
0 → 100644
View file @
9953a97c
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
);
This diff is collapsed.
Click to expand it.
Backend/src/routes/v1/todo.route.js
View file @
9953a97c
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:
300
1/todos
curl http://localhost:
8082/v
1/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:
300
1/todos/pending
curl http://localhost:
8082/v
1/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:
300
1/todos/endDate
*/
// FIXME - Descide if to keep
//
/* Get all TODOS: :Last x days( EG: 15days )
//
curl http://localhost:
8082/v
1/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:
300
1/todos/search?startDate=2020-11-04&
end
Date=2020-12-30
curl -X "GET" http://localhost:
8082/v
1/todos/search?startDate
Min
=2020-11-04&
start
Date
Max
=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
;
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment