-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into backend/feedback
- Loading branch information
Showing
5 changed files
with
108 additions
and
7 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,30 @@ | ||
import Contact from "../models/contact.js"; | ||
|
||
export async function saveContact(req, resp) { | ||
try { | ||
const { name, email, subject, message } = req.body; | ||
|
||
// Check if all fields are provided | ||
if (!name || !email || !subject || !message) { | ||
return resp.status(400).json({ message: "All fields are required." }); | ||
} | ||
|
||
// Create new contact document | ||
const newContact = new Contact({ name, email, subject, message }); | ||
|
||
// Save contact form data to the database | ||
await newContact.save(); | ||
|
||
// Respond with success message | ||
resp | ||
.status(201) | ||
.json({ message: "Contact form submitted successfully!", newContact }); | ||
} catch (error) { | ||
console.error("Error saving contact form:", error); | ||
resp.status(500).json({ message: "Failed to submit contact form.", error }); | ||
} | ||
} | ||
|
||
export async function getContact(req, resp) { | ||
resp.send('hello contact') | ||
} |
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 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const contactSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
match: [/^\S+@\S+\.\S+$/, "Please enter a valid email address"] | ||
}, | ||
subject: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
message: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
const Contact = mongoose.model("Contact", contactSchema); | ||
|
||
export default Contact; |
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,8 @@ | ||
import express from "express"; | ||
const router = express.Router(); | ||
import { getContact, saveContact } from "../controllers/contactController.js"; | ||
|
||
router.post("/saveContact", saveContact); | ||
router.get("/saveContact", getContact); | ||
|
||
export default 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