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
10 changes: 3 additions & 7 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
{
"development": {
"username": "sequelize",
"password": "sequelize",
"database": "project_organizer_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"database": "project_organizer_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "project_organizer_production",
"host": "127.0.0.1",
"use_env_variable": "DATABASE_URL",
"dialect": "postgres"
}
}
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();

router.get("/", (req, res) => {
db.category
.findAll()
.then((categories) => {
res.render("categories/index", { categories: categories });
})
.catch((error) => {
console.log("Error in GET /", error);
res.status(400).render("main/404");
});
});

// GET /categories/:id - display a specific category
router.get("/:id", async (req, res) => {
try {
const category = await db.category.findOne({
where: { id: req.params.id },
});
if (!category) throw Error();
const projects = await category.getProjects();
res.render("categories/show", { category: category, projects: projects });
} catch (error) {
console.log(error);
res.status(400).render("main/404");
}
});

module.exports = router;
77 changes: 43 additions & 34 deletions controllers/projects.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
let express = require('express')
let db = require('../models')
let router = express.Router()
let express = require("express");
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) => {
res.redirect('/')
})
.catch((error) => {
res.status(400).render('main/404')
})
})
router.post("/", async (req, res) => {
try {
const newProject = await db.project.create({
name: req.body.name,
githubLink: req.body.githubLink,
deployLink: req.body.deployedLink,
description: req.body.description,
});
const categoryInputs = await req.body.category.split(", ");
console.log(categoryInputs);
categoryInputs.forEach(async (categoryInput) => {
const [category] = await db.category.findOrCreate({
where: {
name: categoryInput,
},
});
newProject.addCategory(category);
});
res.redirect("../");
} catch (error) {
res.status(400).render("main/404");
}
});

// GET /projects/new - display form for creating a new project
router.get('/new', (req, res) => {
res.render('projects/new')
})
router.get("/new", (req, res) => {
res.render("projects/new");
});

// GET /projects/:id - display a specific project
router.get('/:id', (req, res) => {
db.project.findOne({
where: { id: req.params.id }
})
.then((project) => {
if (!project) throw Error()
res.render('projects/show', { project: project })
})
.catch((error) => {
res.status(400).render('main/404')
})
})
router.get("/:id", (req, res) => {
db.project
.findOne({
where: { id: req.params.id },
})
.then((project) => {
if (!project) throw Error();
res.render("projects/show", { project: project });
})
.catch((error) => {
res.status(400).render("main/404");
});
});

module.exports = router
module.exports = router;
16 changes: 16 additions & 0 deletions dbTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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);
});
});
60 changes: 31 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
const express = require('express')
const ejsLayouts = require('express-ejs-layouts')
const db = require('./models')
const rowdy = require('rowdy-logger')
const express = require("express");
const ejsLayouts = require("express-ejs-layouts");
const db = require("./models");
const rowdy = require("rowdy-logger");

const app = express()
const PORT = process.env.PORT || 3000
rowdy.begin(app)
const app = express();
const PORT = process.env.PORT || 3000;
rowdy.begin(app);

app.set('view engine', 'ejs')
app.use(require('morgan')('dev'))
app.use(express.urlencoded({ extended: false }))
app.use(ejsLayouts)
app.set("view engine", "ejs");
app.use(require("morgan")("dev"));
app.use(express.urlencoded({ extended: false }));
app.use(ejsLayouts);

app.get('/', (req, res) => {
db.project.findAll()
.then((projects) => {
res.render('main/index', { projects: projects })
})
.catch((error) => {
console.log('Error in GET /', error)
res.status(400).render('main/404')
})
})
app.get("/", (req, res) => {
db.project
.findAll()
.then((projects) => {
res.render("main/index", { projects: projects });
})
.catch((error) => {
console.log("Error in GET /", error);
res.status(400).render("main/404");
});
});

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

app.get('*', (req, res) => {
res.render('main/404')
})
app.get("*", (req, res) => {
res.render("main/404");
});

app.listen(PORT, function() {
rowdy.print()
console.log(`listening on port: ${PORT}`)
})
app.listen(PORT, function () {
rowdy.print();
console.log(`listening on port: ${PORT}`);
});
28 changes: 28 additions & 0 deletions migrations/20230405222904-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/20230405223808-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;
};
27 changes: 27 additions & 0 deletions models/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"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;
};
46 changes: 25 additions & 21 deletions models/project.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
const {
Model
} = require('sequelize');
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class project extends Model {
/**
Expand All @@ -11,24 +9,30 @@ module.exports = (sequelize, DataTypes) => {
*/
static associate(models) {
// define association here
models.project.belongsToMany(models.category, {
through: "categoriesProjects",
});
}
}
project.init({
githubLink: {
type: DataTypes.TEXT,
validate: { isUrl: true }
project.init(
{
githubLink: {
type: DataTypes.TEXT,
validate: { isUrl: true },
},
name: DataTypes.STRING,
deployLink: {
type: DataTypes.TEXT,
validate: { isUrl: true },
},
description: {
type: DataTypes.TEXT,
},
},
name: DataTypes.STRING,
deployLink: {
type: DataTypes.TEXT,
validate: { isUrl: true }
},
description: {
type: DataTypes.TEXT
},
}, {
sequelize,
modelName: 'project',
});
{
sequelize,
modelName: "project",
}
);
return project;
};
};
Loading