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

router.get("/", (req, res) => {
db.category.findAll()
.then((categories) => {

res.render("categories/show", {categories})
})
})

router.get("/:id", async (req, res) => {
try {
catId = req.params.id;
// EAGER LOADING
const categoryInfo = await db.category.findByPk(catId, {
include: [db.project]
})

// LAZY LOADING
// const categoryInfo = await db.category.findByPk(catId);
// const projectInfo = await categoryInfo.getProjects();

// console.log("cat info", categoryInfo)
// console.log("proj info", projectInfo)
res.render("categories/details", { categoryInfo})
} catch (error) {
console.log(error)
}
})

module.exports = router;
21 changes: 14 additions & 7 deletions controllers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ let db = require('../models')
let router = express.Router()

// POST /projects - create a new project
router.post('/', (req, res) => {
db.project.create({
router.post('/', async (req, res) => {
try {
console.log(req.body);
const newProject = await db.project.create( {
name: req.body.name,
githubLink: req.body.githubLink,
deployLink: req.body.deployedLink,
description: req.body.description
description: req.body.description,
})
.then((project) => {
res.redirect('/')
const [newCategory] = await db.category.findOrCreate({
where: {
name: req.body.category
}
})
.catch((error) => {
// returns [newCategory, true/false]
await newProject.addCategory(newCategory);
res.redirect('/')
} catch (err) {
res.status(400).render('main/404')
})
}
})

// GET /projects/new - display form for creating a new project
Expand Down
37 changes: 37 additions & 0 deletions dbTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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()

const db = require('./models')

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)
})
})
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/20230405222642-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/20230405223435-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;
};
1 change: 1 addition & 0 deletions models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = (sequelize, DataTypes) => {
*/
static associate(models) {
// define association here
models.project.belongsToMany(models.category, { through: "categoriesProjects"})
}
}
project.init({
Expand Down
61 changes: 61 additions & 0 deletions scratch-pad.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
As a user, I want to categorize projects using different names. For example, all of my Node projects will be under the category "Node".


As a user, I want to assign multiple categories to a single project.
As a user, I want to view a list of categories I've assigned.
As a user, I want to view projects associated with a category I've selected.

Part 1: Create a Category model
In order to add categories, create a Sequelize model to store categories. It's recommended that you name this model category. It will store one attribute: the name of the category (a string).

Once this model has been created, run the migration for the model and test the model's functionality. This can be done in a separate file. An example:


-------------------------

sequelize model:create --name category --attributes name:string

sequelize model:create --name categoriesProjects --attributes categoryId:integer,projectId:integer




-----------------------------


.then( () => {
const newCategory = db.category.findOrCreate({
category: req.body.category
})
return newCategory;
})
.then(() => {
newProject.addCategory(newCategory);
})



-----------------

.then(() => {
const categoryDetails = db.category.findOne({
where: {
name: req.body.category
}
}
)
return categoryDetails
.then ( () => {

const projectDetails = db.project.findOne({
where: {
name: req.body.name,
githubLink: req.body.githubLink,
deployLink: req.body.deployedLink,
description: req.body.description,
}
})
return projectDetails
})
})
projectDetails.addCategory(categoryDetails)
6 changes: 6 additions & 0 deletions views/categories/details.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1><%=categoryInfo.name%></h1>
<ul>
<% categoryInfo.projects.forEach(project => { %>
<li><%=project.name%></li>
<% }) %>
</ul>
6 changes: 6 additions & 0 deletions views/categories/show.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Categories:</h1>
<ul>
<% categories.forEach(category => { %>
<li><a href="/categories/<%=category.id%>"><%=category.name%></a></li>
<% }) %>
</ul>
5 changes: 5 additions & 0 deletions views/projects/new.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@
<textarea type="text" class="form-control" id="description" name="description" required></textarea>
</div>

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

<input type="submit" class="btn btn-primary">
</form>