From e0b06e3be8ffc1491bc47d290f886b3e5f408c7f Mon Sep 17 00:00:00 2001 From: Kyle Belanger Date: Wed, 5 Mar 2025 17:11:21 -0500 Subject: [PATCH] update controllers and models --- backend/src/controllers/authController.js | 2 +- .../src/controllers/collectionController.js | 2 +- backend/src/controllers/ratingController.js | 2 +- backend/src/controllers/whiskeyController.js | 2 +- backend/src/models/Collection.js | 45 ++++++----- backend/src/models/Rating.js | 81 ++++++++++--------- backend/src/models/User.js | 74 +++++++++-------- backend/src/models/Whiskey.js | 47 +++++++++++ backend/src/models/Whisky.js | 44 ---------- backend/src/models/index.js | 18 +++-- 10 files changed, 166 insertions(+), 151 deletions(-) create mode 100644 backend/src/models/Whiskey.js delete mode 100644 backend/src/models/Whisky.js diff --git a/backend/src/controllers/authController.js b/backend/src/controllers/authController.js index febdb27..cea0a50 100644 --- a/backend/src/controllers/authController.js +++ b/backend/src/controllers/authController.js @@ -1,5 +1,5 @@ const jwt = require('jsonwebtoken'); -const { User } = require('../models/User'); +const { User } = require('../models'); const { Op } = require('sequelize'); exports.register = async (req, res) => { diff --git a/backend/src/controllers/collectionController.js b/backend/src/controllers/collectionController.js index b764eba..19018fc 100644 --- a/backend/src/controllers/collectionController.js +++ b/backend/src/controllers/collectionController.js @@ -1,4 +1,4 @@ -const { Collection, Whiskey } = require('../models/Whiskey'); +const { Collection, Whiskey } = require('../models'); // Get user's collection exports.getUserCollection = async (req, res) => { diff --git a/backend/src/controllers/ratingController.js b/backend/src/controllers/ratingController.js index b0c007c..2cfeaee 100644 --- a/backend/src/controllers/ratingController.js +++ b/backend/src/controllers/ratingController.js @@ -1,4 +1,4 @@ -const { Rating, Whiskey, User } = require('../models/Whiskey'); +const { Rating, Whiskey, User } = require('../models'); // Get all ratings for a whiskey exports.getWhiskeyRatings = async (req, res) => { diff --git a/backend/src/controllers/whiskeyController.js b/backend/src/controllers/whiskeyController.js index fd5951c..027f7ed 100644 --- a/backend/src/controllers/whiskeyController.js +++ b/backend/src/controllers/whiskeyController.js @@ -1,4 +1,4 @@ -const { Whiskey } = require('../models/Whiskey'); +const { Whiskey } = require('../models'); const { Op } = require('sequelize'); // Get all whiskies diff --git a/backend/src/models/Collection.js b/backend/src/models/Collection.js index b2cb0cd..7759b31 100644 --- a/backend/src/models/Collection.js +++ b/backend/src/models/Collection.js @@ -1,23 +1,26 @@ const { DataTypes } = require('sequelize'); -const { sequelize } = require('../config/db'); -const Collection = sequelize.define('Collection', { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - }, - purchaseDate: { - type: DataTypes.DATE - }, - purchasePrice: { - type: DataTypes.FLOAT - }, - notes: { - type: DataTypes.TEXT - }, - bottleStatus: { - type: DataTypes.ENUM('sealed', 'opened', 'empty'), - defaultValue: 'sealed' - } -}); \ No newline at end of file +module.exports = (sequelize) => { + const Collection = sequelize.define('Collection', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + purchaseDate: { + type: DataTypes.DATE + }, + purchasePrice: { + type: DataTypes.FLOAT + }, + notes: { + type: DataTypes.TEXT + }, + bottleStatus: { + type: DataTypes.ENUM('sealed', 'opened', 'empty'), + defaultValue: 'sealed' + } + }); + + return Collection; +}; \ No newline at end of file diff --git a/backend/src/models/Rating.js b/backend/src/models/Rating.js index 9c8b403..0ce389b 100644 --- a/backend/src/models/Rating.js +++ b/backend/src/models/Rating.js @@ -1,42 +1,45 @@ const { DataTypes } = require('sequelize'); -const { sequelize } = require('../config/db'); -const Rating = sequelize.define('Rating', { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - }, - score: { - type: DataTypes.INTEGER, - allowNull: false, - validate: { - min: 0, - max: 100 +module.exports = (sequelize) => { + const Rating = sequelize.define('Rating', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + score: { + type: DataTypes.INTEGER, + allowNull: false, + validate: { + min: 0, + max: 100 + } + }, + notes: { + type: DataTypes.TEXT + }, + nose: { + type: DataTypes.INTEGER, + validate: { + min: 0, + max: 10 + } + }, + taste: { + type: DataTypes.INTEGER, + validate: { + min: 0, + max: 10 + } + }, + finish: { + type: DataTypes.INTEGER, + validate: { + min: 0, + max: 10 + } } - }, - notes: { - type: DataTypes.TEXT - }, - nose: { - type: DataTypes.INTEGER, - validate: { - min: 0, - max: 10 - } - }, - taste: { - type: DataTypes.INTEGER, - validate: { - min: 0, - max: 10 - } - }, - finish: { - type: DataTypes.INTEGER, - validate: { - min: 0, - max: 10 - } - } -}); \ No newline at end of file + }); + + return Rating; +}; \ No newline at end of file diff --git a/backend/src/models/User.js b/backend/src/models/User.js index b39ae1a..893c13f 100644 --- a/backend/src/models/User.js +++ b/backend/src/models/User.js @@ -1,39 +1,43 @@ const { DataTypes } = require('sequelize'); -const { sequelize } = require('../config/db'); -const bcrypt = require('bcrypt'); -const User = sequelize.define('User', { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - }, - username: { - type: DataTypes.STRING, - allowNull: false, - unique: true - }, - email: { - type: DataTypes.STRING, - allowNull: false, - unique: true, - validate: { - isEmail: true +module.exports = (sequelize) => { + const User = sequelize.define('User', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + username: { + type: DataTypes.STRING, + allowNull: false, + unique: true + }, + email: { + type: DataTypes.STRING, + allowNull: false, + unique: true, + validate: { + isEmail: true + } + }, + password: { + type: DataTypes.STRING, + allowNull: false } - }, - password: { - type: DataTypes.STRING, - allowNull: false - } -}, { - hooks: { - beforeCreate: async (user) => { - const salt = await bcrypt.genSalt(10); - user.password = await bcrypt.hash(user.password, salt); - } - } -}); - -User.prototype.validatePassword = async function(password) { - return await bcrypt.compare(password, this.password); + }); + + // Add password hashing hook + User.beforeCreate(async (user) => { + const bcrypt = require('bcrypt'); + const salt = await bcrypt.genSalt(10); + user.password = await bcrypt.hash(user.password, salt); + }); + + // Add password validation method + User.prototype.validatePassword = async function(password) { + const bcrypt = require('bcrypt'); + return await bcrypt.compare(password, this.password); + }; + + return User; }; \ No newline at end of file diff --git a/backend/src/models/Whiskey.js b/backend/src/models/Whiskey.js new file mode 100644 index 0000000..fcba28b --- /dev/null +++ b/backend/src/models/Whiskey.js @@ -0,0 +1,47 @@ +const { DataTypes } = require('sequelize'); + +module.exports = (sequelize) => { + const Whiskey = sequelize.define('Whiskey', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + name: { + type: DataTypes.STRING, + allowNull: false + }, + distillery: { + type: DataTypes.STRING, + allowNull: false + }, + type: { + type: DataTypes.STRING, + allowNull: false + }, + country: { + type: DataTypes.STRING, + allowNull: false + }, + region: { + type: DataTypes.STRING + }, + age: { + type: DataTypes.INTEGER + }, + abv: { + type: DataTypes.FLOAT + }, + price: { + type: DataTypes.FLOAT + }, + description: { + type: DataTypes.TEXT + }, + imageUrl: { + type: DataTypes.STRING + } + }); + + return Whiskey; +}; \ No newline at end of file diff --git a/backend/src/models/Whisky.js b/backend/src/models/Whisky.js deleted file mode 100644 index 3f0cd19..0000000 --- a/backend/src/models/Whisky.js +++ /dev/null @@ -1,44 +0,0 @@ -const { DataTypes } = require('sequelize'); -const { sequelize } = require('../config/db'); - -const Whiskey = sequelize.define('Whiskey', { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - }, - name: { - type: DataTypes.STRING, - allowNull: false - }, - distillery: { - type: DataTypes.STRING, - allowNull: false - }, - type: { - type: DataTypes.STRING, - allowNull: false - }, - country: { - type: DataTypes.STRING, - allowNull: false - }, - region: { - type: DataTypes.STRING - }, - age: { - type: DataTypes.INTEGER - }, - abv: { - type: DataTypes.FLOAT - }, - price: { - type: DataTypes.FLOAT - }, - description: { - type: DataTypes.TEXT - }, - imageUrl: { - type: DataTypes.STRING - } -}); \ No newline at end of file diff --git a/backend/src/models/index.js b/backend/src/models/index.js index bace54f..8bbb97a 100644 --- a/backend/src/models/index.js +++ b/backend/src/models/index.js @@ -1,11 +1,14 @@ +// src/models/index.js const { sequelize } = require('../config/db'); -const User = require('./User'); -const Whiskey = require('./Whiskey'); -const Collection = require('./Collection'); -const Rating = require('./Rating'); +const { DataTypes } = require('sequelize'); -// Define relationships +// Initialize models +const User = require('./User')(sequelize); +const Whiskey = require('./Whiskey')(sequelize); +const Collection = require('./Collection')(sequelize); +const Rating = require('./Rating')(sequelize); +// Define associations User.hasMany(Collection); Collection.belongsTo(User); @@ -18,12 +21,11 @@ Rating.belongsTo(User); Whiskey.hasMany(Rating); Rating.belongsTo(Whiskey); -// Exports - +// Export models module.exports = { User, Whiskey, Collection, Rating, sequelize -}; +}; \ No newline at end of file