Skip to content

Commit

Permalink
task list updating when documents are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorFlanigan committed Dec 2, 2021
1 parent 7c760cc commit 7200407
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 261 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,6 @@ fabric.properties

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)

.vscode
.vscode
*/**/.env
frontend/web/firebaseConfig.js
1 change: 0 additions & 1 deletion backend/functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 82 additions & 93 deletions backend/functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import {TaskType, TaskTypeWithID} from "./types";
import { TaskType, TaskTypeWithID } from "./types";
admin.initializeApp();
admin.firestore().settings({
ignoreUndefinedProperties: true,
Expand All @@ -10,7 +10,7 @@ admin.firestore().settings({
// https://firebase.google.com/docs/functions/typescript
//
export const helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
functions.logger.info("Hello logs!", { structuredData: true });
response.send("Hello from Firebase!");
});

Expand All @@ -21,15 +21,8 @@ export const helloWorld = functions.https.onRequest((request, response) => {
*/

const getTaskFromRequest = (request: functions.Request): TaskType => {
const {
title,
description,
dueDate,
priority,
groupId,
creator,
createdAt,
} = request.body;
const { title, description, dueDate, priority, groupId, creator, createdAt } =
request.body;
const task: TaskType = {
title,
creator,
Expand All @@ -41,104 +34,100 @@ const getTaskFromRequest = (request: functions.Request): TaskType => {
};
return task;
};
export const createTask = functions.https
.onRequest(async (request, response) => {
const task = {...getTaskFromRequest(request),
createdAt: new Date().toISOString(),
};
export const createTask = functions.https.onRequest(
async (request, response) => {
const task = {
...getTaskFromRequest(request),
createdAt: new Date().toISOString(),
};

if (!task.title || !task.creator) {
response.status(400).send("Title is required.");
return;
}
if (!task.title || !task.creator) {
response.status(400).send("Title is required.");
return;
}

try {
const ref = await admin.firestore().collection("tasks").add(task);
response.send(ref.id);
} catch (e) {
response.status(500).send(e);
}
});
try {
const ref = await admin.firestore().collection("tasks").add(task);
response.send(ref.id);
} catch (e) {
response.status(500).send(e);
}
}
);

/**
* On Success: Returns a Task.
* On Failure: Returns 400 Response (Bad Request).
*/
export const getTask = functions.https
.onRequest(async (request, response) => {
const id = request.query.id as string;
if (!id || typeof id !== "string") {
response.status(400).send("ID is required.");
return;
}
try {
const ref = await admin.firestore().collection("tasks").doc(id).get();
response.send(ref.data());
} catch (e) {
response.status(500).send(e);
}
}
);
export const getTask = functions.https.onRequest(async (request, response) => {
const id = request.query.id as string;
if (!id || typeof id !== "string") {
response.status(400).send("ID is required.");
return;
}
try {
const ref = await admin.firestore().collection("tasks").doc(id).get();
response.send(ref.data());
} catch (e) {
response.status(500).send(e);
}
});

/**
* On Success: Returns all Tasks.
* On Failure: Returns 400 Response (Bad Request).
*/
export const listTasks = functions.https
.onRequest(async (request, response) => {
try {
const ref = await admin.firestore().collection("tasks").get();
const tasks: TaskTypeWithID[] = [];
ref.forEach((doc) => {
tasks.push({...doc.data(), id: doc.id} as TaskTypeWithID);
});
response.send(tasks);
} catch (e) {
response.status(500).send(e);
}
export const listTasks = functions.https.onRequest(
async (request, response) => {
try {
const ref = await admin.firestore().collection("tasks").get();
const tasks: TaskTypeWithID[] = [];
ref.forEach((doc) => {
tasks.push({ ...doc.data(), id: doc.id } as TaskTypeWithID);
});
response.send(tasks);
} catch (e) {
response.status(500).send(e);
}
);
}
);

export const updateTask = functions.https
.onRequest(async (request, response) => {
const id = request.query.id as string;
if (!id || typeof id !== "string") {
response.status(400).send("ID is required.");
return;
}
const task = getTaskFromRequest(request);
try {
const ref = await admin.firestore().collection("tasks").doc(id);
const exists = await (await ref.get()).exists;
if (!exists) {
response.status(404).send("Task not found.");
return;
}
await ref.update(
task
);
response.send("ok");
} catch (e) {
response.status(500).send(e);
}
export const updateTask = functions.https.onRequest(
async (request, response) => {
const id = request.query.id as string;
if (!id || typeof id !== "string") {
response.status(400).send("ID is required.");
return;
}
);

export const deleteTask = functions.https
.onRequest(async (request, response) => {
const id = request.query.id as string;
if (!id || typeof id !== "string") {
response.status(400).send("ID is required.");
const task = getTaskFromRequest(request);
try {
const ref = await admin.firestore().collection("tasks").doc(id);
const exists = await (await ref.get()).exists;
if (!exists) {
response.status(404).send("Task not found.");
return;
}
try {
const ref = await admin.firestore().collection("tasks").doc(id);
await ref.delete();
response.send("ok");
} catch (e) {
response.status(500).send(e);
}
await ref.update(task);
response.send("ok");
} catch (e) {
response.status(500).send(e);
}
);

}
);

export const deleteTask = functions.https.onRequest(
async (request, response) => {
const id = request.query.id as string;
if (!id || typeof id !== "string") {
response.status(400).send("ID is required.");
return;
}
try {
const ref = await admin.firestore().collection("tasks").doc(id);
await ref.delete();
response.send("ok");
} catch (e) {
response.status(500).send(e);
}
}
);
Loading

0 comments on commit 7200407

Please sign in to comment.