37 lines
941 B
JavaScript
37 lines
941 B
JavaScript
const { ValidationError } = require('../shared/errors');
|
|
|
|
class UsersService {
|
|
constructor({ usersRepository }) {
|
|
this.usersRepository = usersRepository;
|
|
}
|
|
|
|
async getAllUsers() {
|
|
return await this.usersRepository.findAll();
|
|
}
|
|
|
|
async getUserById(id) {
|
|
return await this.usersRepository.findById(id);
|
|
}
|
|
|
|
async updateUserProfile(id, updateData) {
|
|
const { name, email, avatarUrl } = updateData;
|
|
|
|
if (name && name.trim() === '') {
|
|
throw new ValidationError('Name cannot be empty');
|
|
}
|
|
|
|
if (email && email.trim() === '') {
|
|
throw new ValidationError('Email cannot be empty');
|
|
}
|
|
|
|
const dataToUpdate = {};
|
|
if (name !== undefined) dataToUpdate.name = name;
|
|
if (email !== undefined) dataToUpdate.email = email;
|
|
if (avatarUrl !== undefined) dataToUpdate.avatarUrl = avatarUrl;
|
|
|
|
return await this.usersRepository.update(id, dataToUpdate);
|
|
}
|
|
}
|
|
|
|
module.exports = UsersService;
|