Initial commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user