From 1458e936ea6343fb3b2f015cf0b2d30b182c19ef Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 1 Dec 2019 11:03:55 -0700 Subject: [PATCH] Naming & Association corrections: Correct naming of Account and Shelf Controllers; Add hasMany associations to db models --- server/controllers/account.js | 10 +++++----- server/controllers/shelf.js | 4 ++-- server/getSequelizeModels.js | 9 +++++++++ server/routes/account.js | 20 ++++++++++---------- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/server/controllers/account.js b/server/controllers/account.js index 1d08eed..fdd3273 100644 --- a/server/controllers/account.js +++ b/server/controllers/account.js @@ -1,6 +1,6 @@ const crypto = require('crypto'); -class Account { +class AccountController { constructor (userModel) { this.model = userModel; } @@ -131,8 +131,8 @@ class Account { } async createUser (email, username, displayName, password, needsConfirmation) { - const hashData = Account.hashPassword(password); - // The data should already have gone through Account.cleanCreateAccountFormData() + const hashData = AccountController.hashPassword(password); + // The data should already have gone through AccountController.cleanCreateAccountFormData() try { return await this.model.create({ email, @@ -203,7 +203,7 @@ class Account { }; } - const passwordIsValid = Account.verifyPassword(existingUser.passwordHash, existingUser.passwordSalt, password); + const passwordIsValid = AccountController.verifyPassword(existingUser.passwordHash, existingUser.passwordSalt, password); if (!passwordIsValid) { return { error: true, @@ -223,4 +223,4 @@ class Account { } -module.exports = Account; \ No newline at end of file +module.exports = AccountController; \ No newline at end of file diff --git a/server/controllers/shelf.js b/server/controllers/shelf.js index 932e0d3..51bedea 100644 --- a/server/controllers/shelf.js +++ b/server/controllers/shelf.js @@ -1,4 +1,4 @@ -class Shelf { +class ShelfController { constructor (shelfModel, shelfItemModel) { this.model = shelfModel; this.itemModel = shelfItemModel; @@ -65,4 +65,4 @@ class Shelf { } } -module.exports = Shelf; \ No newline at end of file +module.exports = ShelfController; \ No newline at end of file diff --git a/server/getSequelizeModels.js b/server/getSequelizeModels.js index d47ccd8..8268ee7 100644 --- a/server/getSequelizeModels.js +++ b/server/getSequelizeModels.js @@ -76,6 +76,9 @@ function getSequelizeModels (sequelize) { name: { type: Sequelize.STRING, allowNull: false, + validate: { + len: [1, 32], + }, }, isPublic: { type: Sequelize.BOOLEAN, @@ -104,6 +107,7 @@ function getSequelizeModels (sequelize) { foreignKey: 'userId', onDelete: 'CASCADE', }); + User.hasMany(Shelf); const BookReference = sequelize.define('bookReference', { id: { @@ -195,6 +199,7 @@ function getSequelizeModels (sequelize) { foreignKey: 'shelfId', onDelete: 'CASCADE', }); + Shelf.hasMany(ShelfItem); ShelfItem.belongsTo(BookReference, { foreignKey: 'bookId', onDelete: 'CASCADE', @@ -275,6 +280,7 @@ function getSequelizeModels (sequelize) { foreignKey: 'userId', onDelete: 'CASCADE', }); + User.hasMany(Status); const Recommendation = sequelize.define('recommendation', { id: { @@ -339,6 +345,9 @@ function getSequelizeModels (sequelize) { foreignKey: 'toUser', onDelete: 'CASCADE', }); + User.hasMany(Recommendation, { + foreignKey: 'toUser', + }) return { User, diff --git a/server/routes/account.js b/server/routes/account.js index fd6a55b..2118570 100644 --- a/server/routes/account.js +++ b/server/routes/account.js @@ -1,7 +1,7 @@ const fs = require('fs'); const path = require('path'); -const Account = require('../controllers/account'); -const Shelf = require('../controllers/shelf'); +const AccountController = require('../controllers/account'); +const ShelfController = require('../controllers/shelf'); async function routes(fastify, options) { fastify.get('/api/accounts/test', async (request, reply) => { @@ -16,14 +16,14 @@ async function routes(fastify, options) { }); } - const formDataIsValid = Account.createAccountDataIsValid(request.body); + const formDataIsValid = AccountController.createAccountDataIsValid(request.body); if (formDataIsValid !== true) { return reply.code(400).send(formDataIsValid); } - const formData = Account.cleanCreateAccountFormData(request.body); + const formData = AccountController.cleanCreateAccountFormData(request.body); - const account = new Account(fastify.models.User); + const account = new AccountController(fastify.models.User); const canCreateUser = await account.canCreateUser(formData.email, formData.username); if (canCreateUser !== true) { @@ -37,7 +37,7 @@ async function routes(fastify, options) { return reply.code(400).send(newUser); } - const shelf = new Shelf(fastify.models.Shelf, fastify.models.ShelfItem); + const shelf = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem); const defaultShelvesCreated = await shelf.createDefaultShelves(newUser); // If some of the default shelves are not created successfully, delete the user and send an error @@ -101,12 +101,12 @@ async function routes(fastify, options) { }); } - const formDataIsValid = Account.confirmAccountDataIsValid(request.body); + const formDataIsValid = AccountController.confirmAccountDataIsValid(request.body); if (formDataIsValid !== true) { return reply.code(400).send(formDataIsValid); } - const account = new Account(fastify.models.User); + const account = new AccountController(fastify.models.User); const confirmed = await account.confirmUser(request.body.id, request.body.confirm); @@ -157,12 +157,12 @@ async function routes(fastify, options) { }); fastify.post('/api/account/login', async (request, reply) => { - const formDataIsValid = Account.loginDataIsValid(request.body); + const formDataIsValid = AccountController.loginDataIsValid(request.body); if (formDataIsValid !== true) { return reply.code(400).send(formDataIsValid); } - const account = new Account(fastify.models.User); + const account = new AccountController(fastify.models.User); const user = await account.validateLogin(request.body.email, request.body.password); if (user.error === true) {