Commit 8fa5ad73 authored by Arthur.Pautrel's avatar Arthur.Pautrel
Browse files

Finished bonus exercise TPLAB

parent c83fc9be
var mongoose = require('mongoose');
var priceSchema = new mongoose.Schema({
product: {
type: mongoose.Schema.Types.ObjectId,
ref: "Products",
required: true
},
price: Number,
source: String
}, {
timestamps: true,
});
var Prices = mongoose.model('Prices', priceSchema);
module.exports = Prices;
\ No newline at end of file
......@@ -20,10 +20,10 @@ router.put('/update', auth.ensureSignedIn, async (req, res, next) => {
res.json(result);
})
router.delete('/delete-user', auth.ensureSignedIn, async (req, res, next) => {
router.delete('/delete', auth.ensureSignedIn, async (req, res, next) => {
const { id } = req.body;
const deletedUser = await categoryService.remove(id);
res.json(deletedUser);
const deletedCategory = await categoryService.remove(id);
res.json(deletedCategory);
})
router.get('categorized-items', async (req, res) => {
......
......@@ -6,5 +6,6 @@ router.use('/user', require('./user'));
router.use('/category', require('./category'));
router.use('/item', require('./item'));
router.use('/product', require('./product'));
router.use('/price', require('./price'));
module.exports = router;
\ No newline at end of file
......@@ -20,10 +20,10 @@ router.put('/update', auth.ensureSignedIn, async (req, res, next) => {
res.json(result);
})
router.delete('/delete-user', auth.ensureSignedIn, async (req, res, next) => {
router.delete('/delete', auth.ensureSignedIn, async (req, res, next) => {
const { id } = req.body;
const deletedUser = await itemService.remove(id);
res.json(deletedUser);
const deletedItem = await itemService.remove(id);
res.json(deletedItem);
})
router.get('/:id', auth.ensureSignedIn, async (req, res) => {
......
const express = require('express');
const auth = require("../middlewares/auth");
const priceService = require("../services/price");
var router = express.Router();
router.post('/create', auth.ensureSignedIn, async (req, res) => {
const { product, price, source } = req.body;
const result = await priceService.create(product, price, source);
res.json(result);
})
router.get('/all', async (req, res) => {
const prices = await priceService.findAll();
res.json(prices);
})
router.put('/update', auth.ensureSignedIn, async (req, res, next) => {
const { _id, product, price, source } = req.body;
const result = await priceService.update(_id, product, price, source);
res.json(result);
})
router.delete('/delete', auth.ensureSignedIn, async (req, res, next) => {
const { id } = req.body;
const deletedPrice = await priceService.remove(id);
res.json(deletedPrice);
})
router.get('categorized-items', async (req, res) => {
const result = await priceService.findCategorizedItems();
res.json(result);
})
router.get('/:id', auth.ensureSignedIn, async (req, res) => {
const { id } = req.params;
const result = await priceService.findById(id);
res.json(result);
})
module.exports = router;
\ No newline at end of file
......@@ -20,10 +20,10 @@ router.put('/update', auth.ensureSignedIn, async (req, res, next) => {
res.json(result);
})
router.delete('/delete-user', auth.ensureSignedIn, async (req, res, next) => {
router.delete('/delete', auth.ensureSignedIn, async (req, res, next) => {
const { id } = req.body;
const deletedUser = await productService.remove(id);
res.json(deletedUser);
const deletedProduct = await productService.remove(id);
res.json(deletedProduct);
})
router.get('/:id', auth.ensureSignedIn, async (req, res) => {
......
......@@ -54,7 +54,7 @@ const update = async (id, name, desc, imageUrl) => {
if (updateCategory) {
return {success: true, data: updateCategory};
} else {
return {sucess: false, error: "Failed to change username"};
return {sucess: false, error: "Failed to update"};
}
} catch (err) {
return {sucess: false, error: err};
......
......@@ -54,7 +54,7 @@ const update = async (id, name, desc, category) => {
if (updateItem) {
return {success: true, data: updateItem};
} else {
return {sucess: false, error: "Failed to change username"};
return {sucess: false, error: "Failed to update"};
}
} catch (err) {
return {sucess: false, error: err};
......
const Prices = require("../models/price");
const findById = async (id) => {
try {
const price = await Prices.findById(id);
if (price) {
return {success: true, data: price};
} else {
return {sucess: false, error: "Price's id does not exist"};
}
} catch (err) {
return {sucess: false, error: err};
}
}
const findAll = async () => {
try {
const prices = await Prices.find();
if (prices) {
return {success: true, data: prices};
} else {
return {sucess: false, error: "No prices found"};
}
} catch (err) {
return {sucess: false, error: err};
}
}
const create = async (product, price, source) => {
try {
const newPrice = {
product,
price,
source
}
const createdPrice = await Prices.create(newPrice);
return {
success: true,
data: createdPrice
}
} catch (err) {
return {
success: false,
error: err
}
}
}
const update = async (id, product, price, source) => {
try {
const updatePrice = await Prices.updateOne({"_id": id}, {$set: {"product": product, "price": price, "source": source}});
if (updatePrice) {
return {success: true, data: updatePrice};
} else {
return {sucess: false, error: "Failed to update"};
}
} catch (err) {
return {sucess: false, error: err};
}
}
const remove = async (id) => {
try {
const retDelete = await Prices.deleteOne({"_id": id});
if (retDelete) {
return {success: true, data: retDelete};
} else {
return {sucess: false, error: "Price's id does not exist"};
}
} catch (err) {
return {sucess: false, error: err};
}
}
module.exports = {
findById,
findAll,
create,
update,
remove
}
\ No newline at end of file
......@@ -56,7 +56,7 @@ const update = async (id, title, item, user, imageUrl, desc) => {
if (updateProduct) {
return {success: true, data: updateProduct};
} else {
return {sucess: false, error: "Failed to change username"};
return {sucess: false, error: "Failed to update"};
}
} catch (err) {
return {sucess: false, error: err};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment