第一版提交,答题功能OK,题库管理待完善
This commit is contained in:
91
api/models/user.ts
Normal file
91
api/models/user.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { query, run, get } from '../database';
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
name: string;
|
||||
phone: string;
|
||||
password?: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface CreateUserData {
|
||||
name: string;
|
||||
phone: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
export class UserModel {
|
||||
static async create(data: CreateUserData): Promise<User> {
|
||||
const id = uuidv4();
|
||||
const sql = `
|
||||
INSERT INTO users (id, name, phone, password)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
try {
|
||||
await run(sql, [id, data.name, data.phone, data.password || '']);
|
||||
return this.findById(id) as Promise<User>;
|
||||
} catch (error: any) {
|
||||
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
|
||||
throw new Error('手机号已存在');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async updatePasswordById(id: string, password: string): Promise<void> {
|
||||
const sql = `UPDATE users SET password = ? WHERE id = ?`;
|
||||
await run(sql, [password, id]);
|
||||
}
|
||||
|
||||
static async delete(id: string): Promise<void> {
|
||||
await run(`DELETE FROM users WHERE id = ?`, [id]);
|
||||
}
|
||||
|
||||
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]);
|
||||
return user || null;
|
||||
}
|
||||
|
||||
static async findByPhone(phone: string): Promise<User | null> {
|
||||
const sql = `SELECT id, name, phone, password, created_at as createdAt FROM users WHERE phone = ?`;
|
||||
const user = await get(sql, [phone]);
|
||||
return user || null;
|
||||
}
|
||||
|
||||
static async findAll(limit = 10, offset = 0): Promise<{ users: User[]; total: number }> {
|
||||
const usersSql = `
|
||||
SELECT id, name, phone, password, created_at as createdAt
|
||||
FROM users
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`;
|
||||
const countSql = `SELECT COUNT(*) as total FROM users`;
|
||||
|
||||
const [users, countResult] = await Promise.all([
|
||||
query(usersSql, [limit, offset]),
|
||||
get(countSql)
|
||||
]);
|
||||
|
||||
return {
|
||||
users,
|
||||
total: countResult.total
|
||||
};
|
||||
}
|
||||
|
||||
static validateUserData(data: CreateUserData): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!data.name || 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位中国手机号');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user