71 lines
2.3 KiB
JavaScript
71 lines
2.3 KiB
JavaScript
const express = require("express");
|
|
const cors = require("cors");
|
|
const bcrypt = require("bcryptjs");
|
|
|
|
const app = express();
|
|
|
|
const corsOptions = {
|
|
origin: "http://localhost:5173"
|
|
};
|
|
|
|
app.use(cors(corsOptions));
|
|
// parse requests of content-type - application/json
|
|
app.use(express.json());
|
|
// parse requests of content-type - application/x-www-form-urlencoded
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// database
|
|
const db = require("./models");
|
|
const Role = db.Role;
|
|
const User = db.User;
|
|
const PitchType = db.PitchType;
|
|
|
|
db.sequelize.sync();
|
|
// force: true will drop the table if it already exists
|
|
// db.sequelize.sync({force: true}).then(() => {
|
|
// console.log('Drop and Re-sync Database with { force: true }');
|
|
// initial();
|
|
// });
|
|
|
|
// simple route
|
|
app.get("/", (req, res) => {
|
|
res.json({ message: "Welcome to bullpen application." });
|
|
});
|
|
|
|
// routes
|
|
require('./routes/auth.routes')(app);
|
|
require('./routes/user.routes')(app);
|
|
require('./routes/pitchType.routes')(app);
|
|
require('./routes/bullpenSession.routes')(app);
|
|
|
|
function initial() {
|
|
Role.bulkCreate([
|
|
{ name: 'user' },
|
|
{ name: 'administrator' },
|
|
]);
|
|
User.bulkCreate([
|
|
{ firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: bcrypt.hashSync('nolan', 8) },
|
|
{ firstName: 'Sandy', lastName: 'Koufax', dateOfBirth: new Date(1935, 12, 30), email: 'sandy.koufax@bullpen.com', password: bcrypt.hashSync('sandy', 8) },
|
|
{ firstName: 'Pedro', lastName: 'Martinez', dateOfBirth: new Date(1971, 10, 25), email: 'pedro.martinez@bullpen.com', password: bcrypt.hashSync('pedro', 8) },
|
|
{ firstName: 'randy', lastName: 'johnson', dateOfBirth: new Date(1963, 9, 10), email: 'randy.johnson@bullpen.com', password: bcrypt.hashSync('randy', 8) }
|
|
]);
|
|
|
|
User.findAll().then(users => {
|
|
users.forEach(user => {
|
|
user.setRoles([1]);
|
|
});
|
|
});
|
|
|
|
PitchType.bulkCreate([
|
|
{ name: 'Fastball', abbreviation: 'FB' },
|
|
{ name: 'Curveball', abbreviation: 'CB' },
|
|
{ name: 'Slider', abbreviation: 'SL' },
|
|
{ name: 'Changeup', abbreviation: 'CH' },
|
|
{ name: 'Cutter', abbreviation: 'CUT' },
|
|
{ name: 'Sweeper', abbreviation: 'SW' },
|
|
{ name: 'Slurve', abbreviation: 'SLV' },
|
|
]);
|
|
}
|
|
|
|
module.exports = app;
|