Skip to content

Commit

Permalink
done with review post and get
Browse files Browse the repository at this point in the history
  • Loading branch information
makon57 committed Jul 1, 2021
1 parent c727433 commit 12d7d25
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 5 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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');


Expand Down Expand Up @@ -41,10 +42,11 @@ app.use(
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('/login', loginRouter);
app.use('/signup', signupRouter);
// app.use('/trails', trailsRouter);

// catch 404 and forward to error handler
Expand All @@ -57,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
1 change: 1 addition & 0 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const restoreUser = async (req, res, next) => {
if (user) {
res.locals.authenticated = true;
res.locals.user = user;
// console.log(res.locals.user)
next();
}
} catch (err) {
Expand Down
15 changes: 8 additions & 7 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const getUserFromSession = async req => {

router.get('/', csrfProtection, asyncHandler(async(req, res) => {
const trails = await db.Trail.findAll();
const user = await getUserFromSession(req)
const user = await getUserFromSession(req);
res.render('index', ({ trails, user }));
}));


router.get('/trail/:id(\\d+)', csrfProtection, asyncHandler(async(req, res, next) => {
router.get('/trail/:id(\\d+)', asyncHandler(async(req, res, next) => {
const trailId = parseInt(req.params.id, 10);
const trail = await db.Trail.findByPk(trailId);
const reviews = await db.Review.findAll({ where: trailId, include: "User" })
const reviews = await db.Review.findAll({ where: { trailId }, include: "User" })
res.render('trail-detail', {
title: "Park Detail",
trail,
Expand All @@ -40,11 +40,12 @@ router.get('/trail/:id(\\d+)', csrfProtection, asyncHandler(async(req, res, next
}));


router.post('/trail/:id(\\d+)', csrfProtection, asyncHandler(async(req, res, next) => {
router.post('/trail/:id(\\d+)', asyncHandler(async(req, res, next) => {
const { text } = req.body;
const newReview = await Review.build({ text, userId: req.user.id, trailId:req.trail.id, createdAt: req.review.createdAt });
await newReview.save();
res.redirect('/trail/:id(\\d+)');
const trailId = req.params.id;
const user = req.session.auth.userId
await db.Review.create({ text, userId: user, trailId, rating: 4, createdAt: new Date() });
res.redirect(`/trail/${trailId}`);
}));

// router.put('/:id(\\d+)', validateTweet, handleValidationErrors,asyncHandler(async(req, res, next) => {
Expand Down

0 comments on commit 12d7d25

Please sign in to comment.