forked from Sharvin26/TodoApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 256e69f
Showing
9 changed files
with
2,286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"projects": { | ||
"default": "todoapp-655c1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.