Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
yarn.lock
package-lock.json
14 changes: 14 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const express = require('express')
const app = express()
const cors = require('cors')

const AppRouter = require('./routes/AppRouter')

const PORT = process.env.PORT || 3001

app.use(cors())
app.use(express.json())

app.get('/', (req, res) => res.json({ message: 'Server Works' }))
app.use('/api', AppRouter)
app.listen(PORT, () => console.log(`Server Started On Port: ${PORT}`))
20 changes: 20 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"development": {
"database": "nfl_development",
"host": "127.0.0.1",
"dialect": "postgres",
"define": {
"underscored": true
}
},
"test": {
"database": "nfl_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "nfl_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
64 changes: 64 additions & 0 deletions controllers/TeamController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { Team } = require('../models')

const GetAllTeams = async (req, res) => {
try {
const teams = await Team.findAll()
res.send(teams)
} catch (error) {
throw error
}
}

const GetTeamById = async (req, res) => {
try {
const team = await Team.findByPk(req.params.team_id)
res.send(team)
} catch (error) {
throw error
}
}

const CreateTeam = async (req, res) => {
try {
let teamId = parseInt(req.params.team_id)
let teamBody = {
teamId,
...req.body
}
let team = await Team.create(teamBody)
res.send(team)
} catch (error) {
throw error
}
}

const UpdateTeam = async (req, res) => {
try {
let teamId = parseInt(req.params.team_id)
let updatedTeam = await Team.update(req.body, {
where: { id: teamId },
returning: true
})
res.send(updatedTeam)
} catch (error) {
throw error
}
}

const DeleteTeam = async (req, res) => {
try {
let teamId = parseInt(req.params.team_id)
await Team.destroy({ where: { id: teamId } })
res.send({ message: `Deleted team with id of ${teamId}` })
} catch (error) {
throw error
}
}

module.exports = {
GetAllTeams,
GetTeamById,
CreateTeam,
UpdateTeam,
DeleteTeam
}
36 changes: 36 additions & 0 deletions migrations/20220412175704-create-player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('players', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
team: {
type: Sequelize.STRING
},
position: {
type: Sequelize.STRING
},
jerseyNumber: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('players');
}
};
33 changes: 33 additions & 0 deletions migrations/20220412175820-create-city.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('cities', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
state: {
type: Sequelize.STRING
},
team: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('cities');
}
};
30 changes: 30 additions & 0 deletions migrations/20220412175852-create-team.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('teams', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
coach: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('teams');
}
};
15 changes: 15 additions & 0 deletions migrations/20220412180647-change-player-so-it-is-correct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('players', 'teamId', {
type: Sequelize.INTEGER
})
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('players', 'teamId', {
type: Sequelize.INTEGER
})
}
}
15 changes: 15 additions & 0 deletions migrations/20220412181018-change-city-so-it-is-correct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('cities', 'teamId', {
type: Sequelize.INTEGER
})
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('cities', 'teamId', {
type: Sequelize.INTEGER
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('teams', 'createdAt', 'created_at')
},
down: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('users', 'created_at', 'createdAt')
}
}
10 changes: 10 additions & 0 deletions migrations/20220413125024-remove-underscores-in-updated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('teams', 'updatedAt', 'updated_at')
},
down: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('teams', 'updated_at', 'updatedAt')
}
}
35 changes: 35 additions & 0 deletions models/city.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class City extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
City.belongsTo(models.Team, { foreignKey: 'team_id', as: 'team' })
}
}
City.init(
{
name: DataTypes.STRING,
state: DataTypes.STRING,
teamId: {
type: DataTypes.INTEGER,
field: 'team_id',
onDelete: 'CASCADE',
references: {
model: 'teams',
key: 'id'
}
}
},
{
sequelize,
modelName: 'City',
tableName: 'cities'
}
)
return City
}
37 changes: 37 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
36 changes: 36 additions & 0 deletions models/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class Player extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
Player.belongsTo(models.Team, { foreignKey: 'team_id', as: 'team' })
}
}
Player.init(
{
name: DataTypes.STRING,
teamId: {
type: DataTypes.INTEGER,
field: 'team_id',
onDelete: 'CASCADE',
references: {
model: 'teams',
key: 'id'
}
},
position: DataTypes.STRING,
jerseyNumber: DataTypes.INTEGER
},
{
sequelize,
modelName: 'Player',
tableName: 'players'
}
)
return Player
}
Loading