Skip to content

Commit

Permalink
Merge pull request #4 from makon57/finishings
Browse files Browse the repository at this point in the history
Finishings
  • Loading branch information
lemlooma authored Jul 2, 2021
2 parents 27b9bfd + fbc07d1 commit 1d28a43
Show file tree
Hide file tree
Showing 102 changed files with 6,576 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules/
8 changes: 8 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = {
'config': path.resolve('config', 'database.js'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations')
};
12 changes: 12 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const session = require('express-session');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const loginRouter = require('./routes/login');
// const { asyncHandler } = require('./utils');
const signupRouter = require('./routes/signup');
const { restoreUser } = require('./auth')
// const trailsRouter = require('./routes/trails');


const app = express();

Expand Down Expand Up @@ -35,8 +41,13 @@ app.use(
// create Session table if it doesn't already exist
store.sync();

app.use(express.static('images'));
app.use(restoreUser);
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/login', loginRouter);
app.use('/signup', signupRouter);
// app.use('/trails', trailsRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
Expand All @@ -48,6 +59,7 @@ app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
console.log(res.locals.message)

// render the error page
res.status(err.status || 500);
Expand Down
53 changes: 53 additions & 0 deletions auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

const db = require('./db/models');

const loginUser = (req, res, user) => {
req.session.auth = {
userId: user.id,
};
};

const logoutUser = (req, res) => {
delete req.session.auth;
};

const requireAuth = (req, res, next) => {
if (!res.locals.authenticated) {
return res.redirect('/login');
}
return next();
};

const restoreUser = async (req, res, next) => {
// Log the session object to the console
// to assist with debugging.
console.log(req.session);

if (req.session.auth) {
const { userId } = req.session.auth;

try {
const user = await db.User.findByPk(userId);

if (user) {
res.locals.authenticated = true;
res.locals.user = user;
// console.log(res.locals.user)
next();
}
} catch (err) {
res.locals.authenticated = false;
next(err);
}
} else {
res.locals.authenticated = false;
next();
}
};

module.exports = {
loginUser,
logoutUser,
requireAuth,
restoreUser,
};
11 changes: 11 additions & 0 deletions config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ module.exports = {
dialect: 'postgres',
seederStorage: 'sequelize',
},
production: {
use_env_variable: 'DATABASE_URL',
dialect: 'postgres',
seederStorage: 'sequelize',
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
},
};
Binary file modified db/.DS_Store
Binary file not shown.
38 changes: 38 additions & 0 deletions db/migrations/20210628202037-create-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
unique: true,
allowNull: false,
type: Sequelize.STRING(50)
},
email: {
unique: true,
allowNull: false,
type: Sequelize.STRING(255)
},
password: {
allowNull: false,
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};
34 changes: 34 additions & 0 deletions db/migrations/20210628202553-create-trail-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('TrailLists', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
unique: true,
allowNull: false,
type: Sequelize.STRING(255)
},
userId: {
references: { model: "Users" },
allowNull: false,
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('TrailLists');
}
};
34 changes: 34 additions & 0 deletions db/migrations/20210628202553-create-trailList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('TrailLists', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
unique: true,
allowNull: false,
type: Sequelize.STRING(255)
},
userId: {
references: { model: "Users" },
allowNull: false,
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('TrailLists');
}
};
38 changes: 38 additions & 0 deletions db/migrations/20210628202815-create-trail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Trails', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
unique:true,
allowNull: false,

type: Sequelize.STRING(50)
},
description: {
allowNull: false,
type: Sequelize.TEXT
},
state: {
allowNull: false,
type: Sequelize.STRING(50)
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Trails');
}
};
40 changes: 40 additions & 0 deletions db/migrations/20210628202816-create-joins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Joins', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
trailId: {
allowNull: false,
references: { model: "Trails" },
type: Sequelize.INTEGER
},
trailListId: {
allowNull: false,
references: { model: "TrailLists" },
type: Sequelize.INTEGER
},
hasVisited: {
type: Sequelize.BOOLEAN
},
wantToVisit: {
type: Sequelize.BOOLEAN
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Joins');
}
};
42 changes: 42 additions & 0 deletions db/migrations/20210628203040-create-review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Reviews', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
trailId: {
allowNull: false,
references: { model: "Trails" },
type: Sequelize.INTEGER
},
userId: {
references: { model: "Users" },
allowNull: false,
type: Sequelize.INTEGER
},
rating: {
allowNull: false,
type: Sequelize.INTEGER
},
text: {
allowNull: false,
type: Sequelize.TEXT
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Reviews');
}
};
14 changes: 14 additions & 0 deletions db/models/join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Join = sequelize.define('Join', {
trailId: DataTypes.INTEGER,
trailShelfId: DataTypes.INTEGER,
hasVisited: DataTypes.BOOLEAN,
wantToVisit: DataTypes.BOOLEAN
}, {});
Join.associate = function(models) {
// Join.belongsTo(models.TrailList, {foreignKey: 'trailListId'})
// Join.hasMany(models.Trail, {foreignKey:'trailId'})
};
return Join;
};
14 changes: 14 additions & 0 deletions db/models/review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Review = sequelize.define('Review', {
trailId: DataTypes.INTEGER,
userId: DataTypes.INTEGER,
rating: DataTypes.INTEGER,
text: DataTypes.TEXT
}, {});
Review.associate = function(models) {
Review.belongsTo(models.User,{foreignKey:'userId'})
Review.belongsTo(models.Trail, {foreignKey: 'trailId'})
};
return Review;
};
Loading

0 comments on commit 1d28a43

Please sign in to comment.