Skip to content

Commit

Permalink
[修改] 房型新增 status 狀態
Browse files Browse the repository at this point in the history
  • Loading branch information
ptp_ting-xuan committed Oct 31, 2023
1 parent 9f426e6 commit 3ae5c31
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/controllers/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import RoomModel from '@/models/room';

export const getRoomList: RequestHandler = async (_req, res, next) => {
try {
const result = await RoomModel.find();
const result = await RoomModel.find({
status: 1
});

res.send({
status: true,
Expand All @@ -17,7 +19,10 @@ export const getRoomList: RequestHandler = async (_req, res, next) => {

export const getRoomById: RequestHandler = async (req, res, next) => {
try {
const result = await RoomModel.findById(req.params.id);
const result = await RoomModel.findOne({
_id: req.params.id,
status: 1
});
if (!result) {
throw createHttpError(404, '此房型不存在');
}
Expand Down Expand Up @@ -93,7 +98,16 @@ export const updateRoomById: RequestHandler = async (req, res, next) => {

export const deleteRoomById: RequestHandler = async (req, res, next) => {
try {
const result = await RoomModel.findByIdAndRemove(req.params.id);
const result = await RoomModel.findByIdAndUpdate(
req.params.id,
{
status: -1
},
{
new: true,
runValidators: true
}
);
if (!result) {
throw createHttpError(404, '此房型不存在');
}
Expand Down
5 changes: 4 additions & 1 deletion src/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ export const checkOrder: RequestHandler = async (req, _res, next) => {
try {
const { roomId, checkInDate, checkOutDate, peopleNum } = req.body;

const roomInfo = await RoomModel.findById(roomId);
const roomInfo = await RoomModel.findOne({
_id: roomId,
status: 1
});

if (!roomInfo) {
throw createHttpError(404, '此房型不存在');
Expand Down
6 changes: 6 additions & 0 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface IRoom extends Document {
bedInfo: string;
maxPeople: number;
price: number;
// 可使用:1,已刪除:-1
status: number;
}

const roomSchema = new Schema<IRoom>(
Expand Down Expand Up @@ -70,6 +72,10 @@ const roomSchema = new Schema<IRoom>(
price: {
type: Number,
required: [true, 'price 未填寫']
},
status: {
type: Number,
default: 1
}
},
{
Expand Down
3 changes: 3 additions & 0 deletions src/routes/api/v1/admin/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ router.get(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
},
Expand Down Expand Up @@ -107,6 +108,7 @@ router.put(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
},
Expand Down Expand Up @@ -170,6 +172,7 @@ router.delete(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
},
Expand Down
4 changes: 4 additions & 0 deletions src/routes/api/v1/admin/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ router.get(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
}
Expand Down Expand Up @@ -75,6 +76,7 @@ router.post(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"_id": "653e4661336cdccc752127a0",
"createdAt": "2023-10-29T11:47:45.641Z",
"updatedAt": "2023-10-29T11:47:45.641Z"
Expand Down Expand Up @@ -130,6 +132,7 @@ router.put(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"_id": "653e4661336cdccc752127a0",
"createdAt": "2023-10-29T11:47:45.641Z",
"updatedAt": "2023-10-29T11:47:45.641Z"
Expand Down Expand Up @@ -173,6 +176,7 @@ router.delete(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": -1,
"_id": "653e4661336cdccc752127a0",
"createdAt": "2023-10-29T11:47:45.641Z",
"updatedAt": "2023-10-29T11:47:45.641Z"
Expand Down
4 changes: 4 additions & 0 deletions src/routes/api/v1/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ router.get(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
},
Expand Down Expand Up @@ -90,6 +91,7 @@ router.get(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
},
Expand Down Expand Up @@ -155,6 +157,7 @@ router.post(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
},
Expand Down Expand Up @@ -228,6 +231,7 @@ router.delete(
"bedInfo": "一張大床",
"maxPeople": 4,
"price": 10000,
"status": 1,
"createdAt": "2023-10-10T09:54:40.063Z",
"updatedAt": "2023-10-10T09:54:40.063Z"
},
Expand Down

0 comments on commit 3ae5c31

Please sign in to comment.