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

finished #53

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "
/node_modules
.DS_Store
.env" >> .gitignore
14 changes: 14 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"development": {
"database": "Mice_development",
"dialect": "postgres"
},
"test": {
"database": "Mice_test",
"dialect": "postgres"
},
"production": {
"database": "Mice_production",
"dialect": "postgres"
}
}
24 changes: 24 additions & 0 deletions controllers/CategoryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { Category } = require("../models");

const getAllCategories = async (req, res) => {
try {
const getCategories = Category.findAll();
res.send(getCategories);
} catch (error) {
throw error;
}
};

const getOneCategory = async (req, res) => {
try {
const getOneCategory = await Category.findByPk(req.params.category_id);
res.send(getOneCategory);
} catch (error) {
throw error;
}
};

module.exports = {
getAllCategories,
getOneCategory,
};
19 changes: 19 additions & 0 deletions controllers/MiceController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { Mouse } = require("../models");

const CreateMouse = async (req, res) => {
try {
let categoryid = parseInt(req.params.categoryId);
let MouseBody = {
categoryid,
...req.body,
};
const newMouse = await Mouse.create(MouseBody);
res.send(newMouse);
} catch (error) {
throw error;
}
};

module.exports = {
CreateMouse,
};
27 changes: 27 additions & 0 deletions migrations/20220412221404-create-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable("categories", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable("categories");
},
};
46 changes: 46 additions & 0 deletions migrations/20220412221631-create-mice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable("mice", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
allowNull: false,
type: Sequelize.STRING,
},
description: {
allowNull: false,
type: Sequelize.STRING,
},
category_id: {
allowNull: false,
type: Sequelize.INTEGER,
field: "category_id",
onDelete: "CASCADE",
references: {
model: "categories",
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("mice");
},
};
44 changes: 44 additions & 0 deletions migrations/20220412222737-create-inventory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable("inventoryList", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
MouseId: {
type: Sequelize.INTEGER,
field: "mouse_id",
onDelete: "CASCADE",
references: {
model: "mice",
key: "id",
},
},
sold: {
type: Sequelize.INTEGER,
},
stock: {
type: Sequelize.INTEGER,
},
returned: {
type: Sequelize.INTEGER,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
field: "updated_at",
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date(),
},
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable("inventoryList");
},
};
27 changes: 27 additions & 0 deletions migrations/20220413042505-adding-timestamp-to-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.changeColumn("categories", "createdAt", {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date(),
}),
await queryInterface.changeColumn("categories", "updatedAt", {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date(),
});
},

async down(queryInterface, Sequelize) {
await queryInterface.changeColumn("categories", "createdAt", {
allowNull: false,
type: Sequelize.DATE,
}),
await queryInterface.changeColumn("categories", "updatedAt", {
allowNull: false,
type: Sequelize.DATE,
});
},
};
18 changes: 18 additions & 0 deletions migrations/20220418063302-add-time-to-mice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.changeColumn("mice", "created_at", {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date(),
});
},

async down(queryInterface, Sequelize) {
await queryInterface.changeColumn("mice", "created_at", {
allowNull: false,
type: Sequelize.DATE,
});
},
};
9 changes: 9 additions & 0 deletions migrations/20220418064045-change-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.renameColumn("mice", "created_at", "createdAt");
},

down: function (queryInterface, Sequelize) {
//
},
};
9 changes: 9 additions & 0 deletions migrations/20220418064220-change-name-for-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.renameColumn("mice", "updated_at", "updatedAt");
},

down: function (queryInterface, Sequelize) {
//
},
};
10 changes: 10 additions & 0 deletions migrations/20220418064540-change-name-for-category_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.renameColumn("mice", "category_id", "categoryId");
},

down: function (queryInterface, Sequelize) {
//
},
};
30 changes: 30 additions & 0 deletions models/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Category 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
Category.hasOne(models.Mouse, {
foreignKey: "category_id",
onDelete: "cascade",
onUpdate: "cascade",
});
}
}
Category.init(
{
name: DataTypes.STRING,
},
{
sequelize,
modelName: "Category",
tableName: "categories",
}
);
return Category;
};
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/inventory.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 Inventory 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
Inventory.belongsTo(models.Mouse, {
foreignKey: "mouse_id",
onDelete: "cascade",
onUpdate: "cascade",
});
}
}
Inventory.init(
{
MouseId: DataTypes.INTEGER,
sold: DataTypes.INTEGER,
stock: DataTypes.INTEGER,
returned: DataTypes.INTEGER,
},
{
sequelize,
modelName: "Inventory",
tableName: "inventoryList",
}
);
return Inventory;
};
Loading