Skip to content

Commit 9799fa1

Browse files
author
Lauren Pothuru
committed
Implement folder feature endpoints
1 parent bc63f97 commit 9799fa1

File tree

2 files changed

+168
-2
lines changed

2 files changed

+168
-2
lines changed

backend/src/app.ts

Lines changed: 162 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
QuestionForm,
1818
QuestionFormWithId,
1919
LocationTravelTimes,
20+
Folder,
2021
} from '@common/types/db-types';
2122
// Import Firebase configuration and types
2223
import { auth } from 'firebase-admin';
@@ -42,7 +43,7 @@ const likesCollection = db.collection('likes');
4243
const usersCollection = db.collection('users');
4344
const pendingBuildingsCollection = db.collection('pendingBuildings');
4445
const contactQuestionsCollection = db.collection('contactQuestions');
45-
46+
const folderCollection = db.collection('folders');
4647
const travelTimesCollection = db.collection('travelTimes');
4748

4849
// Middleware setup
@@ -219,7 +220,7 @@ app.get('/api/review/like/:userId', authenticate, async (req, res) => {
219220
});
220221

221222
/**
222-
* Takes in the location type in the URL and returns the number of reviews made forr that location
223+
* Takes in the location type in the URL and returns the number of reviews made for that location
223224
*/
224225
app.get('/api/review/:location/count', async (req, res) => {
225226
const { location } = req.params;
@@ -1733,4 +1734,163 @@ app.post('/api/create-distance-to-campus', async (req, res) => {
17331734
}
17341735
});
17351736

1737+
// Endpoint to add a new folder for a user
1738+
app.post('/api/folders', authenticate, async (req, res) => {
1739+
try {
1740+
if (!req.user) throw new Error('Not authenticated');
1741+
const { uid } = req.user;
1742+
const { folderName } = req.body;
1743+
if (!folderName || folderName.trim() === '') {
1744+
return res.status(400).send('Folder name is required');
1745+
}
1746+
const newFolderRef = folderCollection.doc();
1747+
1748+
// Create a new folder document
1749+
await newFolderRef.set({
1750+
name: folderName,
1751+
userId: uid,
1752+
createdAt: new Date(),
1753+
});
1754+
1755+
return res.status(201).json({ id: newFolderRef.id, name: folderName });
1756+
} catch (err) {
1757+
console.error(err);
1758+
return res.status(500).send('Error creating folder');
1759+
}
1760+
});
1761+
1762+
// Endpoint to get all folders for a user
1763+
app.get('/api/folders', authenticate, async (req, res) => {
1764+
try {
1765+
if (!req.user) throw new Error('Not authenticated');
1766+
const { uid } = req.user;
1767+
1768+
// Fetch all folders for this user
1769+
const folderSnapshot = await folderCollection.where('userId', '==', uid).get();
1770+
1771+
const folders = folderSnapshot.docs.map((doc) => ({
1772+
id: doc.id,
1773+
...doc.data(),
1774+
}));
1775+
1776+
return res.status(200).json(folders);
1777+
} catch (err) {
1778+
console.error(err);
1779+
return res.status(500).send('Error fetching folders');
1780+
}
1781+
});
1782+
1783+
// Endpoint to delete a folder by ID
1784+
app.delete('/api/folders/:folderId', authenticate, async (req, res) => {
1785+
try {
1786+
if (!req.user) throw new Error('Not authenticated');
1787+
const { uid } = req.user;
1788+
const { folderId } = req.params;
1789+
1790+
const folderRef = folderCollection.doc(folderId);
1791+
const folderDoc = await folderRef.get();
1792+
1793+
if (!folderDoc.exists) {
1794+
return res.status(404).send('Folder not found');
1795+
}
1796+
1797+
if (folderDoc.data()?.userId !== uid) {
1798+
return res.status(403).send('Unauthorized to delete this folder');
1799+
}
1800+
1801+
await folderRef.delete();
1802+
return res.status(200).send('Folder deleted successfully');
1803+
} catch (err) {
1804+
console.error(err);
1805+
return res.status(500).send('Error deleting folder');
1806+
}
1807+
});
1808+
1809+
// Endpoint to rename a folder by ID
1810+
app.put('/api/folders/:folderId', authenticate, async (req, res) => {
1811+
try {
1812+
if (!req.user) throw new Error('Not authenticated');
1813+
const { uid } = req.user;
1814+
const { folderId } = req.params;
1815+
const { newName } = req.body;
1816+
1817+
const folderRef = folderCollection.doc(folderId);
1818+
const folderDoc = await folderRef.get();
1819+
1820+
if (!folderDoc.exists) {
1821+
return res.status(404).send('Folder not found');
1822+
}
1823+
1824+
if (folderDoc.data()?.userId !== uid) {
1825+
return res.status(403).send('Unauthorized to rename this folder');
1826+
}
1827+
1828+
await folderRef.update({ name: newName });
1829+
return res.status(200).send('Folder renamed successfully');
1830+
} catch (err) {
1831+
console.error(err);
1832+
return res.status(500).send('Error renaming folder');
1833+
}
1834+
});
1835+
1836+
// Endpoint to add an apartment to a folder
1837+
app.post('/api/folders/:id/apartments/:apartmentId', authenticate, async (req, res) => {
1838+
try {
1839+
if (!req.user) throw new Error('Not authenticated');
1840+
const { uid } = req.user;
1841+
const { folderId, aptId } = req.body;
1842+
1843+
const folderRef = folderCollection.doc(folderId);
1844+
const folderDoc = await folderRef.get();
1845+
1846+
if (!folderDoc.exists) {
1847+
return res.status(404).send('Folder not found');
1848+
}
1849+
1850+
if (folderDoc.data()?.userId !== uid) {
1851+
return res.status(403).send('Unauthorized to modify this folder');
1852+
}
1853+
1854+
const apartments = folderDoc.data()?.apartments || [];
1855+
if (apartments.includes(aptId)) {
1856+
return res.status(400).send('Apartment already in folder');
1857+
}
1858+
1859+
apartments.push(aptId);
1860+
await folderRef.update({ apartments });
1861+
return res.status(200).send('Apartment added to folder successfully');
1862+
} catch (err) {
1863+
console.error(err);
1864+
return res.status(500).send('Error adding apartment to folder');
1865+
}
1866+
});
1867+
1868+
// Endpoint to remove an apartment from a folder
1869+
app.post('/api/folders/:id/apartments/:apartmentId', authenticate, async (req, res) => {
1870+
try {
1871+
if (!req.user) throw new Error('Not authenticated');
1872+
const { uid } = req.user;
1873+
const { folderId, aptId } = req.body;
1874+
1875+
const folderRef = folderCollection.doc(folderId);
1876+
const folderDoc = await folderRef.get();
1877+
1878+
if (!folderDoc.exists) {
1879+
return res.status(404).send('Folder not found');
1880+
}
1881+
1882+
if (folderDoc.data()?.userId !== uid) {
1883+
return res.status(403).send('Unauthorized to modify this folder');
1884+
}
1885+
1886+
let apartments = folderDoc.data()?.apartments || [];
1887+
apartments = apartments.filter((id: string) => id !== aptId);
1888+
await folderRef.update({ apartments });
1889+
return res.status(200).send('Apartment removed from folder successfully');
1890+
} catch (err) {
1891+
console.error(err);
1892+
return res.status(500).send('Error removing apartment from folder');
1893+
}
1894+
});
1895+
17361896
export default app;

common/types/db-types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,9 @@ export type QuestionForm = {
100100
};
101101

102102
export type QuestionFormWithId = QuestionForm & Id;
103+
104+
export type Folder = {
105+
readonly name: string;
106+
readonly userId: string;
107+
readonly apartmentIds: string[];
108+
};

0 commit comments

Comments
 (0)