添加注释
This commit is contained in:
		| @@ -3,6 +3,7 @@ import { UserModule } from './users/users.module'; | |||||||
| import { TypeOrmModule } from '@nestjs/typeorm'; | import { TypeOrmModule } from '@nestjs/typeorm'; | ||||||
| import { Users } from './entity/user.entity'; | import { Users } from './entity/user.entity'; | ||||||
|  |  | ||||||
|  | // 数据库连接池配置 | ||||||
| @Module({ | @Module({ | ||||||
|     imports: [ |     imports: [ | ||||||
|         TypeOrmModule.forRoot({ |         TypeOrmModule.forRoot({ | ||||||
|   | |||||||
| @@ -1,7 +1,9 @@ | |||||||
| import { Entity, Column, PrimaryGeneratedColumn, Table } from 'typeorm'; | import { Entity, Column, PrimaryGeneratedColumn, Table } from 'typeorm'; | ||||||
|  |  | ||||||
|  | // 实体层 | ||||||
| @Entity() | @Entity() | ||||||
| export class Users { | export class Users { | ||||||
|  |     // 以下是数据库基本字段 | ||||||
|     @PrimaryGeneratedColumn() |     @PrimaryGeneratedColumn() | ||||||
|     id: number; |     id: number; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,30 +2,36 @@ import { Body, Controller, Delete, Get, Injectable, Param, Patch, Post, Put } fr | |||||||
| import { UserService } from 'src/users/users.service'; | import { UserService } from 'src/users/users.service'; | ||||||
| import { Users } from '../entity/user.entity'; | import { Users } from '../entity/user.entity'; | ||||||
|  |  | ||||||
|  | // @Injectable()注入器 | ||||||
| @Controller('users') | @Controller('users') | ||||||
| export class UserController { | export class UserController { | ||||||
|     constructor(private readonly userService: UserService) { } |     constructor(private readonly userService: UserService) { } | ||||||
|  |  | ||||||
|  |     // 查找所有用户 | ||||||
|     @Get("/findAll") |     @Get("/findAll") | ||||||
|     async findAll(): Promise<Users[]> { |     async findAll(): Promise<Users[]> { | ||||||
|         return await this.userService.findAll(); |         return await this.userService.findAll(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 根据id查找用户 | ||||||
|     @Get("/findById/:id") |     @Get("/findById/:id") | ||||||
|     async findById(@Param('id') id: number): Promise<Users> { |     async findById(@Param('id') id: number): Promise<Users> { | ||||||
|         return await this.userService.findOne(id); |         return await this.userService.findOne(id); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 根据id删除用户 | ||||||
|     @Delete("/remove/:id") |     @Delete("/remove/:id") | ||||||
|     async remove(@Param('id') id: number): Promise<void> { |     async remove(@Param('id') id: number): Promise<void> { | ||||||
|         await this.userService.remove(id); |         await this.userService.remove(id); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 新建用户 | ||||||
|     @Post("/save") |     @Post("/save") | ||||||
|     async save(@Body() user: Users): Promise<Users> { |     async save(@Body() user: Users): Promise<Users> { | ||||||
|         return await this.userService.save(user) |         return await this.userService.save(user) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 根据id修改用户 | ||||||
|     @Put("/update") |     @Put("/update") | ||||||
|     async update(@Param('id') id: number, @Body() user: Users): Promise<void> { |     async update(@Param('id') id: number, @Body() user: Users): Promise<void> { | ||||||
|         this.userService.update(id, user); |         this.userService.update(id, user); | ||||||
|   | |||||||
| @@ -1,30 +1,37 @@ | |||||||
| import { Users } from "src/entity/user.entity"; | import { Users } from "src/entity/user.entity"; | ||||||
| import { Injectable } from "@nestjs/common"; | import { Injectable } from "@nestjs/common"; | ||||||
| import { InjectRepository } from "@nestjs/typeorm"; | import { InjectRepository } from "@nestjs/typeorm"; | ||||||
| import { FindOneOptions, Repository } from "typeorm"; | import { Repository } from "typeorm"; | ||||||
|  |  | ||||||
|  | // service层 | ||||||
| @Injectable() | @Injectable() | ||||||
| export class UserService { | export class UserService { | ||||||
|  |     // 注入器 | ||||||
|     constructor( |     constructor( | ||||||
|         @InjectRepository(Users) |         @InjectRepository(Users) | ||||||
|         private userRepository: Repository<Users>) { } |         private userRepository: Repository<Users>) { } | ||||||
|  |  | ||||||
|  |     // 查找所有用户 | ||||||
|     async findAll(): Promise<Users[]> { |     async findAll(): Promise<Users[]> { | ||||||
|         return await this.userRepository.find(); |         return await this.userRepository.find(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 根据id查找用户 | ||||||
|     async findOne(id: number): Promise<Users> { |     async findOne(id: number): Promise<Users> { | ||||||
|         return await this.userRepository.findOne(id) |         return await this.userRepository.findOne(id) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 根据id删除用户 | ||||||
|     async remove(id: number): Promise<void> { |     async remove(id: number): Promise<void> { | ||||||
|         await this.userRepository.delete(id) |         await this.userRepository.delete(id) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 新建用户 | ||||||
|     async save(user: Users): Promise<Users> { |     async save(user: Users): Promise<Users> { | ||||||
|         return await this.userRepository.save(user) |         return await this.userRepository.save(user) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // 根据id修改用户 | ||||||
|     async update(id: number, user: Users): Promise<void> { |     async update(id: number, user: Users): Promise<void> { | ||||||
|         await this.userRepository.update(id, user); |         await this.userRepository.update(id, user); | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user