-
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.
Ported app to use the official mongodb driver, created User, Product,…
… Cart, Order models and related operations like add to cart, create new order etc
- Loading branch information
1 parent
49ae122
commit 439600c
Showing
17 changed files
with
378 additions
and
102 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 |
---|---|---|
@@ -1,27 +1,78 @@ | ||
const Sequelize = require('sequelize'); | ||
const mongodb = require('mongodb'); | ||
const getDb = require('../util/database').getDb; | ||
|
||
const sequelize = require('../util/database'); | ||
class Product { | ||
constructor(title, price, description, imageUrl, id, userId) { | ||
this.title = title; | ||
this.price = price; | ||
this.description = description; | ||
this.imageUrl = imageUrl; | ||
this._id = id ? new mongodb.ObjectId(id) : null; | ||
this.userId = userId; | ||
} | ||
|
||
save() { | ||
const db = getDb(); | ||
let dbOp; | ||
if (this._id) { | ||
// Update the product | ||
dbOp = db | ||
.collection('products') | ||
.updateOne({ _id: this._id }, { $set: this }); | ||
} else { | ||
dbOp = db.collection('products').insertOne(this); | ||
} | ||
return dbOp | ||
.then(result => { | ||
console.log(result); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
} | ||
|
||
static fetchAll() { | ||
const db = getDb(); | ||
return db | ||
.collection('products') | ||
.find() | ||
.toArray() | ||
.then(products => { | ||
console.log(products); | ||
return products; | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
} | ||
|
||
static findById(prodId) { | ||
const db = getDb(); | ||
return db | ||
.collection('products') | ||
.find({ _id: new mongodb.ObjectId(prodId) }) | ||
.next() | ||
.then(product => { | ||
console.log(product); | ||
return product; | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
} | ||
|
||
const Product = sequelize.define('product', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
autoIncrement: true, | ||
allowNull: false, | ||
primaryKey: true | ||
}, | ||
title: Sequelize.STRING, | ||
price: { | ||
type: Sequelize.DOUBLE, | ||
allowNull: false | ||
}, | ||
imageUrl: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
description: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
static deleteById(prodId) { | ||
const db = getDb(); | ||
return db | ||
.collection('products') | ||
.deleteOne({ _id: new mongodb.ObjectId(prodId) }) | ||
.then(result => { | ||
console.log('Deleted'); | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = Product; |
Oops, something went wrong.