96 lines
2.4 KiB
JavaScript
96 lines
2.4 KiB
JavaScript
import Collection from "../models/Collection.js";
|
|
|
|
export const createCollection = async (req, res, next) => {
|
|
const newCollection = new Collection({
|
|
full_name: req.body.full_name,
|
|
date_of_birth: req.body.date_of_birth,
|
|
userRef: req.body.userRef,
|
|
id_num: req.body.id_num,
|
|
address: req.body.address,
|
|
images: req.body.images,
|
|
selfie: req.body.selfie,
|
|
status: req.body.status,
|
|
});
|
|
console.log(req.body);
|
|
try {
|
|
const savedCollection = await newCollection.save();
|
|
res.status(200).json(savedCollection);
|
|
console.log(savedCollection);
|
|
} catch (err) {
|
|
// console.log(req.body);
|
|
|
|
res.status(400).json({ message: err.message });
|
|
|
|
next(err);
|
|
}
|
|
};
|
|
|
|
export const updateCollection = async (req, res, next) => {
|
|
try {
|
|
console.log(req.params.id);
|
|
const updatedCollection = await Collection.findByIdAndUpdate(
|
|
req.params.id,
|
|
{ $set: req.body },
|
|
{ new: true }
|
|
);
|
|
if (updatedCollection) {
|
|
res.status(200).json(updatedCollection);
|
|
} else {
|
|
res.status(200).json("Collection does not exist.");
|
|
}
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
};
|
|
|
|
export const deleteCollection = async (req, res, next) => {
|
|
try {
|
|
const getCollection = await Collection.findByIdAndDelete(req.params.id);
|
|
if (getCollection) {
|
|
res.status(200).json("Collection has been deleted.");
|
|
} else {
|
|
res.status(404).json("Collection does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(404).json({ message: err.message });
|
|
|
|
next(err);
|
|
}
|
|
};
|
|
export const getCollections = async (req, res, next) => {
|
|
try {
|
|
const getCollection = await Collection.find();
|
|
// const Collection = getCollection.reverse();
|
|
if (getCollection) {
|
|
res.status(200).json(getCollection);
|
|
} else {
|
|
res.status(404).json("Collection does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(404).json({ message: err.message });
|
|
|
|
next(err);
|
|
}
|
|
};
|
|
export const getCollection = async (req, res, next) => {
|
|
try {
|
|
const foundCollection = await Collection.findById(req.params.id)
|
|
.populate({
|
|
path: "userRef",
|
|
model: "User",
|
|
// select: "fName lName username photo",
|
|
})
|
|
|
|
.exec();
|
|
|
|
if (foundCollection) {
|
|
res.status(200).json(foundCollection);
|
|
} else {
|
|
res.status(404).json("Collection does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(500).json({ message: err.message });
|
|
next(err);
|
|
}
|
|
};
|