Skip to content

Commit c49d2fe

Browse files
committed
complete code 1.0
1 parent 3ca90d0 commit c49d2fe

File tree

7 files changed

+43
-42
lines changed

7 files changed

+43
-42
lines changed

.env

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Mongo DB
22
# Local development
3-
MONGODB_URI_DEV='mongodb+srv://admin:PASSWORD@eapi-g5m4h.mongodb.net/eapi'
3+
MONGODB_URI_DEV='YOUR PROD URL LIKNK FOR MONGODB'
44

55
# Local development
6-
MONGODB_URI_PROD='mongodb+srv://admin:PASSWORD@eapi-g5m4h.mongodb.net/eapi'
6+
MONGODB_URI_PROD='YOUR PROD URL LIKNK FOR MONGODB'
77

88
# Port
9-
PORT=3000
9+
PORT=5000
1010

1111
# Debug
1212
LOG_LEVEL='debug'

src/config/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const config = {
1313
port: parseInt(process.env.PORT, 10),
1414
database_URL: process.env.MONGODB_URI_DEV,
1515
api: {
16-
prefix: '/api'
16+
prefix: '/api-dev'
1717
}
1818
},
1919
prod: {

src/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ const onError = error => {
3737
};
3838

3939
const onListening = () => {
40-
const bind = typeof port === 'string' ? 'pipe ' + port : 'port ' + port;
40+
const bind = typeof port === 'string' ? 'pipe ' + port : 'port: ' + port;
4141
debug('Listening on ' + bind);
4242
console.log(`
4343
************************************************
44-
🚀 Server listening on port: ${bind} 🚀
44+
🚀 Server listening on ${bind} 🚀
4545
************************************************
4646
`);
4747
};
Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
const todoService = require('../services/todo');
22

3-
exports.getTasks = async (req, res, next) => {
4-
try {
5-
const tasks = await todoService.getTasks();
6-
return res.status(201).json({
7-
message: 'Fetched posts successfully.',
8-
tasks: tasks,
9-
status: 200
10-
});
11-
} catch (err) {
12-
if (!err.statusCode) {
13-
err.statusCode = 500;
14-
}
15-
return err;
16-
}
17-
};
18-
19-
exports.getTasksById = async (req, res, next) => {
20-
const taskId = req.params.id;
21-
const task = await todoService.getTasksById(taskId);
22-
try {
23-
if (!task) {
24-
throw new Error('Task not found');
3+
module.exports = {
4+
getTasks: async (req, res, next) => {
5+
try {
6+
const tasks = await todoService.getTasks();
7+
return res.status(201).json({
8+
message: 'Fetched posts successfully.',
9+
tasks: tasks,
10+
status: 200
11+
});
12+
} catch (err) {
13+
if (!err.statusCode) {
14+
err.statusCode = 500;
15+
}
16+
return err;
2517
}
18+
},
2619

27-
return res.status(201).json({
28-
message: 'Task fetched',
29-
task: task,
30-
status: 200
31-
});
32-
} catch (err) {
33-
if (!err.statusCode) {
34-
err.statusCode = 500;
20+
createTask: async (req, res, next) => {
21+
try {
22+
const task = todoService.createTask(req.body.title, req.body.status);
23+
await task.save();
24+
return res.status(201).json({
25+
message: 'Fetched posts successfully.',
26+
status: 200
27+
});
28+
} catch (err) {
29+
if (!err.statusCode) {
30+
err.statusCode = 500;
31+
}
32+
return err;
3533
}
36-
return err;
3734
}
3835
};

src/todo/models/task.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Database models
2+
Database model
33
*/
44
const mongoose = require('mongoose');
55

@@ -8,7 +8,7 @@ try {
88
} catch (error) {
99
const taskSchema = mongoose.Schema({
1010
title: { type: String, required: true },
11-
content: { type: String, required: true }
11+
status: { type: String, required: true }
1212
});
1313
module.exports = mongoose.model('Task', taskSchema);
1414
}

src/todo/routes/todo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ const router = express.Router();
33
const todoController = require('../controllers/todoController');
44
//End points
55
router.get('/tasks', todoController.getTasks);
6-
router.get('/tasks/:id', todoController.getTasksById);
6+
router.post('/tasks', todoController.createTask);
77

88
module.exports = router;

src/todo/services/todo.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ class TodoService {
55
return Task.find();
66
}
77

8-
static getTasksById(id) {
9-
return Task.findById(id);
8+
static createTask(title, status) {
9+
const task = new Task({
10+
title: title,
11+
status: status
12+
});
13+
return task;
1014
}
1115
}
1216

0 commit comments

Comments
 (0)