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.
17 changes: 17 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"development": {
"database": "farmer_database_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"database": "farmer_database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "farmer_database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
58 changes: 58 additions & 0 deletions controllers/AnimalController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { Ranch, Animal } = require('../models')



const GetAnimal = async (req, res) => {
try {
const animal = await Animal.findByPk(req.params.animal_id)
res.send(animal)
} catch (error) {
throw error
}
}

const CreateAnimal = async (req, res) => {
try {
let ranchId = parseInt(req.params.ranch_id)
let animalBody = {
ranchId,
...req.body
}
let animal = await Animal.create(animalBody)
res.json(animal)
} catch (error) {
throw error
}
}

const UpdateAnimal = async (req, res) => {
try {
let animalId = parseInt(req.params.animal_id)
let updatedAnimal = await Animal.update(req.body, {
where: { id: animalId },
returning: true
})
res.json(updatedAnimal)
} catch (error) {
throw error
}
}

const DeleteAnimal = async (req, res) => {
try {
let animalId = parseInt(req.params.animal_id)
await Animal.destroy({ where: {id: animalId} })
res.send({ message: `Deleted twert with an id of ${animalId}`})
} catch (error) {
throw error
}
}

module.exports = {


GetAnimal,
CreateAnimal,
UpdateAnimal,
DeleteAnimal
}
40 changes: 40 additions & 0 deletions migrations/20220412185647-create-ranch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('ranches', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
location: {
type: Sequelize.STRING
},
acreage: {
type: Sequelize.INTEGER
},
description: {
type: Sequelize.STRING
},
createdAt: {
field: 'created_at',
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
field: 'updated_at',
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('ranches');
}
};
47 changes: 47 additions & 0 deletions migrations/20220412190430-create-animal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('animals', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
animalName: {
type: Sequelize.STRING
},
species: {
type: Sequelize.STRING
},
sex: {
type: Sequelize.STRING
},
ranchId: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'ranch_id',
onDelete: 'CASCADE',
references: {
model: 'ranches',
key: 'id'
}
},
createdAt: {
field: 'created_at',
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
field: 'updated_at',
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('animals');
}
};
63 changes: 63 additions & 0 deletions migrations/20220412191939-create-daily.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('dailies', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
feeding: {
type: Sequelize.STRING
},
exercise: {
type: Sequelize.STRING
},
training: {
type: Sequelize.STRING
},
health: {
type: Sequelize.STRING
},
upkeep: {
type: Sequelize.STRING
},
animalId: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'animal_id',
onDelete: 'CASCADE',
references: {
model: 'animals',
key: 'id'
}
},
ranchId: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'ranch_id',
onDelete: 'CASCADE',
references: {
model: 'ranches',
key: 'id'
}
},
createdAt: {
field: 'created_at',
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
field: 'updated_at',
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date()
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('dailies');
}
};
46 changes: 46 additions & 0 deletions models/animal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Animal 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) {
// define association here
Animal.belongsTo(models.Ranch, {
foreignKey: 'ranch_id',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})
Animal.hasMany(models.Daily, {
foreignKey: 'animal_id',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})
}
}
Animal.init({
animalName: DataTypes.STRING,
species: DataTypes.STRING,
sex: DataTypes.STRING,
ranchId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'ranch_id',
onDelete: 'CASCADE',
references: {
model: 'ranches',
key: 'id'
}
}
}, {
sequelize,
modelName: 'Animal',
tableName: 'animals'
});
return Animal;
};
58 changes: 58 additions & 0 deletions models/daily.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Daily 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) {
// define association here
Daily.belongsTo(models.Ranch, {
foreignKey: 'ranch_id',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})
Daily.belongsTo(models.Animal, {
foreignKey: 'animal_id',
onDelete: "CASCADE",
onUpdate: 'CASCADE'
})
}
}
Daily.init({
feeding: DataTypes.STRING,
exercise: DataTypes.STRING,
training: DataTypes.STRING,
health: DataTypes.STRING,
upkeep: DataTypes.STRING,
animalId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'animal_id',
onDelete: 'CASCADE',
references: {
model: 'animals',
key: 'id'
}
},
ranchId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'ranch_id',
onDelete: 'CASCADE',
references: {
model: 'ranches',
key: 'id'
}
}
}, {
sequelize,
modelName: 'Daily',
tableName: 'dailies'
});
return Daily;
};
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;
Loading