-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserService.js
More file actions
37 lines (30 loc) · 1010 Bytes
/
userService.js
File metadata and controls
37 lines (30 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// userService.js - 负责用户相关的业务逻辑
const db = require('./db');
class UserService {
/**
* 获取所有用户
*/
async getAllUsers() {
// 可以在这里添加权限检查等业务逻辑
const sql = 'SELECT id, name, email FROM users';
return db.all(sql);
}
/**
* 创建一个新用户
* @param {string} name
* @param {string} email
*/
async createUser(name, email) {
// 业务逻辑: 检查输入参数、验证邮箱格式等
if (!name || !email) {
throw new Error("Name and email are required.");
}
// 实际的数据库操作
const sql = 'INSERT INTO users (name, email) VALUES (?, ?)';
const result = await db.run(sql, [name, email]);
// 返回新创建用户的 ID
return { id: result.lastID, name, email };
}
// 可以添加更多方法如 findUserById, updateUser, deleteUser 等...
}
module.exports = new UserService();