Skip to content

Commit

Permalink
Merge pull request PsionicGeek#8 from MohakHarjani/mohak
Browse files Browse the repository at this point in the history
Added user schema
  • Loading branch information
PsionicGeek authored Jan 8, 2024
2 parents d869d99 + 2584b2a commit 944bfa0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
19 changes: 15 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ var usersRouter = require('./routes/users');
var adminRouter = require('./routes/admin');

var app = express();

//=======================================================================================================================
const mongoose = require('mongoose')
const dotenv = require('dotenv')
dotenv.config({ path: './env' })
//==========================================================================================================================
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
Expand All @@ -25,12 +29,12 @@ app.use('/users', usersRouter);
app.use('/admin', adminRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
app.use(function (req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
Expand All @@ -39,5 +43,12 @@ app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error');
});
//====================================================================================================================
//CONNECTION WITH THE DATABASE
mongoose.connect(process.env.DB_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => { console.log('CONNECTED TO DATABASE :)') })
.catch((err) => { console.log('CONNECTION TO DATABASE FAILED :(', err) })


module.exports = app;
//==========================================================================================================================
module.exports = app;
5 changes: 3 additions & 2 deletions models/dishesSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//

const mongoose = require('mongoose');
const Category = require('./categorySchema.js')

const Schema= mongoose.Schema({
name:{
Expand All @@ -25,8 +26,7 @@ const Schema= mongoose.Schema({
required:true
},
image:{
data:Buffer,
contentType:String,
type : String,
required:true
},
price:{
Expand Down Expand Up @@ -59,3 +59,4 @@ const Schema= mongoose.Schema({

const Dish = mongoose.model('Dish',Schema);
module.exports = Dish;

2 changes: 2 additions & 0 deletions models/orderSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// },

const mongoose = require('mongoose');
const User = require('./userSchema')
const Dish = require('./dishesSchema')
const Schema= mongoose.Schema({
status:{
type:Number,
Expand Down
2 changes: 2 additions & 0 deletions models/paymentSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//
//},
const mongoose = require('mongoose');
const User = require('./userSchema')
const Order = require('./orderSchema')
const Schema= mongoose.Schema({
payment_id:{
type:Number,
Expand Down
48 changes: 48 additions & 0 deletions models/userSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const mongoose = require('mongoose');
const Order = require('./orderSchema.js')
//==============================================================================
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true
},
mobileNumber: {
type: String,
required: true,
unique: true,
validate: {
validator: function (value) {
return /^\d{10}$/.test(value);
},
message: props => `${props.value} is not a valid mobile number! Please enter a 10-digit number.`,
}
},
email: {
type: String,
unique : true
},
password: {
type: String,
required: true,
},
address : [
{
type : String,
required : true,
}
],
isAdmin : {
type : Boolean,
default : false
},
orders : [
{
type : mongoose.Schema.Types.ObjectId,
ref : 'Order'
}
]
})
//===================================================================================
const User = mongoose.model('user', userSchema)
module.exports = User

0 comments on commit 944bfa0

Please sign in to comment.