import { Injectable , Logger , NotFoundException } from '@nestjs/common' ; import { IUsersRepository } from 'application/ports/IUsersRepository' ; import { User } from 'domain/models/User' ; @ Injectable ( ) export class UsersUseCases { private readonly logger = new Logger ( UsersUseCases . name ) ; constructor ( private readonly usersRepository ) { } async getUsers ( ) { this . logger . log ( 'Find all users' ) ; return await this . usersRepository . find ( { loadEagerRelations : true } ) ; } async getUserById ( id ) { this . logger . log ( `Find the user: ${ id }` ) ; const user = await this . usersRepository . findOne ( id ) ; if ( ! user ) throw new NotFoundException ( `The user {${ id }} has not found.` ) ; return user ; } async createUser ( user ) { this . logger . log ( `Saving a user` ) ; return await this . usersRepository . save ( user ) ; } async updateUser ( user ) { this . logger . log ( `Updating a user: ${ user . id }` ) ; const result = await this . usersRepository . update ( { id : user . id } , user ) ; return result . affected > 0 ; } async deleteUser ( id ) { this . logger . log ( `Deleting a user: ${ id }` ) ; const result = await this . usersRepository . delete ( { id } ) ; return result . affected > 0 ; } }