Datasets:
Download code/1749192826066_userController.js from TwanAPI/data1: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/datasets/TwanAPI/data1/resolve/main/code/1749192826066_userController.js
- Command line
-
hf download hf://datasets/TwanAPI/data1/code/1749192826066_userController.js
-
curl -L -o 1749192826066_userController.js https://huggingface.co/datasets/TwanAPI/data1/resolve/main/code/1749192826066_userController.js
1.35 kB
| const User = require('./User'); // Thay './User' bằng đường dẫn thực tế đến file mô hình User của bạn | |
| // Lấy tất cả người dùng | |
| exports.getAllUsers = async (req, res) => { | |
| try { | |
| const users = await User.find(); | |
| res.status(200).json(users); | |
| } catch (err) { | |
| res.status(500).send(err); | |
| } | |
| }; | |
| // Lấy người dùng theo ID | |
| exports.getUserById = async (req, res) => { | |
| try { | |
| const user = await User.findById(req.params.id); | |
| res.status(200).json(user); | |
| } catch (err) { | |
| res.status(500).send(err); | |
| } | |
| }; | |
| // Tạo một người dùng mới | |
| exports.createUser = async (req, res) => { | |
| try { | |
| const newUser = new User(req.body); | |
| await newUser.save(); | |
| res.status(201).send(newUser); | |
| } catch (err) { | |
| res.status(500).send(err); | |
| } | |
| }; | |
| // Cập nhật một người dùng | |
| exports.updateUser = async (req, res) => { | |
| try { | |
| const updatedUser = await User.findByIdAndUpdate(req.params.id, req.body, { new: true }); | |
| res.status(200).json(updatedUser); | |
| } catch (err) { | |
| res.status(500).send(err); | |
| } | |
| }; | |
| // Xóa một người dùng | |
| exports.deleteUser = async (req, res) => { | |
| try { | |
| await User.findByIdAndDelete(req.params.id); | |
| res.status(204).send(); | |
| } catch (err) { | |
| res.status(500).send(err); | |
| } | |
| }; | |