Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Latest code for LL-319, publish/subscribe route files, and database c…
Browse files Browse the repository at this point in the history
…onnection file
  • Loading branch information
KevinLemon112 committed Feb 26, 2024
1 parent 0516728 commit 6170820
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/notification/publish/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();

const dbURI = `mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASS}@luminositycluster-0.cgornhw.mongodb.net/Luminosity`; //MongoDb Connection String
const dbURI = `mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASS}@luminositycluster-0.cgornhw.mongodb.net/test`; //MongoDb Connection String

mongoose.connect(dbURI);

Expand Down
20 changes: 13 additions & 7 deletions src/notification/publish/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,41 @@ mongoose.connect(db.dbURI)

server.post("/publish/accounts/new", async (req, res) => {
try {
const eventData = {
const eventData = new EventModel({
email: req.body.email,
name: req.body.name,
};
});

const newEvent = await EventModel.create(eventData);
const newEvent = await eventData.save(); // Save the new document to the database
res.json(newEvent);

res.status(201).json(newEvent); // HTTP 201 Created status code for successful creation
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});


server.delete("/publish/accounts/:id/delete", async (req, res) => {
try {
const deletedEvent = await EventModel.findByIdAndDelete(req.params.id);

if (!deletedEvent) {
const events = await EventModel.findByIdAndDelete(req.params.id);

if (!events) {
// If the document doesn't exist, return a 404 Not Found response
res.status(404).json({ error: 'Event not found' });
return;
}


// If the document is successfully deleted, return a success message
res.json({ message: 'Event deleted successfully' });
} catch (error) {
// If an error occurs during deletion, return a 500 Internal Server Error response
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});


server.get("/publish/:id", async (req, res) => {
const client: MqttClient = mqtt.connect(process.env.ACTIVE_MQ_ENDPOINT as string, options);
Expand Down
2 changes: 1 addition & 1 deletion src/notification/subscribe/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ client.on('message', async (receivedTopic, msg) => {

console.error(error);
}
});
});

0 comments on commit 6170820

Please sign in to comment.