import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Users } from './users.entity';
import { AddUsersDto } from './dto/add-users.dto';

@Injectable()
export class UsersService {
    constructor(
        @InjectRepository(Users)
        private usersRepository: Repository<Users>,
    ) { }

    add(addUsersDto: AddUsersDto): Promise<Users> {
        const users = new Users();
        users.name = addUsersDto.name;
        users.sex = addUsersDto.sex;
        users.phoneNUmber = addUsersDto.phoneNumber;
        users.address = addUsersDto.phoneNumber;
        return this.usersRepository.save(users);
    }

    findAll(): Promise<Users[]> {
        return this.usersRepository.find();
    }

    findOne(id: number): Promise<Users | null> {
        return this.usersRepository.findOneBy({ id });
    }

    async delete(id: number): Promise<void> {
        await this.usersRepository.delete(id);
    }
}