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');
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;
module.exports = AccountController;

View File

@ -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;
module.exports = ShelfController;

View File

@ -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,

View File

@ -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) {