Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group 3 - Nick Lopez #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
15 changes: 15 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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.use(express.urlencoded({ extended: true }))

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": "restaurant_database",
"host": "127.0.0.1",
"dialect": "postgres",
"define": {
"underscored": true
}
},
"test": {
"database": "restaurant_database",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "restaurant_database",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
19 changes: 19 additions & 0 deletions controllers/EmployeeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { Employee } = require('../models')

const CreateEmployee = async (req, res) => {
try {
let restaurantId = parseInt(req.params.restaurant_id)
let employeeBody = {
restaurantId,
...req.body
}
let employee = await Employee.create(employeeBody)
res.send(employee)
} catch (error) {
throw error
}
}

module.exports = {
CreateEmployee
}
Empty file added controllers/OwnerController.js
Empty file.
50 changes: 50 additions & 0 deletions controllers/RestaurantController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { Restaurant } = require('../models')
const GetRestaurants = async (req, res) => {
try {
const restaurants = await Restaurant.findAll()
res.send(restaurants)
} catch (error) {
throw error
}
}
const CreateRestaurant = async (req, res) => {
try {
let restaurantId = parseInt(req.params.restaurant_id)
let restaurantBody = {
restaurantId,
...req.body
}
let restaurant = await Restaurant.create(restaurantBody)
res.send(restaurant)
} catch (error) {
throw error
}
}
const UpdateRestaurant = async (req, res) => {
try {
let restaurantId = parseInt(req.params.restaurant_id)
let updatedRestaurant = await Restaurant.update(req.body, {
where: { id: restaurantId },
returning: true
})
res.send(updatedRestaurant)
} catch (error) {
throw error
}
}
const DeleteRestaurant = async (req, res) => {
try {
let restaurantId = parseInt(req.params.restaurant_id)
await Restaurant.destroy({ where: { id: restaurantId } })
res.send({ message: `deleted twert with id of ${restaurantId}` })
} catch (error) {
throw error
}
}

module.exports = {
GetRestaurants,
CreateRestaurant,
DeleteRestaurant,
UpdateRestaurant
}
30 changes: 30 additions & 0 deletions migrations/20220412173934-create-restaurant.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('restaurants', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
restaurantName: {
type: Sequelize.STRING,
field: 'restaurant_name'
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
field: 'created_at'
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
field: 'updated_at'
}
})
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('restaurants')
}
}
29 changes: 29 additions & 0 deletions migrations/20220412174042-create-employee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('employees', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
employeeName: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
field: 'created_at'
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
field: 'updated_at'
}
})
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('employees')
}
}
29 changes: 29 additions & 0 deletions migrations/20220412174120-create-owner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('owners', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
ownerName: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
field: 'created_at'
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
field: 'updated_at'
}
})
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('owners')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

module.exports = {
up(queryInterface, Sequelize) {
return queryInterface.addColumn('employees', 'restaurantId', {
type: Sequelize.INTEGER
})
},

down(queryInterface, Sequelize) {
return queryInterface.removeColumn('employees', 'restaurantId')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

module.exports = {
up(queryInterface, Sequelize) {
return queryInterface.addColumn('owners', 'restaurantId', {
type: Sequelize.INTEGER
})
},

down(queryInterface, Sequelize) {
return queryInterface.removeColumn('owners', 'restaurantId')
}
}
33 changes: 33 additions & 0 deletions models/employee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class Employee 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) {
Employee.belongsTo(models.Restaurant, { foreignKey: 'restaurantId' })
}
}
Employee.init(
{
employeeName: DataTypes.STRING,
restaurantId: {
type: DataTypes.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'restaurants',
key: 'id'
}
}
},
{
sequelize,
modelName: 'Employee',
tableName: 'employees'
}
)
return Employee
}
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;
33 changes: 33 additions & 0 deletions models/owner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class Owner 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) {
Owner.belongsTo(models.Restaurant, { foreignKey: 'restaurantId' })
}
}
Owner.init(
{
ownerName: DataTypes.STRING,
restaurantId: {
type: DataTypes.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'restaurants',
key: 'id'
}
}
},
{
sequelize,
modelName: 'Owner',
tableName: 'owners'
}
)
return Owner
}
26 changes: 26 additions & 0 deletions models/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class Restaurant 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) {
Restaurant.hasMany(models.Employee, { foreignKey: 'restaurantId' })
Restaurant.hasOne(models.Owner, { foreignKey: 'restaurantId' })
}
}
Restaurant.init(
{
restaurantName: { type: DataTypes.STRING, field: 'restaurant_name' }
},
{
sequelize,
modelName: 'Restaurant',
tableName: 'restaurants'
}
)
return Restaurant
}
Loading