97 lines
2.4 KiB
JavaScript
97 lines
2.4 KiB
JavaScript
import Endpoint from "../models/Endpoint.js";
|
|
|
|
export const createEndpoint = async (req, res, next) => {
|
|
const newEndpoint = new Endpoint({
|
|
name: req.body.name,
|
|
endpoint: req.body.endpoint,
|
|
method: req.body.method,
|
|
collection_id: req.body.collection_id,
|
|
created_by: req.body.created_by,
|
|
body_json: req.body.body_json,
|
|
form_data: req.body.form_data,
|
|
header: req.body.header,
|
|
params: req.body.params,
|
|
});
|
|
console.log(req.body);
|
|
try {
|
|
const savedEndpoint = await newEndpoint.save();
|
|
res.status(200).json(savedEndpoint);
|
|
console.log(savedEndpoint);
|
|
} catch (err) {
|
|
// console.log(req.body);
|
|
|
|
res.status(400).json({ message: err.message });
|
|
|
|
next(err);
|
|
}
|
|
};
|
|
|
|
export const updateEndpoint = async (req, res, next) => {
|
|
try {
|
|
console.log(req.params.id);
|
|
const updatedEndpoint = await Endpoint.findByIdAndUpdate(
|
|
req.params.id,
|
|
{ $set: req.body },
|
|
{ new: true }
|
|
);
|
|
if (updatedEndpoint) {
|
|
res.status(200).json(updatedEndpoint);
|
|
} else {
|
|
res.status(200).json("Endpoint does not exist.");
|
|
}
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
};
|
|
|
|
export const deleteEndpoint = async (req, res, next) => {
|
|
try {
|
|
const getEndpoint = await Endpoint.findByIdAndDelete(req.params.id);
|
|
if (getEndpoint) {
|
|
res.status(200).json("Endpoint has been deleted.");
|
|
} else {
|
|
res.status(404).json("Endpoint does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(404).json({ message: err.message });
|
|
|
|
next(err);
|
|
}
|
|
};
|
|
export const getEndpoints = async (req, res, next) => {
|
|
try {
|
|
const getEndpoint = await Endpoint.find();
|
|
// const Endpoint = getEndpoint.reverse();
|
|
if (getEndpoint) {
|
|
res.status(200).json(getEndpoint);
|
|
} else {
|
|
res.status(404).json("Endpoint does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(404).json({ message: err.message });
|
|
|
|
next(err);
|
|
}
|
|
};
|
|
export const getEndpoint = async (req, res, next) => {
|
|
try {
|
|
const foundEndpoint = await Endpoint.findById(req.params.id)
|
|
.populate({
|
|
path: "userRef",
|
|
model: "User",
|
|
// select: "fName lName username photo",
|
|
})
|
|
|
|
.exec();
|
|
|
|
if (foundEndpoint) {
|
|
res.status(200).json(foundEndpoint);
|
|
} else {
|
|
res.status(404).json("Endpoint does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(500).json({ message: err.message });
|
|
next(err);
|
|
}
|
|
};
|