Naming & Association corrections:

Correct naming of Account and Shelf Controllers;
Add hasMany associations to db models
This commit is contained in:
Robbie Antenesse 2019-12-01 11:03:55 -07:00
parent 53662c0a60
commit 1458e936ea
4 changed files with 26 additions and 17 deletions

View File

@ -1,6 +1,6 @@
const crypto = require('crypto'); const crypto = require('crypto');
class Account { class AccountController {
constructor (userModel) { constructor (userModel) {
this.model = userModel; this.model = userModel;
} }
@ -131,8 +131,8 @@ class Account {
} }
async createUser (email, username, displayName, password, needsConfirmation) { async createUser (email, username, displayName, password, needsConfirmation) {
const hashData = Account.hashPassword(password); const hashData = AccountController.hashPassword(password);
// The data should already have gone through Account.cleanCreateAccountFormData() // The data should already have gone through AccountController.cleanCreateAccountFormData()
try { try {
return await this.model.create({ return await this.model.create({
email, 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) { if (!passwordIsValid) {
return { return {
error: true, error: true,
@ -223,4 +223,4 @@ class Account {
} }
module.exports = Account; module.exports = AccountController;

View File

@ -1,4 +1,4 @@
class Shelf { class ShelfController {
constructor (shelfModel, shelfItemModel) { constructor (shelfModel, shelfItemModel) {
this.model = shelfModel; this.model = shelfModel;
this.itemModel = shelfItemModel; this.itemModel = shelfItemModel;
@ -65,4 +65,4 @@ class Shelf {
} }
} }
module.exports = Shelf; module.exports = ShelfController;

View File

@ -76,6 +76,9 @@ function getSequelizeModels (sequelize) {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
allowNull: false, allowNull: false,
validate: {
len: [1, 32],
},
}, },
isPublic: { isPublic: {
type: Sequelize.BOOLEAN, type: Sequelize.BOOLEAN,
@ -104,6 +107,7 @@ function getSequelizeModels (sequelize) {
foreignKey: 'userId', foreignKey: 'userId',
onDelete: 'CASCADE', onDelete: 'CASCADE',
}); });
User.hasMany(Shelf);
const BookReference = sequelize.define('bookReference', { const BookReference = sequelize.define('bookReference', {
id: { id: {
@ -195,6 +199,7 @@ function getSequelizeModels (sequelize) {
foreignKey: 'shelfId', foreignKey: 'shelfId',
onDelete: 'CASCADE', onDelete: 'CASCADE',
}); });
Shelf.hasMany(ShelfItem);
ShelfItem.belongsTo(BookReference, { ShelfItem.belongsTo(BookReference, {
foreignKey: 'bookId', foreignKey: 'bookId',
onDelete: 'CASCADE', onDelete: 'CASCADE',
@ -275,6 +280,7 @@ function getSequelizeModels (sequelize) {
foreignKey: 'userId', foreignKey: 'userId',
onDelete: 'CASCADE', onDelete: 'CASCADE',
}); });
User.hasMany(Status);
const Recommendation = sequelize.define('recommendation', { const Recommendation = sequelize.define('recommendation', {
id: { id: {
@ -339,6 +345,9 @@ function getSequelizeModels (sequelize) {
foreignKey: 'toUser', foreignKey: 'toUser',
onDelete: 'CASCADE', onDelete: 'CASCADE',
}); });
User.hasMany(Recommendation, {
foreignKey: 'toUser',
})
return { return {
User, User,

View File

@ -1,7 +1,7 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const Account = require('../controllers/account'); const AccountController = require('../controllers/account');
const Shelf = require('../controllers/shelf'); const ShelfController = require('../controllers/shelf');
async function routes(fastify, options) { async function routes(fastify, options) {
fastify.get('/api/accounts/test', async (request, reply) => { 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) { if (formDataIsValid !== true) {
return reply.code(400).send(formDataIsValid); 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); const canCreateUser = await account.canCreateUser(formData.email, formData.username);
if (canCreateUser !== true) { if (canCreateUser !== true) {
@ -37,7 +37,7 @@ async function routes(fastify, options) {
return reply.code(400).send(newUser); 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); const defaultShelvesCreated = await shelf.createDefaultShelves(newUser);
// If some of the default shelves are not created successfully, delete the user and send an error // 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) { if (formDataIsValid !== true) {
return reply.code(400).send(formDataIsValid); 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); 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) => { fastify.post('/api/account/login', async (request, reply) => {
const formDataIsValid = Account.loginDataIsValid(request.body); const formDataIsValid = AccountController.loginDataIsValid(request.body);
if (formDataIsValid !== true) { if (formDataIsValid !== true) {
return reply.code(400).send(formDataIsValid); 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); const user = await account.validateLogin(request.body.email, request.body.password);
if (user.error === true) { if (user.error === true) {