Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
304CEM_resit_backend/api/controllers/items.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
120 lines (116 sloc)
3.82 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Item = require('../models/item'); | |
const User = require('../models/user'); | |
const mongoose = require('mongoose'); | |
const jwt = require('jsonwebtoken'); | |
//On Get request, return all item objects in the database | |
exports.getAllItems = (req, res, next) => { | |
Item.find() | |
//Without the __v argument as it has no use to the app | |
.select('-__v') | |
.exec() | |
.then(docs => { | |
const response = { | |
items: docs.map(doc => { | |
return { | |
_id: doc._id, | |
name: doc.name, | |
publisher: doc.publisher, | |
category: doc.category, | |
features: doc.features, | |
condition: doc.condition, | |
askPrice: doc.askPrice, | |
location: doc.location, | |
picture: doc.pictures, | |
//with each item, also return a request link to the individual item for more information | |
request: { | |
type: 'GET', | |
url: 'https://caracold-304resit-api.herokuapp.com/itemlist/' + doc._id | |
} | |
} | |
}) | |
} | |
res.status(200).json(response); | |
}) | |
.catch(err => { | |
console.log(err); | |
res.status(500).json({ | |
error: err | |
}); | |
}); | |
} | |
//Handle post request to create a new item in the database | |
exports.newItem = (req, res, next) => { | |
//The poster's id is obtained from the token and stored in the databse along with the item | |
const decoded = jwt.decode(req.header.token); | |
const userId = decoded.username; | |
const item = new Item({ | |
_id: new mongoose.Types.ObjectId(), | |
name: req.body.name, | |
publisher: userId, | |
category: req.body.category, | |
features: req.body.features, | |
condition: req.body.condition, | |
askPrice: req.body.askPrice, | |
location: req.body.location, | |
picture: req.file.path | |
}) | |
item.save() | |
.then(result => { | |
console.log(result); | |
res.status(201).json({ | |
message: 'Item successfully created', | |
newItem: { | |
_id: result._id, | |
name: result.name, | |
publisher: result.publisher, | |
category: result.category, | |
features: result.features, | |
condition: result.condition, | |
askPrice: result.askPrice, | |
location: result.location, | |
picture: result.picture | |
} | |
}); | |
}) | |
.catch(err => { | |
console.log(err); | |
res.status(500).json({ | |
error: err | |
}); | |
}); | |
} | |
//Handles Get request for specific item by id | |
exports.getItem = (req, res, next) => { | |
const id = req.params.itemId; | |
Item.findById(id) | |
.exec() | |
.then(doc => { | |
console.log(doc); | |
if (doc) { | |
res.status(200).json(doc); | |
} else { | |
res.status(404).json({ | |
message: 'Object ID not found.' | |
}) | |
} | |
}) | |
.catch(err => { | |
console.log(err); | |
res.status(500).json({ error: err }); | |
}) | |
} | |
//Handles delete request to remove specified item from the database | |
exports.deleteItem = (req, res, next) => { | |
const id = req.params.itemId; | |
Item.remove({ _id: id }) | |
.exec() | |
.then(result => { | |
res.status(200).json({ message: 'Item successfully removed' }) | |
}) | |
.catch(err => { | |
console.log(err); | |
res.status(500).json({ | |
error: err | |
}); | |
}); | |
} |