0% found this document useful (0 votes)
29 views2 pages

Table and Server Management API

Uploaded by

crazy bhl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Table and Server Management API

Uploaded by

crazy bhl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Route to add a new table

[Link]('/table', async (req, res) => {


const { Table_Number } = [Link];

if (!Table_Number) {
return [Link](400).json({ error: 'Table_Number is required.' });
}

try {
const newTable = new Single_Table({ Table_Number });
await [Link]();
[Link](201).send('Table added successfully');
} catch (err) {
[Link](500).json({ error: 'Failed to add table', details:
[Link] });
}
});

[Link](`/table`, async (req, res) => {


try {
const items = await Single_Table.find();
[Link](200).json(items);
} catch (error) {
[Link](500).json({ error: 'An error occurred while fetching the
tables.' });
}
});

// Route to get item by ID


[Link]('/table/:id', async (req, res) => {
try {
const { id } = [Link];

if (![Link](id)) {
return [Link](400).json({ error: 'Invalid ID format.' });
}
const item = await Single_Table.findById(id);
if (!item) {
return [Link](404).json({ error: 'Table not found.' });
}
[Link](200).json(item);
} catch (error) {
[Link](500).json({ error: 'An error occurred while fetching the table.'
});
}
});

// Route to delete item


[Link]('/table/:id', async (req, res) => {
try {
const { id } = [Link];

// Check if the provided ID is valid


if (![Link](id)) {
return [Link](400).json({ error: 'Invalid ID format.' });
}

// Attempt to find and delete the table entry


const result = await Single_Table.findByIdAndDelete(id);

if (!result) {
return [Link](404).json({ message: 'Table not found' });
}
return [Link](200).json({ message: 'Table deleted successfully' });
} catch (error) {
[Link](500).send({ message: [Link] });
}
});

[Link]('/servers/:id', async (req, res) => {


try {
const { Name, Last_Name, Adresse, Cin, Salary, Shift, Ratings,
Tables_Under_Control } = [Link]; // Extract data from request body
const { id } = [Link]; // Extract id from request params

if (![Link](id)) {
return [Link](400).json({ error: 'Invalid ID.' });
}

const server = await [Link](id); // Use 'server' variable for


the found document
if (!server) {
return [Link](404).json({ message: 'Server not found' });
}

// Update the server fields


[Link] = Name;
server.Last_Name = Last_Name;
[Link] = Adresse;
[Link] = Cin;
[Link] = Salary;
[Link] = Shift;
[Link] = Ratings;
server.Tables_Under_Control = Tables_Under_Control; // Ensure it's handled
as an array

const result = await [Link](); // Save the updated server


return [Link](200).json({ message: 'Server updated successfully', data:
result });
} catch (error) {
[Link](500).send({ message: [Link] });
}
});

You might also like