-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from Sawan-Kushwah/backend/contactForm
Added backend api of contact form
- Loading branch information
Showing
5 changed files
with
134 additions
and
21 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,49 @@ | ||
const ContactForm = require('../Models/Contact'); | ||
|
||
const submitQueery = async (req, res) => { | ||
console.log(req.body); | ||
|
||
// Extract data from the request body | ||
const { name, email, message } = req.body; | ||
|
||
// Validate the presence of required fields | ||
if (!name || !email || !message) { | ||
return res.status(400).json({ error: "All fields are required: name, email, message." }); | ||
} | ||
|
||
const contactData = { | ||
name: name, | ||
email: email, | ||
msg: message, | ||
createdAt: new Date(), | ||
}; | ||
|
||
console.log(contactData); | ||
|
||
try { | ||
// Declare the variable properly | ||
const queery = new ContactForm(contactData); | ||
|
||
// Save to the database | ||
await queery.save(); | ||
|
||
// Send a success response | ||
res.status(200).json({ message: 'Contact form submitted successfully.' }); | ||
} catch (error) { | ||
// Log the error for debugging | ||
console.error("Error in submitting contact form:", error); | ||
|
||
// Send an error response | ||
res.status(500).json({ error: "Contact form not submitted successfully." }); | ||
} | ||
} | ||
|
||
const sayHello = async (req, res) => { | ||
console.log("GET request to contact form route"); | ||
res.status(200).json({ message: 'Hello from the server!' }); | ||
} | ||
|
||
module.exports = { | ||
submitQueery, | ||
sayHello | ||
}; |
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,31 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
// Define the schema for the contact form | ||
const contactFormSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
lowercase: true, | ||
match: [/.+\@.+\..+/, 'Please enter a valid email address'] | ||
}, | ||
msg: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
// Create the model from the schema | ||
const ContactForm = mongoose.model('ContactForm', contactFormSchema); | ||
|
||
module.exports = ContactForm; |
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,9 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const { submitQueery, sayHello } = require("../Controllers/contact"); | ||
|
||
router.post("/submitContactForm", submitQueery); | ||
router.get("/contactHello", sayHello) | ||
|
||
|
||
module.exports = router; |