generated from chingu-voyages/voyage-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #42 from chingu-voyages/form-data-db
Save Form Data to Database
- Loading branch information
Showing
10 changed files
with
203 additions
and
73 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
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,16 @@ | ||
import mongoose from "mongoose"; | ||
import { dbConnectStr, myTestDb } from "./env.js"; | ||
|
||
export default async function connectDb() { | ||
try { | ||
await mongoose.connect(dbConnectStr, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
dbName: myTestDb, | ||
}); | ||
console.log("Successfully connected to MongoDB"); | ||
} catch (error) { | ||
console.log("Error with Mongo connection:", error); | ||
process.exit(1); | ||
} | ||
} |
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,7 @@ | ||
import dotenv from "dotenv"; | ||
dotenv.config(); // load env vars from .env | ||
|
||
// Env variables | ||
export const port = process.env.PORT; | ||
export const dbConnectStr = process.env.DATABASE_CONNECTION_STRING; | ||
export const myTestDb = process.env.MY_TEST_DB; |
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,42 @@ | ||
import formSchema from "../validators/form.validator.js"; | ||
import Form from "../models/form.model.js"; | ||
|
||
export async function newAppt(req, res, next) { | ||
try { | ||
const { error } = formSchema.validate(req.body); | ||
|
||
// handle validation errors | ||
if (error) { | ||
res.status(422); | ||
return next({ message: error.details[0].message }); | ||
} | ||
|
||
// send value to db | ||
const { earlyTimeHour, lateTimeHour, ...rest } = req.body; | ||
const newForm = new Form({ | ||
...rest, | ||
timeRange: { | ||
earlyTimeHour: "hello", | ||
lateTimeHour, | ||
}, | ||
}); | ||
await newForm.save(); | ||
|
||
// send success response | ||
res.status(201); | ||
res.json({ message: "ok" }); | ||
} catch (error) { | ||
console.log(error); | ||
// Mongo validation error | ||
if (error.name === "ValidationError") { | ||
res.status(400); | ||
return next({ | ||
message: "Invalid request", | ||
}); | ||
} | ||
res.status(500); | ||
return next({ | ||
message: "An internal server error occurred", | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const formSchema = new mongoose.Schema({ | ||
name: String, | ||
email: String, | ||
phone: String, | ||
address: String, | ||
timeRange: { | ||
earlyTimeHour: Number, | ||
lateTimeHour: Number, | ||
}, | ||
dateCreated: { type: Date, default: Date.now }, | ||
status: { | ||
type: String, | ||
enum: ["Pending", "Confirmed", "Cancelled", "Visited"], | ||
default: "Pending", | ||
}, | ||
}); | ||
|
||
const Form = mongoose.model("Form", formSchema, "forms"); | ||
|
||
export default Form; |
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,25 @@ | ||
import Joi from "joi"; | ||
|
||
const formSchema = Joi.object({ | ||
name: Joi.string().min(2).max(255).required().trim(), | ||
email: Joi.string() | ||
.email({ | ||
minDomainSegments: 2, | ||
tlds: { allow: false }, | ||
}) | ||
.trim(), | ||
phone: Joi.string() | ||
.pattern(new RegExp(/^[0-9]*$/)) | ||
.length(10) | ||
.trim(), | ||
address: Joi.string().required(), | ||
earlyTimeHour: Joi.number().min(9).max(16).required(), | ||
lateTimeHour: Joi.number() | ||
.min(10) | ||
.max(17) | ||
.required() | ||
// check that lateTime > earlyTime | ||
.greater(Joi.ref("earlyTimeHour")), | ||
}); | ||
|
||
export default formSchema; |