63 lines
1.4 KiB
JavaScript
63 lines
1.4 KiB
JavaScript
export class UserController {
|
|
constructor(getMeHandler, getAllUsersHandler, getUserByIdHandler, updateProfileHandler) {
|
|
this.getMeHandler = getMeHandler;
|
|
this.getAllUsersHandler = getAllUsersHandler;
|
|
this.getUserByIdHandler = getUserByIdHandler;
|
|
this.updateProfileHandler = updateProfileHandler;
|
|
}
|
|
|
|
async getMe(req, res, next) {
|
|
try {
|
|
const user = await this.getMeHandler.handle({ userId: req.user.id });
|
|
res.json({
|
|
success: true,
|
|
data: user.toJSON()
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
async getAllUsers(req, res, next) {
|
|
try {
|
|
const users = await this.getAllUsersHandler.handle({});
|
|
res.json({
|
|
success: true,
|
|
data: users.map(u => u.toJSON())
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
async getUserById(req, res, next) {
|
|
try {
|
|
const user = await this.getUserByIdHandler.handle({ userId: req.params.id });
|
|
res.json({
|
|
success: true,
|
|
data: user.toJSON()
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
|
|
async updateProfile(req, res, next) {
|
|
try {
|
|
const { name, email } = req.body;
|
|
const user = await this.updateProfileHandler.handle({
|
|
userId: req.user.id,
|
|
name,
|
|
email
|
|
});
|
|
res.json({
|
|
success: true,
|
|
message: 'Profile updated successfully',
|
|
data: user.toJSON()
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
}
|
|
}
|