Users CRUD 实现
This commit is contained in:
parent
ab75254f61
commit
6d0c55f0a2
@ -1,27 +1,27 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm'
|
||||
import { Users } from './users/users.entity';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
import { UserModule } from './users/users.module';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Users } from './entity/user.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'mssql',
|
||||
host: 'intpointer.com',
|
||||
type: "mssql",
|
||||
host: "intpointer.com",
|
||||
port: 1433,
|
||||
username: 'myh',
|
||||
password: '20231103#MS_Sql',
|
||||
database: 'Elm',
|
||||
username: "myh",
|
||||
password: "20231103#MS_Sql",
|
||||
database: "Elm",
|
||||
entities: [Users],
|
||||
synchronize: false,
|
||||
logging: false,
|
||||
"options": {
|
||||
"encrypt": false,
|
||||
"enableArithAbort": true
|
||||
}
|
||||
}),
|
||||
UserModule,
|
||||
],
|
||||
})
|
||||
|
||||
export class AppModule { }
|
@ -1,4 +1,4 @@
|
||||
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { Entity, Column, PrimaryGeneratedColumn, Table } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Users {
|
||||
@ -16,5 +16,4 @@ export class Users {
|
||||
|
||||
@Column('text')
|
||||
address: string
|
||||
|
||||
}
|
@ -9,7 +9,7 @@ async function bootstrap() {
|
||||
app.setBaseViewsDir(join(__dirname, '..', 'views'));
|
||||
app.setViewEngine('hbs');
|
||||
|
||||
app.listen(9000, '0.0.0.0', () => {
|
||||
await app.listen(9000, '0.0.0.0', () => {
|
||||
console.log(`Server start on http://0.0.0.0:9000`);
|
||||
});
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
export class AddUsersDto {
|
||||
name: string
|
||||
sex: string
|
||||
phoneNumber: string
|
||||
address: string
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersModule } from './users.module';
|
||||
import { UsersService } from './users.service';
|
||||
import { UsersController } from './users.controller';
|
||||
|
||||
@Module({
|
||||
imports: [UsersModule],
|
||||
providers: [UsersService],
|
||||
controllers: [UsersController]
|
||||
})
|
||||
export class UserHttpModule {}
|
@ -1,39 +1,33 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
ParseIntPipe,
|
||||
Render,
|
||||
} from '@nestjs/common';
|
||||
import { UsersService } from "./users.service";
|
||||
import { Users } from './users.entity';
|
||||
import { AddUsersDto } from './dto/add-users.dto'
|
||||
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';
|
||||
|
||||
@Controller('Users')
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) { }
|
||||
@Controller('users')
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) { }
|
||||
|
||||
@Post()
|
||||
add(@Body() addUsersDto: AddUsersDto): Promise<Users> {
|
||||
return this.usersService.add(addUsersDto);
|
||||
@Get("/findAll")
|
||||
async findAll(): Promise<Users[]> {
|
||||
return await this.userService.findAll();
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(): Promise<Users[]> {
|
||||
return this.usersService.findAll();
|
||||
@Get("/findById/:id")
|
||||
async findById(@Param('id') id: number): Promise<Users> {
|
||||
return await this.userService.findOne(id);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id', ParseIntPipe) id: number): Promise<Users> {
|
||||
return this.usersService.findOne(id);
|
||||
@Delete("/remove/:id")
|
||||
async remove(@Param('id') id: number): Promise<void> {
|
||||
await this.userService.remove(id);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
delete(@Param('id') id: number): Promise<void> {
|
||||
return this.usersService.delete(id);
|
||||
@Post("/save")
|
||||
async save(@Body() user: Users): Promise<Users> {
|
||||
return await this.userService.save(user)
|
||||
}
|
||||
|
||||
@Put("/update")
|
||||
async update(@Param('id') id: number, @Body() user: Users): Promise<void> {
|
||||
this.userService.update(id, user);
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { Users } from 'src/entity/user.entity';
|
||||
import { UserService } from 'src/users/users.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UsersService } from './users.service';
|
||||
import { UsersController } from './users.controller';
|
||||
import { Users } from './users.entity';
|
||||
import { UserController } from 'src/users/users.controller';
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Users])],
|
||||
providers: [UsersService],
|
||||
controllers: [UsersController],
|
||||
providers: [UserService],
|
||||
controllers: [UserController],
|
||||
})
|
||||
export class UsersModule {}
|
||||
export class UserModule {}
|
@ -1,34 +1,31 @@
|
||||
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';
|
||||
import { Users } from "src/entity/user.entity";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { FindOneOptions, Repository } from "typeorm";
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
export class UserService {
|
||||
constructor(
|
||||
@InjectRepository(Users)
|
||||
private usersRepository: Repository<Users>,
|
||||
) { }
|
||||
private userRepository: 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);
|
||||
async findAll(): Promise<Users[]> {
|
||||
return await this.userRepository.find();
|
||||
}
|
||||
|
||||
findAll(): Promise<Users[]> {
|
||||
return this.usersRepository.find();
|
||||
async findOne(id: number): Promise<Users> {
|
||||
return await this.userRepository.findOne(id)
|
||||
}
|
||||
|
||||
findOne(id: number): Promise<Users | null> {
|
||||
return this.usersRepository.findOneBy({ id });
|
||||
async remove(id: number): Promise<void> {
|
||||
await this.userRepository.delete(id)
|
||||
}
|
||||
|
||||
async delete(id: number): Promise<void> {
|
||||
await this.usersRepository.delete(id);
|
||||
async save(user: Users): Promise<Users> {
|
||||
return await this.userRepository.save(user)
|
||||
}
|
||||
|
||||
async update(id: number, user: Users): Promise<void> {
|
||||
await this.userRepository.update(id, user);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user