Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.1-todo backend assignment completed #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions week-6/6.1-todo/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
9 changes: 7 additions & 2 deletions week-6/6.1-todo/backend/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const cors = require('cors');
const { getAllTodo, createTodo, updateTodo, deleteTodoById } = require('./routes/todo'); // importing callback functions for routes
const { getAllTodo, createTodo, updateTodo, deleteTodoById, deleteTodo, searchTodo } = require('./routes/todo'); // importing callback functions for routes
const app = express();
const PORT = 3001;

Expand All @@ -20,9 +20,14 @@ app.put('/todos/:id', updateTodo);
// Delete a todo
app.delete('/todos/:id', deleteTodoById);

// Delete all todos
app.delete("/todos" , deleteTodo);


// TODO: can you implement search todo route ???

app.get("/todo" , searchTodo);

app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
});
122 changes: 116 additions & 6 deletions week-6/6.1-todo/backend/routes/todo.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,131 @@
let todos = []; // in memory space
let count = 0;
let deletedTodos = [];

export async function getAllTodo (req, res, next){
async function getAllTodo (req, res, next){ // to get all your todos history
// write here
res.json({
todos,
deletedTodos
});

}

export async function createTodo (req, res, next){
async function createTodo (req, res, next){
// write here
const todo = req.body.todo;
count++;
if(todo){
todos.push({
id : count,
todo : todo
});
res.json({
message : "todo create successfully...."
})
}else {
res.status(400).json({
message : "unable to get the todo..."
});
}

}

export async function updateTodo (req, res, next){
async function updateTodo (req, res, next){
// write here
const id = parseInt(req.params.id);
const updatedTodo = req.body.todo;
if (id && updatedTodo){
let foundTodo = null;
for (let i=0; i<todos.length; i++){
if (todos[i].id === id){
foundTodo = todos[i];
todos[i].todo = updatedTodo;
}
}
if (foundTodo){
res.json({
message : "updated successfully...."
})
}else {
res.json({
message : "can't get your previous data...."
})
}
}else {
res.json({
message : "Something went wrong while receiving your data..."
})
}

}

export async function deleteTodo (req, res, next){
async function deleteTodo (req, res, next){ // to delete all your todos history
// write here
todos.splice(0,todos.length);
deletedTodos.splice(0 , deletedTodos.length);
res.json({
message : "you have deleted all the todos..."
})
}

export async function deleteTodoById (req, res, next){
async function deleteTodoById (req, res, next){
// write here
}
const id = parseInt(req.params.id);
if (id){
for (let i=0; i<todos.length; i++){
if(todos[i].id === id){
const arr = todos.splice(i,1);
deletedTodos.push(arr[0]);
}
}
res.json({
message : "delete successfully..."
});
}else {
res.json({
message : "unable to get the id..."
});
}

}




async function searchTodo (req , res , next){
const todo = req.body.todo;
if (todo){
let getTodo = null;
for (let i=0; i<todos.length; i++){
if(todos[i].todo === todo){
getTodo = todos[i];
break;
}
}
if(getTodo){
res.json({
searchedTodo : getTodo
})
}else {
res.json ({
message : "can't get your todo..."
});
}
}else {
res.json ({
message : "Something went wrong while receiving your data..."
});
}
}



module.exports = {
deleteTodoById ,
deleteTodo ,
updateTodo ,
createTodo ,
getAllTodo ,
searchTodo
};