generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
22 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 @@ | ||
const mongoose = require("mongoose"); | ||
const Schema = mongoose.Schema; | ||
|
||
const messageSchema = new Schema({ | ||
message: String, | ||
user: String, | ||
}); | ||
|
||
// create mongoose Model | ||
const Message = mongoose.model("Message", messageSchema); | ||
|
||
// export the model so other modules can import it | ||
module.exports = { | ||
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,33 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const { Message } = require("../models/Message.js"); | ||
|
||
router.post("/", async (req, res) => { | ||
try { | ||
const newMessage = new Message({ | ||
message: req.body.message, | ||
user: req.body.user, | ||
}); | ||
|
||
console.log(newMessage); | ||
|
||
const savedMessage = await newMessage.save(); | ||
|
||
console.log("Event created:", savedMessage); | ||
|
||
res.status(201).json({ | ||
status: "Success", | ||
message: "Message sent successfully!", | ||
data: savedMessage, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(400).json({ | ||
status: "Error", | ||
message: "Error sending the message", | ||
error: error.message, | ||
}); | ||
} | ||
}); | ||
|
||
module.exports = router; |
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