forked from intrig-unicamp/paths-viewer
-
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.
#11: Configures firebase and creates routes
- Loading branch information
1 parent
cf2b5f0
commit 917357d
Showing
8 changed files
with
1,267 additions
and
13 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
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,15 @@ | ||
import * as admin from "firebase-admin"; | ||
import * as firebaseConfig from "../firebaseConfig.json"; | ||
|
||
if (!admin.apps.length) { | ||
try { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(firebaseConfig as admin.ServiceAccount), | ||
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL, | ||
}); | ||
} catch (error) { | ||
console.log("Firebase admin initialization error", error?.stack); | ||
} | ||
} | ||
|
||
export default admin; |
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,9 @@ | ||
export interface ICoordinatesData { | ||
date?: string; | ||
time?: string; | ||
id?: string; | ||
line?: string; | ||
latitude?: string; | ||
longitude?: string; | ||
speed?: string; | ||
} |
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,6 @@ | ||
import { ICoordinatesData } from "./ICoordinatesData"; | ||
|
||
export interface ISession { | ||
id: string; | ||
coordinates: ICoordinatesData[]; | ||
} |
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
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,39 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import admin from "../../../config/firebase"; | ||
import { ICoordinatesData } from "../../../models/ICoordinatesData"; | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponse) => { | ||
const db = admin.firestore(); | ||
const { method, query, body } = req; | ||
|
||
try { | ||
if (method == "PUT") { | ||
const { sessionId } = query; | ||
const docRef = db.doc(`sessions/${sessionId}`); | ||
|
||
try { | ||
await db.runTransaction(async (transaction) => { | ||
const snapshot = await transaction.get(docRef); | ||
const coordinates = docRef.collection("coordinates"); | ||
const coordinatesData = body as ICoordinatesData; | ||
const coordinatesRef = coordinates.doc( | ||
`${coordinatesData.date}T${coordinatesData.time}` | ||
); | ||
await transaction.create(coordinatesRef, coordinatesData); | ||
}); | ||
res.status(200).json({ id: docRef.id }); | ||
} catch (err) { | ||
console.error( | ||
`Error while adding coordinate to session ${sessionId}.`, | ||
err | ||
); | ||
res.status(500).json({ statusCode: 500, message: err.message }); | ||
} | ||
} else { | ||
res.setHeader("Allow", ["PUT"]); | ||
res.status(405).end(`Method ${method} Not Allowed`); | ||
} | ||
} catch (err: any) { | ||
res.status(500).json({ statusCode: 500, message: err.message }); | ||
} | ||
}; |
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,20 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import admin from "../../../config/firebase"; | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponse) => { | ||
const db = admin.firestore(); | ||
const { method } = req; | ||
|
||
try { | ||
if (method == "POST") { | ||
const id = new Date().valueOf().toString(); | ||
await db.doc(`sessions/${id}`).create({}); | ||
res.status(200).json({ id }); | ||
} else { | ||
res.setHeader("Allow", ["POST"]); | ||
res.status(405).end(`Method ${method} Not Allowed`); | ||
} | ||
} catch (err: any) { | ||
res.status(500).json({ statusCode: 500, message: err.message }); | ||
} | ||
}; |
Oops, something went wrong.