aboutsummaryrefslogtreecommitdiffstats
path: root/src/controllers/userController.js
blob: a82368f5c2510b14424c136f08c53e614bcb1010 (plain) (blame)
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
import User from "../models/User.js";

function getUserById(id) {
    return User.findById(id).select('-token');
}

function getUserByUsername(username) {
    return User.findOne({ username: username });
}

function listUsers(opts) {
    const limit = opts.limit || 50;
    const offset = opts.offset || 0;
    return User.find().select('-token').skip(offset).limit(limit);
}

function searchUsers(opts) {
    const q = opts.q || '';
    const query = {
        $or: [
            { username: { $regex: q, $options: 'i' } },
            { displayName: { $regex: q, $options: 'i' } }
        ]
    };
    return User.find(query).select('-token');
}

function createUser(data) {
    const user = new User(data);
    return user.save();
}

function updateUser(id, updates) {
    return User.findByIdAndUpdate(id, updates, { new: true }).select('-token');
}

function deleteUser(id) {
    return User.findByIdAndDelete(id);
}

export { getUserById, getUserByUsername, listUsers, searchUsers, createUser, updateUser, deleteUser };