Skip to content

Commit 7ae4187

Browse files
committed
done with backend api
0 parents  commit 7ae4187

File tree

7 files changed

+1680
-0
lines changed

7 files changed

+1680
-0
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DB_URL=<use mongodb atlas url>
2+
LOCAL_DB=<use local mongodb url>

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/.env
3+
/node_server.js

app.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//imports
2+
require('dotenv').config();
3+
const express = require('express');
4+
const app = express();
5+
const mongoose = require('mongoose');
6+
const todoController = require('./controllers/todoController');
7+
const PORT = process.env.PORT || 3000;
8+
9+
//routes api
10+
app.use(express.json());
11+
app.get('/todos', todoController.getAllTodos);
12+
app.post('/todos', todoController.addTodo);
13+
app.patch('/todos/:todoId', todoController.updateTodoById);
14+
app.delete('/todos/:todoId', todoController.deleteTodoById);
15+
app.get('/todos/:todoId', todoController.getTodoById);
16+
17+
//listener
18+
app.listen(PORT, function(){
19+
console.log('Server has started to run');
20+
mongoose.connect(process.env.DB_URL)
21+
.then(function(){
22+
console.log('DB is connected');
23+
})
24+
.catch(function(error){
25+
console.log('DB is not connected: ', error.message);
26+
})
27+
});

controllers/todoController.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const Todo = require('../models/Todo');
2+
3+
async function addTodo(req, res){
4+
try {
5+
const newTodo = await Todo.create(req.body);
6+
res.status(200).json(newTodo);
7+
} catch (error) {
8+
console.log('cant add data: ', error.message);
9+
res.status(401).json({message: 'Cant add data'});
10+
}
11+
}
12+
13+
async function deleteTodoById(req, res){
14+
const todoId = req.params.todoId;
15+
try {
16+
await Todo.findByIdAndDelete(todoId);
17+
res.status(200).json({message: 'Todo has been deleted'});
18+
} catch (error) {
19+
console.log('cant delete: ', error.message);
20+
res.status(401).json({error: error.message});
21+
}
22+
}
23+
24+
async function updateTodoById(req, res){
25+
const todoId = req.params.todoId;
26+
const body = req.body;
27+
try {
28+
await Todo.findByIdAndUpdate(todoId, body);
29+
res.status(200).json({message: 'Todo is updated'});
30+
} catch (error) {
31+
console.log('Cant update: ', error.message);
32+
res.status(401).json({error: error.message});
33+
}
34+
}
35+
36+
async function getTodoById(req, res){
37+
try {
38+
const todo = await Todo.findById(req.params.todoId);
39+
res.status(200).json(todo);
40+
} catch (error) {
41+
console.log('cant get todo: ', error.message);
42+
res.status(401).json({error: error.message});
43+
}
44+
}
45+
46+
async function getAllTodos(req, res){
47+
try {
48+
const todos = await Todo.find();
49+
res.status(200).json(todos);
50+
} catch (error) {
51+
console.log('cant get data: ', error.message);
52+
res.status(401).json({error: error.message});
53+
}
54+
}
55+
module.exports = {
56+
getAllTodos,
57+
addTodo,
58+
updateTodoById,
59+
deleteTodoById,
60+
getTodoById
61+
}

models/Todo.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const mongoose = require('mongoose');
2+
3+
const todoSchema = new mongoose.Schema({
4+
title: {
5+
type: String,
6+
required: true
7+
},
8+
description: {
9+
type: String,
10+
required: true
11+
},
12+
deadline: {
13+
type: Date,
14+
required: true
15+
},
16+
isCompleted: {
17+
type: Boolean,
18+
default: false,
19+
required: true
20+
}
21+
});
22+
23+
const Todo = mongoose.model('todo', todoSchema);
24+
module.exports = Todo;

0 commit comments

Comments
 (0)