feat: implement admin dashboard with full CRUD functionality

This commit is contained in:
2025-06-18 16:20:19 +08:00
parent 95ef4cd617
commit 0ca2d51669
6 changed files with 324 additions and 3 deletions

View File

@ -0,0 +1,24 @@
import { defineEventHandler, createError } from 'h3';
import { db } from '~/server/db';
import { customers } from '~/server/db/schema';
export default defineEventHandler(async () => {
try {
// Select specific fields to avoid exposing sensitive data like passwords
const allCustomers = await db.select({
CustomerID: customers.id,
Name: customers.name,
Contact: customers.contact,
Gender: customers.gender,
IDCard: customers.idCard,
}).from(customers);
return allCustomers;
} catch (error) {
console.error('Error fetching customers:', error);
return createError({
statusCode: 500,
statusMessage: 'Failed to fetch customers',
});
}
});

View File

@ -0,0 +1,39 @@
import { defineEventHandler, readBody, setResponseStatus, createError } from 'h3';
import { db, customers } from '~/server/db';
import bcrypt from 'bcryptjs';
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const { name, gender, contact, idCard, password } = body;
if (!name || !gender || !contact || !idCard || !password) {
return createError({ statusCode: 400, statusMessage: '请填写完整信息' });
}
try {
const hashedPassword = bcrypt.hashSync(password, 10);
await db.insert(customers).values({
name,
gender,
contact,
idCard,
password: hashedPassword,
});
return { message: '用户添加成功!' };
} catch (error: any) {
if (error.message?.includes('UNIQUE constraint failed')) {
if (error.message.includes('customers.contact')) {
return createError({ statusCode: 409, statusMessage: '该手机号已被注册' });
}
if (error.message.includes('customers.id_card')) {
return createError({ statusCode: 409, statusMessage: '该身份证号已被注册' });
}
}
console.error('Add user error:', error);
return createError({ statusCode: 500, statusMessage: '添加用户失败,请稍后重试' });
}
});

View File

@ -0,0 +1,21 @@
import { defineEventHandler, createError } from 'h3';
import { db } from '~/server/db';
import { roomTypes } from '~/server/db/schema';
export default defineEventHandler(async () => {
try {
const allRoomTypes = await db.select({
TypeID: roomTypes.id,
TypeName: roomTypes.typeName,
StarRating: roomTypes.starRating,
}).from(roomTypes);
return allRoomTypes;
} catch (error) {
console.error('Error fetching hotel types:', error);
return createError({
statusCode: 500,
statusMessage: 'Failed to fetch hotel types',
});
}
});

View File

@ -0,0 +1,27 @@
import { defineEventHandler, createError } from 'h3';
import { db } from '~/server/db';
import { reservations, customers } from '~/server/db/schema';
import { eq } from 'drizzle-orm';
export default defineEventHandler(async () => {
try {
const allReservations = await db
.select({
ReservationID: reservations.id,
Name: customers.name,
RoomID: reservations.roomId,
CheckInTime: reservations.checkInTime,
StayDays: reservations.stayDays,
})
.from(reservations)
.leftJoin(customers, eq(reservations.customerId, customers.id));
return allReservations;
} catch (error) {
console.error('Error fetching all reservations:', error);
return createError({
statusCode: 500,
statusMessage: 'Failed to fetch all reservations',
});
}
});

View File

@ -0,0 +1,28 @@
import { defineEventHandler, createError } from 'h3';
import { db } from '~/server/db';
import { rooms, roomTypes } from '~/server/db/schema';
import { eq } from 'drizzle-orm';
export default defineEventHandler(async () => {
try {
const allRooms = await db
.select({
RoomID: rooms.id,
Type: roomTypes.typeName,
Price: rooms.price,
Feature: rooms.feature,
AvailableCount: rooms.availableCount,
TypeID: rooms.typeId,
})
.from(rooms)
.leftJoin(roomTypes, eq(rooms.typeId, roomTypes.id));
return allRooms;
} catch (error) {
console.error('Error fetching all rooms:', error);
return createError({
statusCode: 500,
statusMessage: 'Failed to fetch all rooms',
});
}
});