Software: Apache. PHP/8.1.30 uname -a: Linux server1.tuhinhossain.com 5.15.0-163-generic #173-Ubuntu SMP Tue Oct 14 17:51:00 UTC uid=1002(picotech) gid=1003(picotech) groups=1003(picotech),0(root) Safe-mode: OFF (not secure) /home/picotech/domains/note.picotech.app/public_html/src/controllers/ drwxr-xr-x | |
| Viewing file: Select action/file-type: const Label = require('../models/Label');
const { validationResult } = require('express-validator');
// Get all labels for a user
const getLabels = async (req, res) => {
try {
const userId = req.user.id;
const labels = await Label.findByUser(userId);
res.status(200).json({
success: true,
data: labels,
});
} catch (error) {
console.error('Error fetching labels:', error);
res.status(500).json({
success: false,
message: 'Failed to fetch labels',
error: error.message,
});
}
};
// Get a single label by ID
const getLabel = async (req, res) => {
try {
const { id } = req.params;
const userId = req.user.id;
const label = await Label.findOne({
where: {
id,
user_id: userId,
},
});
if (!label) {
return res.status(404).json({
success: false,
message: 'Label not found',
});
}
res.status(200).json({
success: true,
data: label,
});
} catch (error) {
console.error('Error fetching label:', error);
res.status(500).json({
success: false,
message: 'Failed to fetch label',
error: error.message,
});
}
};
// Create a new label
const createLabel = async (req, res) => {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
success: false,
message: 'Validation failed',
errors: errors.array(),
});
}
const { name, color } = req.body;
const userId = req.user.id;
// Check if label name already exists for this user
const existingLabel = await Label.findOne({
where: {
name,
user_id: userId,
},
});
if (existingLabel) {
return res.status(409).json({
success: false,
message: 'Label name already exists',
});
}
const label = await Label.create({
name,
color: color || '#2563eb',
user_id: userId,
});
res.status(201).json({
success: true,
data: label,
message: 'Label created successfully',
});
} catch (error) {
console.error('Error creating label:', error);
res.status(500).json({
success: false,
message: 'Failed to create label',
error: error.message,
});
}
};
// Update a label
const updateLabel = async (req, res) => {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
success: false,
message: 'Validation failed',
errors: errors.array(),
});
}
const { id } = req.params;
const { name, color } = req.body;
const userId = req.user.id;
const label = await Label.findOne({
where: {
id,
user_id: userId,
},
});
if (!label) {
return res.status(404).json({
success: false,
message: 'Label not found',
});
}
// Check if new name conflicts with existing labels (excluding current one)
if (name && name !== label.name) {
const existingLabel = await Label.findOne({
where: {
name,
user_id: userId,
id: { [require('sequelize').Op.ne]: id },
},
});
if (existingLabel) {
return res.status(409).json({
success: false,
message: 'Label name already exists',
});
}
}
// Update fields
if (name) label.name = name;
if (color) label.color = color;
await label.save();
res.status(200).json({
success: true,
data: label,
message: 'Label updated successfully',
});
} catch (error) {
console.error('Error updating label:', error);
res.status(500).json({
success: false,
message: 'Failed to update label',
error: error.message,
});
}
};
// Delete a label
const deleteLabel = async (req, res) => {
try {
const { id } = req.params;
const userId = req.user.id;
const label = await Label.findOne({
where: {
id,
user_id: userId,
},
});
if (!label) {
return res.status(404).json({
success: false,
message: 'Label not found',
});
}
await label.destroy();
res.status(200).json({
success: true,
message: 'Label deleted successfully',
});
} catch (error) {
console.error('Error deleting label:', error);
res.status(500).json({
success: false,
message: 'Failed to delete label',
error: error.message,
});
}
};
module.exports = {
getLabels,
getLabel,
createLabel,
updateLabel,
deleteLabel,
}; |
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0047 ]-- |