Skip to content

Commit

Permalink
Merge pull request #28 from Blueprint-Boulder/27-update-vendor-route
Browse files Browse the repository at this point in the history
update vendor route added
  • Loading branch information
nh602 authored Nov 16, 2023
2 parents 7a11e85 + 3d59c1b commit 5eda16c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
56 changes: 55 additions & 1 deletion backend/controllers/VendorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,58 @@ const createVendor = async (req, res, next) => {
next();
};

module.exports = {getVendor, getVendors, createVendor, getVendorById, authenticateVendor};
const updateVendor = async (req, res, next) => {
const {vendorId} = req.params;
const {
name,
email,
phone_number,
password,
website,
} = req.body;

// Checks if the required fields are present
if (!password || !email || !name) {
console.log(req.body);
return res.status(400).json({error: 'Missing required fields'});
}

// Hashes the password using bcrypt
let passwordHash;
try {
const salt = await genSalt(10);
passwordHash = await hash(password, salt);
} catch (err) {
console.log(err);
res.status(500).json({error: err});
return;
}

try {
await db.none(
'UPDATE Vendors SET \
name = $1, \
email = $2, \
phone_number = $3, \
password = $4, \
website = $5 \
WHERE vendor_id = $6',
[name, email, phone_number, passwordHash, website, vendorId],
);
} catch (err) {
// Duplicate emails are not allowed
if (err.code === '23505') {
res.status(400).json({error: 'A vendor with that email already exists'});
return;
}

// Other internal error
console.log(err);
res.status(500).json({error: err});
return;
}

next();
};

module.exports = {getVendor, getVendors, createVendor, getVendorById, authenticateVendor, updateVendor};
3 changes: 3 additions & 0 deletions backend/routes/VendorRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
createVendor,
getVendorById,
authenticateVendor,
updateVendor,
} = require('../controllers/VendorController');
const sendSuccessResponse = require('../middleware/successResponse');

Expand All @@ -23,4 +24,6 @@ router.post('/', createVendor, (req, res) => {
res.status(200).json({status: 'success'});
});

router.put('/:vendorId', updateVendor, sendSuccessResponse);

module.exports = router;

0 comments on commit 5eda16c

Please sign in to comment.