题库导入功能完成,考试计划功能完成。
This commit is contained in:
@@ -43,6 +43,58 @@ export class UserModel {
|
||||
await run(`DELETE FROM users WHERE id = ?`, [id]);
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
static async update(id: string, data: Partial<CreateUserData>): Promise<User> {
|
||||
// 验证数据
|
||||
const errors = this.validateUserData(data as CreateUserData);
|
||||
if (errors.length > 0) {
|
||||
throw new Error(errors.join(', '));
|
||||
}
|
||||
|
||||
// 检查手机号唯一性
|
||||
if (data.phone !== undefined) {
|
||||
const existingUser = await this.findByPhone(data.phone);
|
||||
if (existingUser && existingUser.id !== id) {
|
||||
throw new Error('手机号已存在');
|
||||
}
|
||||
}
|
||||
|
||||
// 构建更新字段
|
||||
const fields: string[] = [];
|
||||
const values: any[] = [];
|
||||
|
||||
if (data.name !== undefined) {
|
||||
fields.push('name = ?');
|
||||
values.push(data.name);
|
||||
}
|
||||
|
||||
if (data.phone !== undefined) {
|
||||
fields.push('phone = ?');
|
||||
values.push(data.phone);
|
||||
}
|
||||
|
||||
if (data.password !== undefined) {
|
||||
fields.push('password = ?');
|
||||
values.push(data.password);
|
||||
}
|
||||
|
||||
if (fields.length === 0) {
|
||||
return this.findById(id) as Promise<User>;
|
||||
}
|
||||
|
||||
values.push(id);
|
||||
const sql = `UPDATE users SET ${fields.join(', ')} WHERE id = ?`;
|
||||
|
||||
await run(sql, values);
|
||||
const updatedUser = await this.findById(id);
|
||||
|
||||
if (!updatedUser) {
|
||||
throw new Error('用户不存在');
|
||||
}
|
||||
|
||||
return updatedUser;
|
||||
}
|
||||
|
||||
static async findById(id: string): Promise<User | null> {
|
||||
const sql = `SELECT id, name, phone, password, created_at as createdAt FROM users WHERE id = ?`;
|
||||
const user = await get(sql, [id]);
|
||||
@@ -55,6 +107,12 @@ export class UserModel {
|
||||
return user || null;
|
||||
}
|
||||
|
||||
static async findByName(name: string): Promise<User[]> {
|
||||
const sql = `SELECT id, name, phone, password, created_at as createdAt FROM users WHERE name = ?`;
|
||||
const users = await query(sql, [name]);
|
||||
return users || [];
|
||||
}
|
||||
|
||||
static async findAll(limit = 10, offset = 0): Promise<{ users: User[]; total: number }> {
|
||||
const usersSql = `
|
||||
SELECT id, name, phone, password, created_at as createdAt
|
||||
@@ -75,15 +133,21 @@ export class UserModel {
|
||||
};
|
||||
}
|
||||
|
||||
static validateUserData(data: CreateUserData): string[] {
|
||||
// 验证用户数据,支持部分数据验证
|
||||
static validateUserData(data: Partial<CreateUserData>): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!data.name || data.name.length < 2 || data.name.length > 20) {
|
||||
errors.push('姓名长度必须在2-20个字符之间');
|
||||
// 只验证提供了的数据
|
||||
if (data.name !== undefined) {
|
||||
if (data.name.length < 2 || data.name.length > 20) {
|
||||
errors.push('姓名长度必须在2-20个字符之间');
|
||||
}
|
||||
}
|
||||
|
||||
if (!data.phone || !/^1[3-9]\d{9}$/.test(data.phone)) {
|
||||
errors.push('手机号格式不正确,请输入11位中国手机号');
|
||||
if (data.phone !== undefined) {
|
||||
if (!/^1[3-9]\d{9}$/.test(data.phone)) {
|
||||
errors.push('手机号格式不正确,请输入11位中国手机号');
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
|
||||
Reference in New Issue
Block a user