Skip to content

Commit

Permalink
Merge pull request #18 from brukmula/notes-proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
brukmula committed Jul 31, 2023
2 parents f549ce3 + feb5074 commit 295cb34
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const bibleRouter = require('./routes/bible');
// const indexRouter = require('./routes/index');
const { usersRouter, usersFirebaseInit } = require('./routes/users');
const { socialRouter, socialFirebaseInit } = require('./routes/social');
const {notesRouter, notesFirebaseInit } = require('./routes/notes')
const { notesRouter, notesFirebaseInit } = require('./routes/notes')

// Firebase imports
const firebase = require('firebase');
Expand Down Expand Up @@ -55,7 +55,7 @@ app.use(express.static(path.join(__dirname, 'public')));

// Routes usage
app.use('/api', bibleRouter);
app.use('/', usersRouter, socialRouter);
app.use('/', usersRouter, socialRouter, notesRouter);

app.get('/health', (req, res) => {
res.status(200).send("Healthy: OK");
Expand Down
63 changes: 40 additions & 23 deletions routes/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,49 @@ app.use(bodyParser.json());
// POST endpoint to store notes
// Endpoint for submitting a new note
app.post('/notes', (req, res) => {
const { book, verse, code, tags } = req.body;
const user_in = req.header('user'); // JWT to identify the user
const { book, chapter, verse, note, tags, shared } = req.body;

if (!book || !verse || !code) {
return res.status(400).json({ error: "Book, verse, and code are required fields." });
if (!book || !verse || !note) {
return res.status(400).json({ error: "Book, verse, and note are required fields." });
}
else if (!user_in) {
res.status(400).json({ error: "No user token was sent"} );
}
else {
// Get the current timestamp
const timestamp = new Date().toISOString();

// Create a new note object
const newNote = {
book: book, // Book reference
chapter: chapter, // Chapter reference
verse: verse, // Verse reference
note: note, // Note text (data)
shared: shared ? shared : false, // For later sharing
tags: tags || [], // If no tags provided, default to an empty array
timestamp: timestamp, // Timestamp for feed and age purposes
};

firebaseApp.auth().verifyIdToken(user_in)
.then((token) => {
const uid = token.uid; // User id for DB references
const notes_ref = db.ref(`notes/${uid}`); // User's notes reference

// Get the current timestamp
const timestamp = new Date().toISOString();

// Create a new note object
const newNote = {
book: book,
verse: verse,
code: code,
tags: tags || [], // If no tags provided, default to an empty array
timestamp: timestamp,
};

// Save the note to the Firebase database
db.collection('notes').add(newNote)
.then(() => {
return res.status(201).json({ message: "Note saved successfully." });
})
.catch((error) => {
return res.status(500).json({ error: "Failed to save the note." });
});
const note = {}; // Workaround for later feed building with the notes
note[`${book}_${chapter}_${verse}`] = newNote;

// Save the note to the Firebase database
notes_ref.set(note)
.then(() => {
return res.status(201).json({ message: "Note saved successfully." });
})
.catch((error) => {
console.log(error);
return res.status(500).json({ error: "Failed to save the note." });
});
});
}
});

module.exports = { notesRouter: app, notesFirebaseInit };
26 changes: 26 additions & 0 deletions tests/notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
import requests

uri = "http://127.0.0.1:3000/"
# uri = "https://basil-backend-feutdwkkwq-uc.a.run.app/"

if __name__ == '__main__':
signin_headers: dict = {
'email': 'username@email.com',
'password': 'password'
}
token = requests.post(f"{uri}signin", headers=signin_headers).text
print(token)

data = {
'book': 'Genesis',
'chapter': 1,
'verse': 1,
'note': "example note"
}
headers = {
'user': token
}

response = requests.post(f"{uri}notes", headers=headers, json=data)
print(response.text)

0 comments on commit 295cb34

Please sign in to comment.