import { Body, Controller, Delete, Get, Injectable, Param, Patch, Post, Put } from '@nestjs/common';
import { UserService } from 'src/users/users.service';
import { Users } from '../entity/user.entity';

// @Injectable()注入器
@Controller('users')
export class UserController {
    constructor(private readonly userService: UserService) { }

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

    // 根据id查找用户
    @Get("/findById/:id")
    async findById(@Param('id') id: number): Promise<Users> {
        return await this.userService.findOne(id);
    }

    // 根据id删除用户
    @Delete("/remove/:id")
    async remove(@Param('id') id: number): Promise<void> {
        await this.userService.remove(id);
    }

    // 新建用户
    @Post("/save")
    async save(@Body() user: Users): Promise<Users> {
        return await this.userService.save(user)
    }

    // 根据id修改用户
    @Put("/update/:id")
    async update(@Param('id') id: number, @Body() user: Partial<Users>): Promise<void> {
        this.userService.update(id, user);
    }
}