From 016d1bd2f2f10a722903ca0b47376410f16fda7a Mon Sep 17 00:00:00 2001 From: awellsbiz Date: Wed, 5 Apr 2023 22:24:45 -0700 Subject: [PATCH 1/2] Stepts 1-3.2 complete --- controllers/category.js | 18 ++++++++++++++++++ controllers/projects.js | 35 ++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 controllers/category.js diff --git a/controllers/category.js b/controllers/category.js new file mode 100644 index 00000000..55a260f7 --- /dev/null +++ b/controllers/category.js @@ -0,0 +1,18 @@ +let express = require('express') +let db = require('../models') +let router = express.Router() + + +//GET /categories -- show all categories that exist +router.get('/category', async (req,res) => { + try{ + const allCategories = await db.category.findAll() + console.log("im here") + res.send(allCategories) + }catch(err){ + console.log(err) + } + }) + + module.exports = router + diff --git a/controllers/projects.js b/controllers/projects.js index 3d878a8a..93b67c42 100644 --- a/controllers/projects.js +++ b/controllers/projects.js @@ -3,19 +3,27 @@ let db = require('../models') let router = express.Router() // POST /projects - create a new project -router.post('/', (req, res) => { - db.project.create({ - name: req.body.name, - githubLink: req.body.githubLink, - deployLink: req.body.deployedLink, - description: req.body.description - }) - .then((project) => { +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 + }) + + const [newCategory] = await db.category.findOrCreate({ + where: {name: req.body.category} + }) + await newProject.addCategory(newCategory) + res.redirect('/') - }) - .catch((error) => { + }catch(err){ + console.log(err) res.status(400).render('main/404') - }) + } }) // GET /projects/new - display form for creating a new project @@ -37,4 +45,9 @@ router.get('/:id', (req, res) => { }) }) + + + +//GET /categories/:id -- show a specific category and all the projects with that category + module.exports = router From ebf5b35c7d63b1c6bbdc4d809d623f1a8ad54b18 Mon Sep 17 00:00:00 2001 From: awellsbiz Date: Thu, 6 Apr 2023 10:58:43 -0700 Subject: [PATCH 2/2] Deliverable 80% complete --- controllers/categories.js | 33 +++++++++++++++++ controllers/category.js | 18 --------- dbTest.js | 37 +++++++++++++++++++ index.js | 1 + migrations/20230405222707-create-category.js | 28 ++++++++++++++ ...230405223456-create-categories-projects.js | 31 ++++++++++++++++ models/categoriesprojects.js | 25 +++++++++++++ models/category.js | 24 ++++++++++++ models/project.js | 1 + scratchpad.txt | 15 ++++++++ views/categories/index.ejs | 10 +++++ views/categories/show.ejs | 0 views/projects/new.ejs | 9 ++++- 13 files changed, 212 insertions(+), 20 deletions(-) create mode 100644 controllers/categories.js delete mode 100644 controllers/category.js create mode 100644 dbTest.js create mode 100644 migrations/20230405222707-create-category.js create mode 100644 migrations/20230405223456-create-categories-projects.js create mode 100644 models/categoriesprojects.js create mode 100644 models/category.js create mode 100644 scratchpad.txt create mode 100644 views/categories/index.ejs create mode 100644 views/categories/show.ejs diff --git a/controllers/categories.js b/controllers/categories.js new file mode 100644 index 00000000..b2650cf1 --- /dev/null +++ b/controllers/categories.js @@ -0,0 +1,33 @@ +let express = require('express') +let db = require('../models') +let router = express.Router() + + +//GET /categories -- show all categories that exist +router.get('/', async (req,res) => { + try{ + const allCategories = await db.category.findAll({attributes: ['name']}) + res.render('categories/index', {allCategories: allCategories}) + + }catch(err){ + console.log(err) + } + }) + + //router.get/:id -- show a specific project +router.get('/:id', async (req,res) => { + try{ + const oneCategory = await db.category.findOne({ + where: {id: req.params.id} + }) + await oneCategory.count(db.project) + res.send(count) + + }catch(err){ + console.log(err) + } + +}) + + module.exports = router + diff --git a/controllers/category.js b/controllers/category.js deleted file mode 100644 index 55a260f7..00000000 --- a/controllers/category.js +++ /dev/null @@ -1,18 +0,0 @@ -let express = require('express') -let db = require('../models') -let router = express.Router() - - -//GET /categories -- show all categories that exist -router.get('/category', async (req,res) => { - try{ - const allCategories = await db.category.findAll() - console.log("im here") - res.send(allCategories) - }catch(err){ - console.log(err) - } - }) - - module.exports = router - diff --git a/dbTest.js b/dbTest.js new file mode 100644 index 00000000..41a3f5ad --- /dev/null +++ b/dbTest.js @@ -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) + }) + }) \ No newline at end of file diff --git a/index.js b/index.js index f404f83d..e9753548 100644 --- a/index.js +++ b/index.js @@ -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') diff --git a/migrations/20230405222707-create-category.js b/migrations/20230405222707-create-category.js new file mode 100644 index 00000000..8e52fd79 --- /dev/null +++ b/migrations/20230405222707-create-category.js @@ -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'); + } +}; \ No newline at end of file diff --git a/migrations/20230405223456-create-categories-projects.js b/migrations/20230405223456-create-categories-projects.js new file mode 100644 index 00000000..4e407a24 --- /dev/null +++ b/migrations/20230405223456-create-categories-projects.js @@ -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'); + } +}; \ No newline at end of file diff --git a/models/categoriesprojects.js b/models/categoriesprojects.js new file mode 100644 index 00000000..ae654f01 --- /dev/null +++ b/models/categoriesprojects.js @@ -0,0 +1,25 @@ +'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; +}; \ No newline at end of file diff --git a/models/category.js b/models/category.js new file mode 100644 index 00000000..f7dd25c6 --- /dev/null +++ b/models/category.js @@ -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; +}; \ No newline at end of file diff --git a/models/project.js b/models/project.js index 735a0a47..f6605752 100644 --- a/models/project.js +++ b/models/project.js @@ -11,6 +11,7 @@ module.exports = (sequelize, DataTypes) => { */ static associate(models) { // define association here + models.project.belongsToMany(models.category, {through: 'categoriesProjects'}) } } project.init({ diff --git a/scratchpad.txt b/scratchpad.txt new file mode 100644 index 00000000..3fee8bf1 --- /dev/null +++ b/scratchpad.txt @@ -0,0 +1,15 @@ +TODO User Stories: +--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 + + +Step One: + +sequelize model:create --name category --attributes name:string + + +Step : + +sequelize model:create --name categoriesProjects --attributes categoryId:integer,projectId:integer diff --git a/views/categories/index.ejs b/views/categories/index.ejs new file mode 100644 index 00000000..3f3f874e --- /dev/null +++ b/views/categories/index.ejs @@ -0,0 +1,10 @@ +

A List of All Categories

+ + +<% allCategories.forEach(function(category) { %> + + <% }); %> \ No newline at end of file diff --git a/views/categories/show.ejs b/views/categories/show.ejs new file mode 100644 index 00000000..e69de29b diff --git a/views/projects/new.ejs b/views/projects/new.ejs index 3342d3ee..0c85f2d1 100644 --- a/views/projects/new.ejs +++ b/views/projects/new.ejs @@ -15,11 +15,16 @@ - +
- + +
+ + +
+