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