From 45fa3cfdb33d407f9d46438add96a893bb6b28c6 Mon Sep 17 00:00:00 2001 From: AugustusMadden Date: Thu, 28 Apr 2022 20:44:26 -0400 Subject: [PATCH 1/3] Bid view connected to models --- controllers/index.js | 17 +++++++++++++++++ views/cars/show.hbs | 7 ++++++- views/partials/bid_details.hbs | 17 ++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/controllers/index.js b/controllers/index.js index 5aae779..40c75a0 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -42,6 +42,23 @@ router.get('/create', (req, res) => { }) +router.get('/bid', async (req, res) => { + try { + const user = req.session.user || null + const carData = await Car.findAll(); + const cars = carData.map((car) => car.get({ plain: true })); + + res.render('partials/bid_details', { + user: user, + cars + }); + + } catch (err) { + res.status(500).json(err); + } + +}) + // TODO: Create these authentication views router.get('/login', (req, res) => res.render('auth/login')); router.get('/signup', (req, res) => res.render('auth/signup')); diff --git a/views/cars/show.hbs b/views/cars/show.hbs index 03ae06a..bb43d0c 100644 --- a/views/cars/show.hbs +++ b/views/cars/show.hbs @@ -21,4 +21,9 @@ {{#each car.bids as |bid|}}
  • {{bid.price}} - {{bid.user.username}}
  • {{/each}} - \ No newline at end of file + + + +
    + +
    \ No newline at end of file diff --git a/views/partials/bid_details.hbs b/views/partials/bid_details.hbs index 6181918..ad1788c 100644 --- a/views/partials/bid_details.hbs +++ b/views/partials/bid_details.hbs @@ -1,6 +1,17 @@ -
    -
    {{bid.price}}
    -

    {{bid.user_id}}

    +
    +
    + + +
    +
    + +
    +
      + {{#each cars as |car|}} +
    • {{car.manufacturer}}
    • + {{/each}} +
    +
    \ No newline at end of file From ab699f749c8fe5c7948108a853d9a5f8dca970d8 Mon Sep 17 00:00:00 2001 From: AugustusMadden Date: Thu, 28 Apr 2022 21:04:12 -0400 Subject: [PATCH 2/3] Added car listing to view --- views/partials/bid_details.hbs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/views/partials/bid_details.hbs b/views/partials/bid_details.hbs index ad1788c..36b0612 100644 --- a/views/partials/bid_details.hbs +++ b/views/partials/bid_details.hbs @@ -1,5 +1,13 @@
    +
    + + +
    @@ -8,9 +16,7 @@
      - {{#each cars as |car|}} -
    • {{car.manufacturer}}
    • - {{/each}} +
    From 617423c62b0ac085a6a79a9de07e2d770e09f645 Mon Sep 17 00:00:00 2001 From: AugustusMadden Date: Thu, 28 Apr 2022 23:13:10 -0400 Subject: [PATCH 3/3] Routing and authentication --- controllers/api/bidRoutes.js | 5 ++++- controllers/api/carRoutes.js | 10 ++++++---- controllers/bids.js | 23 +++++++++++++++++++++++ controllers/index.js | 17 ++--------------- views/cars/show.hbs | 2 +- views/partials/bid_details.hbs | 21 ++++++++------------- 6 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 controllers/bids.js diff --git a/controllers/api/bidRoutes.js b/controllers/api/bidRoutes.js index a843189..a9f835e 100644 --- a/controllers/api/bidRoutes.js +++ b/controllers/api/bidRoutes.js @@ -1,5 +1,7 @@ const router = require('express').Router(); const { Car, Bid, User } = require('../../models'); +const { authenticated } = require('../../helpers/middleware') + router.get("/", async (req, res) => { try { @@ -25,8 +27,9 @@ router.get("/:id", async (req, res) => { }); -router.post('/', async (req, res) => { +router.post('/', authenticated, async (req, res) => { try { + console.log(req.body) const newBid = await Bid.create({ ...req.body, user_id: req.session.user_id, diff --git a/controllers/api/carRoutes.js b/controllers/api/carRoutes.js index 60569e6..c9f80ca 100644 --- a/controllers/api/carRoutes.js +++ b/controllers/api/carRoutes.js @@ -7,7 +7,7 @@ const { authenticated } = require('../../helpers/middleware') const upload = multer({ dest: 'uploads/' }) -router.post('/', upload.single('uploaded_file'), async (req, res) => { +router.post('/', [authenticated, upload.single('uploaded_file')], async (req, res) => { try { if (req.body.file) { @@ -49,10 +49,11 @@ router.post('/', upload.single('uploaded_file'), async (req, res) => { color: req.body.color, ending_date: req.body.ending_date, description: req.body.description, - file_path: fileURL.url + file_path: fileURL.url, + user_id: req.session.user.id }) } else { - console.log("HEEEEEEEEEEEEEEEEY"); + console.log(req.body); const carData = await Car.create({ manufacturer: req.body.manufacturer, @@ -61,7 +62,8 @@ router.post('/', upload.single('uploaded_file'), async (req, res) => { mileage: req.body.mileage, color: req.body.color, ending_date: req.body.ending_date, - description: req.body.description + description: req.body.description, + user_id: req.session.user.id }) console.log(carData); res.status(200).json(carData); diff --git a/controllers/bids.js b/controllers/bids.js new file mode 100644 index 0000000..1475ad9 --- /dev/null +++ b/controllers/bids.js @@ -0,0 +1,23 @@ +const express = require('express') +const { Bid, Car, User } = require('../models') + +const router = express.Router() + +router.get('/', async (req, res) => { + try { + const user = req.session.user || null + const carData = await Car.findAll(); + const cars = carData.map((car) => car.get({ plain: true })); + + res.render('partials/bid_details', { + user: user, + cars + }); + + } catch (err) { + res.status(500).json(err); + } + + }) + +module.exports = router \ No newline at end of file diff --git a/controllers/index.js b/controllers/index.js index 40c75a0..bc9d7df 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -2,6 +2,7 @@ const express = require('express') const auth_routes = require('./auth_routes') const apiRoutes = require('./api'); const cars = require('./cars') +const bids = require('./bids') const Car = require('../models/Car') const router = express.Router(); @@ -10,6 +11,7 @@ router.use('/api', apiRoutes); router.use('/users', auth_routes); router.use('/cars', cars) +router.use('/bids', bids) router.get('/', async (req, res) => { @@ -42,22 +44,7 @@ router.get('/create', (req, res) => { }) -router.get('/bid', async (req, res) => { - try { - const user = req.session.user || null - const carData = await Car.findAll(); - const cars = carData.map((car) => car.get({ plain: true })); - res.render('partials/bid_details', { - user: user, - cars - }); - - } catch (err) { - res.status(500).json(err); - } - -}) // TODO: Create these authentication views router.get('/login', (req, res) => res.render('auth/login')); diff --git a/views/cars/show.hbs b/views/cars/show.hbs index bb43d0c..9882774 100644 --- a/views/cars/show.hbs +++ b/views/cars/show.hbs @@ -24,6 +24,6 @@ -
    +
    \ No newline at end of file diff --git a/views/partials/bid_details.hbs b/views/partials/bid_details.hbs index 36b0612..8a395c0 100644 --- a/views/partials/bid_details.hbs +++ b/views/partials/bid_details.hbs @@ -1,23 +1,18 @@ - -
    -
    - - +
    +
    + +
    -
    - - -
    -
      - -
    - \ No newline at end of file