115 lines
2.8 KiB
JavaScript
115 lines
2.8 KiB
JavaScript
import Collection from "../models/Collection.js";
|
|
|
|
export const createCollection = async (req, res, next) => {
|
|
const newCollection = new Collection({
|
|
name: req.body.name,
|
|
created_by: req.body.created_by,
|
|
header: req.body.header,
|
|
});
|
|
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 updateCollectionByEndpoint = async (req, res, next) => {
|
|
try {
|
|
const { id, endpoint_id } = req.params;
|
|
|
|
const updatedCollection = await Collection.findByIdAndUpdate(
|
|
req.params.id,
|
|
{ $push: { endpoints: req.body.endpoint_id } },
|
|
{ new: true }
|
|
);
|
|
|
|
if (updatedCollection) {
|
|
console.log("Updated Collection:", endpoint_id);
|
|
res.status(200).json(updatedCollection);
|
|
} else {
|
|
res.status(404).json({ endpoint: "Collection or endpoint not found." });
|
|
}
|
|
} 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()
|
|
.populate({
|
|
path: "endpoints",
|
|
model: "Endpoint",
|
|
})
|
|
.exec();
|
|
// 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: "endpoints",
|
|
model: "Endpoint",
|
|
})
|
|
.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);
|
|
}
|
|
};
|