106 lines
2.6 KiB
JavaScript
106 lines
2.6 KiB
JavaScript
import KDSModel from "../models/KDS.js";
|
|
|
|
// Resolver to create a new KDS document
|
|
export const createKDS = async (req, res) => {
|
|
try {
|
|
const kdsData = req.body;
|
|
const { ref_id } = kdsData;
|
|
|
|
// Check if a KDS document with the same ref_id already exists
|
|
const existingKDS = await KDSModel.findOne({ ref_id });
|
|
|
|
if (existingKDS) {
|
|
return res.status(400).json({ error: `KDS with ref_id ${ref_id} already exists.` });
|
|
}
|
|
|
|
// Proceed to create the new KDS document if ref_id is unique
|
|
const newKDS = new KDSModel(kdsData);
|
|
await newKDS.save();
|
|
|
|
res.status(201).json(newKDS);
|
|
} catch (error) {
|
|
res.status(400).json({ error: `Failed to create KDS document: ${error.message}` });
|
|
}
|
|
};
|
|
|
|
|
|
// Resolver to get all KDS documents
|
|
export const getAllKDS = async (req, res) => {
|
|
try {
|
|
const kdsList = await KDSModel.find();
|
|
res.status(200).json(kdsList);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Failed to retrieve KDS documents" });
|
|
}
|
|
};
|
|
export const getKDS = async (req, res, next) => {
|
|
try {
|
|
const foundKDS = await KDSModel.findById(req.params.id)
|
|
.populate({
|
|
path: "userRef",
|
|
model: "User",
|
|
// select: "fName lName username photo",
|
|
})
|
|
|
|
.exec();
|
|
|
|
if (foundKDS) {
|
|
res.status(200).json(foundKDS);
|
|
} else {
|
|
res.status(404).json("KDS does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(500).json({ message: err.message });
|
|
next(err);
|
|
}
|
|
};
|
|
|
|
export const updateKDS = async (req, res, next) => {
|
|
try {
|
|
console.log(req.params.id);
|
|
const updatedKDS = await KDSModel.findByIdAndUpdate(
|
|
req.params.id,
|
|
{ $set: req.body },
|
|
{ new: true }
|
|
);
|
|
if (updatedKDS) {
|
|
res.status(200).json(updatedKDS);
|
|
} else {
|
|
res.status(404).json("KDS does not exist.");
|
|
}
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
};
|
|
export const updateKDSrow = async (req, res, next) => {
|
|
try {
|
|
console.log(req.params.id);
|
|
const updatedKDS = await KDSModel.findByIdAndUpdate(
|
|
req.params.id,
|
|
{ $set: req.body },
|
|
{ new: true }
|
|
);
|
|
if (updatedKDS) {
|
|
res.status(200).json(updatedKDS);
|
|
} else {
|
|
res.status(404).json("KDS does not exist.");
|
|
}
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
};
|
|
export const deleteKDS = async (req, res, next) => {
|
|
try {
|
|
const getKDS = await KDSModel.findByIdAndDelete(req.params.id);
|
|
if (getKDS) {
|
|
res.status(200).json("KDS has been deleted.");
|
|
} else {
|
|
res.status(404).json("KDS does not exist.");
|
|
}
|
|
} catch (err) {
|
|
res.status(404).json({ message: err.message });
|
|
|
|
next(err);
|
|
}
|
|
};
|