forked from github/copilot-cli-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.js
More file actions
60 lines (50 loc) · 1.17 KB
/
Copy pathUser.js
File metadata and controls
60 lines (50 loc) · 1.17 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* User model
*/
const users = []; // In-memory storage for demo
let nextId = 1;
class User {
constructor({ id, name, email, password, createdAt }) {
this.id = id || nextId++;
this.name = name;
this.email = email;
this.password = password; // TODO: Hash passwords
this.createdAt = createdAt || new Date();
}
static async findAll() {
return users.map(u => ({ id: u.id, name: u.name, email: u.email }));
}
static async findById(id) {
return users.find(u => u.id === parseInt(id));
}
static async findByEmail(email) {
return users.find(u => u.email === email);
}
static async create(data) {
const user = new User(data);
users.push(user);
return user;
}
static async deleteById(id) {
const index = users.findIndex(u => u.id === parseInt(id));
if (index !== -1) {
users.splice(index, 1);
}
}
async save() {
const index = users.findIndex(u => u.id === this.id);
if (index !== -1) {
users[index] = this;
}
return this;
}
toJSON() {
return {
id: this.id,
name: this.name,
email: this.email,
createdAt: this.createdAt
};
}
}
module.exports = User;