import { Users } from "src/entity/user.entity";
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";

// service层
@Injectable()
export class UserService {
    // 注入器
    constructor(
        @InjectRepository(Users)
        private userRepository: Repository<Users>) { }

    // 查找所有用户
    async findAll(): Promise<Users[]> {
        return await this.userRepository.find();
    }

    // 根据id查找用户
    async findOne(id: number): Promise<Users> {
        return await this.userRepository.findOne(id)
    }

    // 根据id删除用户
    async remove(id: number): Promise<void> {
        await this.userRepository.delete(id)
    }

    // 新建用户
    async save(user: Users): Promise<Users> {
        return await this.userRepository.save(user)
    }

    // 根据id修改用户
    async update(id: number, user: Partial<Users>): Promise<void> {
        try {
            await this.userRepository.update(id,user);
        } catch (error) {
            console.log(error);
        }
    }
}