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

Garrett Heiner #266

Open
wants to merge 4 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
32 changes: 32 additions & 0 deletions controllers/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let express = require('express')
let db = require('../models')
let router = express.Router()

// GET /categories -- show all the categories that exist
router.get('/', (req, res) => {
db.category.findAll()
.then((category) => {
res.render('categories/index', { categories: category })
})
.catch((err) => {
res.status(400).render('main/404')
})
})


// GET /categories/:id -- show a speicifc category and all the projects with that category
router.get('/:id', (req, res) => {
db.category.findOne({
where: { id: req.params.id },
include: [{ model: db.project}]
})
.then((category) => {
if (!category) throw Error()
res.render('categories/show', { category: category })
})
.catch((err) => {
res.status(400).render('main/404')
})
})

module.exports = router
6 changes: 6 additions & 0 deletions controllers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ router.post('/', (req, res) => {
description: req.body.description
})
.then((project) => {
const [category] = db.category.findOrCreate({
where: {
name: req.body.category
}
})
project.addCategory(category)
res.redirect('/')
})
.catch((error) => {
Expand Down
51 changes: 51 additions & 0 deletions dbTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const db = require('./models')

// db.category.create({
// name: 'node'
// })
// .then(category => {
// console.log(category.id)
// })
// .catch(console.log)

// async function createCategory() {
// try {
// const newCategory = await db.category.create({ name: 'python' })
// console.log(newCategory)
// } catch (err) {
// console.log(err)
// }
// }

// createCategory()

// db.project.findOne({
// where: { id: 1 },
// include: [db.category]
// })
// .then(project => {
// // by using eager loading, the project model should have a categories key
// console.log(project.categories)

// // createCategory function should be available to this model - it will create the category then add it to the project
// project.createCategory({ name: 'express' })
// .then(category => {
// console.log(category.id)
// })
// })
// const db = require('./models')

// db.category.findOne({
// where: { id: categoryId },
// include: [db.project]
// })
// .then(category => {
// console.log(category.Projects)
// // })
// db.category.findOne({
// where: { id: 1 },
// include: [db.project]
// })
// .then(category => {
// console.log(category.projects)
// })
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ app.get('/', (req, res) => {
})

app.use('/projects', require('./controllers/projects'))
app.use('/categories', require('./controllers/categories'))

app.get('*', (req, res) => {
res.render('main/404')
Expand Down
28 changes: 28 additions & 0 deletions migrations/20230405223135-create-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
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');
}
};
31 changes: 31 additions & 0 deletions migrations/20230405224412-create-categories-projects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('categoriesProjects', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
categoryId: {
type: Sequelize.INTEGER
},
projectId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('categoriesProjects');
}
};
24 changes: 24 additions & 0 deletions models/categoriesprojects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class categoriesProjects 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
}
}
categoriesProjects.init({
categoryId: DataTypes.INTEGER,
projectId: DataTypes.INTEGER
}, {
sequelize,
modelName: 'categoriesProjects',
});
return categoriesProjects;
};
24 changes: 24 additions & 0 deletions models/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'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
models.category.belongsToMany(models.project, { through: 'categoriesProjects'} )
}
}
category.init({
name: DataTypes.STRING
}, {
sequelize,
modelName: 'category',
});
return category;
};
2 changes: 2 additions & 0 deletions models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = (sequelize, DataTypes) => {
*/
static associate(models) {
// define association here
models.project.belongsToMany(models.category, { through: 'categoriesProjects'} )

}
}
project.init({
Expand Down
8 changes: 8 additions & 0 deletions scratchpad.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sequelize model:create --name category --attributes name:string

categoriesProjects => categoriesProjects model
------------------------
categoryId: FK REFERENCES category.id (integer)
projectId: FK REFERENCES project.id (integer)

sequelize model:create --name categoriesProjects --attributes categoryId:integer,projectId:integer
9 changes: 9 additions & 0 deletions views/categories/show.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Project Categories</h1>

<% categories.forEach(category => { %>
<h2><%= category.name %></h2>
<% projects.forEach(project => { %>
<p><%= project.name %></p>
<% }) %>
<% }) %>

5 changes: 5 additions & 0 deletions views/projects/new.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<input type="text" class="form-control" id="deployedLink" name="deployedLink" required>
</div>

<div class="form-group">
<label for="category">category</label>
<input type="text" class="form-control" id="category" name="category" required></input>
</div>

<div class="form-group">
<label for="description">Description</label>
<textarea type="text" class="form-control" id="description" name="description" required></textarea>
Expand Down