Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0824 #46

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

0824 #46

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions backend/controller/reservation/tableCancel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import {searchTableAndUpdate} from '../../model/reserveCancelModel.js';
//import {searchTableAndUpdate} from '../../model/reserveCancelModel.js';
import {cancelTable} from '../../model/reserveCancelModel.js';

function ReqIsNumber(s) {
return parseFloat(s).toString() !== "NaN";
Expand All @@ -18,7 +19,19 @@ export async function tableCancel(req, res) {
return res.status(400).send({ "error": "invalid phone" });
}

const searchandupdate = await searchTableAndUpdate (phoneNum,restaurantId)
// const searchandupdate = await searchTableAndUpdate (phoneNum,restaurantId)

// if(searchandupdate){

// return res.status(200).json({
// data: {
// tableId: searchandupdate,
// }
// });
// }


const searchandupdate = await cancelTable(phoneNum,restaurantId)

if(searchandupdate){

Expand All @@ -29,6 +42,7 @@ export async function tableCancel(req, res) {
});
}


return res.status(404).json({
error: 'Phone not found in tableList.',
});
Expand Down
16 changes: 4 additions & 12 deletions backend/model/orderSummaryModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,13 @@ try {


export async function getTablesByRestId(restaurantId) {
try {
const [rows] = await pool.query('SELECT id, headcount, vacancy, phone FROM tableList WHERE restaurantId = ?', [restaurantId]);
return rows;
} catch (error) {
throw error;
}
const [rows] = await pool.query('SELECT id, headcount, vacancy, phone FROM tableList WHERE restaurantId = ?', [restaurantId]);
return rows;
}

export async function getReservationByRestId(restaurantId) {
try {
const [rows] = await pool.query('SELECT id, phone, headcount FROM Reservation WHERE restaurantId = ?', [restaurantId]);
return rows;
} catch (error) {
throw error;
}
const [rows] = await pool.query('SELECT id, phone, headcount FROM Reservation WHERE restaurantId = ?', [restaurantId]);
return rows;
}


Expand Down
56 changes: 50 additions & 6 deletions backend/model/reserveCancelModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,57 @@ export async function searchTableAndUpdate(phone, restaurantId) {



export async function cancelTable(phone, restaurantId) {
const connection = await pool.getConnection();

try {
await connection.beginTransaction();
const [tableRows] = await connection.query('SELECT id, headcount FROM tableList WHERE phone = ? AND restaurantId = ?', [phone, restaurantId]);

if (tableRows.length > 0) {
const tableId = tableRows[0].id;
const tableHeadcount = tableRows[0].headcount;
console.log(tableId);
const [orderRows] = await connection.query('SELECT id FROM OrderList WHERE restaurantId = ? AND tableId = ?', [restaurantId, tableId]);



if (orderRows.length > 0) {
const orderId = orderRows[0].id;
await connection.query('DELETE FROM OrderList WHERE id = ?', [orderId]);
}
const [reservationRows] = await connection.query('SELECT id, phone FROM Reservation WHERE headcount <= ? AND restaurantId = ? LIMIT 1', [tableHeadcount, restaurantId]);

if (reservationRows.length > 0) {
const reservationId = reservationRows[0].id;
const reservationPhone = reservationRows[0].phone;

console.log('tableheadcount',tableHeadcount)

console.log('res id',reservationRows[0].id)
console.log('res phone',reservationRows[0].phone)
Comment on lines +95 to +96
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

縮排記得調


await connection.query('UPDATE tableList SET phone = ? WHERE id = ?', [reservationPhone, tableId]);

const [reservationOrderRows] = await connection.query('SELECT id FROM OrderList WHERE reservationId = ?', [reservationId]);

if (reservationOrderRows.length > 0) {
await connection.query('UPDATE OrderList SET reservationId = null, tableId = ? WHERE reservationId = ?', [tableId, reservationId]);
}
await connection.query('DELETE FROM Reservation WHERE id = ?', [reservationId]);
} else {
await connection.query('UPDATE tableList SET phone = null, vacancy = true WHERE id = ?', [tableId]);
}

await connection.commit();
return tableId;
}
await connection.commit();
return null;
} catch (error) {
await connection.rollback();
console.error('Error in cancelReservation:', error);
throw error;
} finally {
connection.release();
}
}