Skip to content

Commit

Permalink
TODOs API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharvin26 committed Mar 30, 2020
0 parents commit 256e69f
Show file tree
Hide file tree
Showing 9 changed files with 2,286 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "todoapp-655c1"
}
}
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*

# Firebase cache
.firebase/

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
1 change: 1 addition & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
112 changes: 112 additions & 0 deletions functions/APIs/todos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const { db } = require('../util/admin');

exports.getAllTodos = (request, response) => {
db
.collection('todos')
.orderBy('createdAt', 'desc')
.get()
.then((data) => {
let todos = [];
data.forEach((doc) => {
todos.push({
todoId: doc.id,
title: doc.data().title,
body: doc.data().body,
createdAt: doc.data().createdAt,
});
});
return response.json(todos);
})
.catch((err) => {
console.error(err);
return response.status(500).json({ error: err.code});
});
};

exports.getOneTodo = (request, response) => {
db
.doc(`/todos/${request.params.todoId}`)
.get()
.then((doc) => {
if (!doc.exists) {
return response.status(404).json(
{
error: 'Todo not found'
});
}
TodoData = doc.data();
TodoData.todoId = doc.id;
return response.json(TodoData);
})
.catch((err) => {
console.error(err);
return response.status(500).json({ error: error.code });
});
};

exports.postOneTodo = (request, response) => {
if (request.body.body.trim() === '') {
return response.status(400).json({ body: 'Must not be empty' });
}

if(request.body.title.trim() === '') {
return response.status(400).json({ title: 'Must not be empty' });
}

const newTodoItem = {
title: request.body.title,
body: request.body.body,
createdAt: new Date().toISOString()
}
db
.collection('todos')
.add(newTodoItem)
.then((doc)=>{
const responseTodoItem = newTodoItem;
responseTodoItem.id = doc.id;
return response.json(responseTodoItem);
})
.catch((err) => {
response.status(500).json({ error: 'Something went wrong' });
console.error(err);
});
};

exports.deleteTodo = (request, response) => {
const document = db.doc(`/todos/${request.params.todoId}`);
document
.get()
.then((doc) => {
if (!doc.exists) {
return response.status(404).json({
error: 'Todo not found'
})}
return document.delete();
})
.then(() => {
response.json({ message: 'Delete successfull' });
})
.catch((err) => {
console.error(err);
return response.status(500).json({
error: err.code
});
});
};

exports.editTodo = ( request, response ) => {
if(request.body.todoId || request.body.createdAt){
response.status(403).json({message: 'Not allowed to edit'});
}
let document = db.collection('todos').doc(`${request.params.todoId}`);
document.update(request.body)
.then(()=> {
response.json({message: 'Updated successfully'});
})
.catch((err) => {
console.error(err);
return response.status(500).json({
error: err.code
});
});
};
18 changes: 18 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const functions = require('firebase-functions');
const app = require('express')();

const {
getAllTodos,
getOneTodo,
postOneTodo,
deleteTodo,
editTodo
} = require('./APIs/todos')

app.get('/todos', getAllTodos);
app.get('/todo/:todoId', getOneTodo);
app.post('/todo', postOneTodo);
app.delete('/todo/:todoId', deleteTodo);
app.put('/todo/:todoId', editTodo);

exports.api = functions.https.onRequest(app);
Loading

0 comments on commit 256e69f

Please sign in to comment.