feat: curd
This commit is contained in:
parent
410bfc1e38
commit
74c77011ea
@ -1,17 +1,15 @@
|
||||
const productConfig = {
|
||||
accessKeyId: 'accessKeyId',
|
||||
accessKeySecret: 'accessKeySecret',
|
||||
endpoint: 'endpoint',
|
||||
bucket: 'bucket',
|
||||
region: 'region',
|
||||
accessKeyId: 'LTAI5tHfUhFjJ335EVo1vcWm',
|
||||
accessKeySecret: '0q5JF4862ai5pznfIZMTIjThVjzaqK',
|
||||
bucket: 'polaris-frontend',
|
||||
dir: 'yitu_image/',
|
||||
};
|
||||
|
||||
const localConfig = {
|
||||
accessKeyId: 'accessKeyId',
|
||||
accessKeySecret: 'accessKeySecret',
|
||||
endpoint: 'endpoint',
|
||||
bucket: 'bucket',
|
||||
region: 'region',
|
||||
accessKeyId: 'LTAI5tHfUhFjJ335EVo1vcWm',
|
||||
accessKeySecret: '0q5JF4862ai5pznfIZMTIjThVjzaqK',
|
||||
bucket: 'polaris-frontend',
|
||||
dir: 'yitu_image/',
|
||||
};
|
||||
|
||||
// 本地运行是没有 process.env.NODE_ENV 的,借此来区分[开发环境]和[生产环境]
|
||||
|
@ -27,6 +27,7 @@
|
||||
"@nestjs/typeorm": "^10.0.2",
|
||||
"@types/ali-oss": "^6.16.11",
|
||||
"ali-oss": "^6.20.0",
|
||||
"dayjs": "^1.11.11",
|
||||
"multer": "1.4.5-lts.1",
|
||||
"mysql2": "^3.9.7",
|
||||
"reflect-metadata": "^0.2.0",
|
||||
|
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@ -29,6 +29,9 @@ importers:
|
||||
ali-oss:
|
||||
specifier: ^6.20.0
|
||||
version: 6.20.0
|
||||
dayjs:
|
||||
specifier: ^1.11.11
|
||||
version: 1.11.11
|
||||
multer:
|
||||
specifier: 1.4.5-lts.1
|
||||
version: 1.4.5-lts.1
|
||||
|
@ -1,10 +1,17 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { UserEntity } from './user/entities/user.entity';
|
||||
import { BannerEntity } from './banner/entities/banner.entity';
|
||||
import { UserModule } from './user/user.module';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { BannerModule } from './banner/banner.module';
|
||||
import { OssModule } from './oss/oss.module';
|
||||
import { SysUserModule } from './sys-user/sys-user.module';
|
||||
|
||||
const ENTITIES = [UserEntity, BannerEntity];
|
||||
|
||||
const MODULE = [UserModule, BannerModule, OssModule];
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -15,10 +22,11 @@ import { OssModule } from './oss/oss.module';
|
||||
username: 'root',
|
||||
password: '123456',
|
||||
database: 'travel-app',
|
||||
entities: [UserModule], // 这里添加实体类
|
||||
entities: ENTITIES, // 这里添加实体类
|
||||
synchronize: true,
|
||||
}),
|
||||
BannerModule,
|
||||
OssModule,
|
||||
...MODULE,
|
||||
SysUserModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Param, Delete } from '@nestjs/common';
|
||||
import { BannerService } from './banner.service';
|
||||
import { CreateBannerDto } from './dto/create-banner.dto';
|
||||
import { UpdateBannerDto } from './dto/update-banner.dto';
|
||||
|
||||
@Controller('banner')
|
||||
export class BannerController {
|
||||
@ -22,11 +21,6 @@ export class BannerController {
|
||||
return this.bannerService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateBannerDto: UpdateBannerDto) {
|
||||
return this.bannerService.update(+id, updateBannerDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.bannerService.remove(+id);
|
||||
|
@ -1,8 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { BannerService } from './banner.service';
|
||||
import { BannerController } from './banner.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { BannerEntity } from './entities/banner.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([BannerEntity])],
|
||||
controllers: [BannerController],
|
||||
providers: [BannerService],
|
||||
})
|
||||
|
@ -1,26 +1,34 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreateBannerDto } from './dto/create-banner.dto';
|
||||
import { UpdateBannerDto } from './dto/update-banner.dto';
|
||||
import { BannerEntity } from './entities/banner.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class BannerService {
|
||||
create(createBannerDto: CreateBannerDto) {
|
||||
return 'This action adds a new banner';
|
||||
constructor(
|
||||
@InjectRepository(BannerEntity)
|
||||
private readonly bannerRepository: Repository<BannerEntity>,
|
||||
) {}
|
||||
async create(createBannerDto: CreateBannerDto) {
|
||||
const res = await this.bannerRepository.create(createBannerDto);
|
||||
await this.bannerRepository.save(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all banner`;
|
||||
return this.bannerRepository.find();
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} banner`;
|
||||
}
|
||||
|
||||
update(id: number, updateBannerDto: UpdateBannerDto) {
|
||||
return `This action updates a #${id} banner`;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} banner`;
|
||||
async remove(id: number) {
|
||||
const result = await this.bannerRepository.delete(id);
|
||||
if (result.affected === 0) {
|
||||
throw new NotFoundException(`User with ID ${id} not found`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,15 @@
|
||||
export class Banner {}
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||
@Entity()
|
||||
export class BannerEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
url: string;
|
||||
|
||||
@Column()
|
||||
type: number;
|
||||
|
||||
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
|
||||
createTime: Date;
|
||||
}
|
||||
|
22
src/http-exception/http-exception.filter.ts
Normal file
22
src/http-exception/http-exception.filter.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import {
|
||||
ExceptionFilter,
|
||||
Catch,
|
||||
ArgumentsHost,
|
||||
HttpException,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
|
||||
@Catch(HttpException)
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const status = exception.getStatus();
|
||||
|
||||
response.status(status).json({
|
||||
code: 1,
|
||||
message: '服务错误',
|
||||
data: null,
|
||||
});
|
||||
}
|
||||
}
|
@ -1,8 +1,15 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { HttpExceptionFilter } from './http-exception/http-exception.filter';
|
||||
import { TransformInterceptor } from './transoform/transform.interceptor';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
// 设置全局路由前缀,例如 'api'
|
||||
app.setGlobalPrefix('api');
|
||||
app.enableCors();
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
app.useGlobalInterceptors(new TransformInterceptor());
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
@ -1,34 +1,17 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { UploadService } from '../upload/upload.server';
|
||||
import multer = require('multer');
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
|
||||
@Controller('demo')
|
||||
import { OssService } from './oss.service';
|
||||
|
||||
@Controller()
|
||||
export class OssController {
|
||||
constructor(private readonly uploadService: UploadService) {}
|
||||
constructor(private readonly uploadService: OssService) {}
|
||||
|
||||
/**
|
||||
* 上传图片到 本地 和 oss
|
||||
* @param body
|
||||
* 获取oss签名
|
||||
* @returns
|
||||
*/
|
||||
@Post('uploadImage')
|
||||
@UseInterceptors(
|
||||
FileInterceptor('file', {
|
||||
storage: multer.diskStorage({
|
||||
destination: (req, file, cb) => {
|
||||
cb(null, 'D:/oss/image');
|
||||
},
|
||||
filename: (req, file, cb) => {
|
||||
cb(null, file.originalname);
|
||||
},
|
||||
}),
|
||||
}),
|
||||
)
|
||||
async uploadImage(@UploadedFile() file: any): Promise<any> {
|
||||
return await this.uploadService.uploadImage(file);
|
||||
@Get('signature')
|
||||
getOssSignature() {
|
||||
return this.uploadService.getSignature();
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { OssService } from './oss.service';
|
||||
import { OssController } from './oss.controller';
|
||||
import { UploadService } from '../upload/upload.server';
|
||||
|
||||
@Module({
|
||||
controllers: [OssController],
|
||||
providers: [OssService, UploadService],
|
||||
providers: [OssService],
|
||||
})
|
||||
export class OssModule {}
|
||||
|
@ -1,80 +1,38 @@
|
||||
import * as OSS from 'ali-oss';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import ossConfig from '../../config/oss';
|
||||
import * as dayjs from 'dayjs';
|
||||
|
||||
@Injectable()
|
||||
export class OssService {
|
||||
private client: any;
|
||||
public constructor() {
|
||||
this.client = new OSS({
|
||||
async getSignature() {
|
||||
const config = {
|
||||
// 填写你自己的 AccessKey
|
||||
accessKeyId: ossConfig.accessKeyId,
|
||||
accessKeySecret: ossConfig.accessKeySecret,
|
||||
region: ossConfig.region,
|
||||
bucket: ossConfig.bucket,
|
||||
});
|
||||
}
|
||||
// 创建存储空间。
|
||||
private async putBucket() {
|
||||
try {
|
||||
const result = await this.client.putBucket('test');
|
||||
console.log(result);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
// 列举所有的存储空间
|
||||
private async listBuckets() {
|
||||
try {
|
||||
const result = await this.client.listBuckets();
|
||||
console.log(result);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
// 上传文件到oss 并返回 图片oss 地址
|
||||
public async putOssFile(ossPath: string, localPath: string): Promise<string> {
|
||||
let res: any;
|
||||
try {
|
||||
res = await this.client.put(ossPath, localPath);
|
||||
// 将文件设置为公共可读
|
||||
await this.client.putACL(ossPath, 'public-read');
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
return res.url;
|
||||
}
|
||||
/**
|
||||
* 获取文件的url
|
||||
* @param filePath
|
||||
*/
|
||||
public async getFileSignatureUrl(filePath: string): Promise<string> {
|
||||
if (filePath == null) {
|
||||
console.log('get file signature failed: file name can not be empty');
|
||||
return null;
|
||||
}
|
||||
let result = '';
|
||||
try {
|
||||
result = this.client.signatureUrl(filePath, { expires: 36000 });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 上传文件大小校验
|
||||
* @param localPath
|
||||
* @param ossPath
|
||||
* @param size
|
||||
*/
|
||||
public async validateFile(
|
||||
ossPath: string,
|
||||
localPath: string,
|
||||
size: number,
|
||||
): Promise<string> {
|
||||
if (size > 5 * 1024 * 1024) {
|
||||
return;
|
||||
} else {
|
||||
return await this.putOssFile(ossPath, localPath);
|
||||
}
|
||||
accessKeySecret: ossConfig.accessKeySecret, // 存储桶名字
|
||||
bucket: ossConfig.bucket, // 文件存储路径
|
||||
dir: ossConfig.dir,
|
||||
};
|
||||
const client = new OSS(config);
|
||||
const date = new Date(); // 时长加 1 天,作为签名的有限期
|
||||
date.setDate(date.getDate() + 1);
|
||||
const policy = {
|
||||
// 设置签名的有效期,格式为Unix时间戳
|
||||
expiration: date.toISOString(),
|
||||
conditions: [
|
||||
['content-length-range', 0, 10485760000], // 设置上传文件的大小限制
|
||||
],
|
||||
}; // 生成签名,策略等信息
|
||||
const formData = await client.calculatePostSignature(policy); // 生成 bucket 域名,客户端将向此地址发送请求
|
||||
const location = await client.getBucketLocation(ossConfig.bucket);
|
||||
const host = `https://${config.bucket}.${location.location}.aliyuncs.com`; // 响应给客户端的签名和策略等信息
|
||||
return {
|
||||
expire: dayjs().add(1, 'days').unix().toString(),
|
||||
policy: formData.policy,
|
||||
signature: formData.Signature,
|
||||
accessId: formData.OSSAccessKeyId,
|
||||
host,
|
||||
dir: config.dir,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
1
src/sys-user/dto/create-sys-user.dto.ts
Normal file
1
src/sys-user/dto/create-sys-user.dto.ts
Normal file
@ -0,0 +1 @@
|
||||
export class CreateSysUserDto {}
|
4
src/sys-user/dto/update-sys-user.dto.ts
Normal file
4
src/sys-user/dto/update-sys-user.dto.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateSysUserDto } from './create-sys-user.dto';
|
||||
|
||||
export class UpdateSysUserDto extends PartialType(CreateSysUserDto) {}
|
13
src/sys-user/entities/sys-user.entity.ts
Normal file
13
src/sys-user/entities/sys-user.entity.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class SysUserEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column()
|
||||
password: string;
|
||||
}
|
20
src/sys-user/sys-user.controller.spec.ts
Normal file
20
src/sys-user/sys-user.controller.spec.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { SysUserController } from './sys-user.controller';
|
||||
import { SysUserService } from './sys-user.service';
|
||||
|
||||
describe('SysUserController', () => {
|
||||
let controller: SysUserController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [SysUserController],
|
||||
providers: [SysUserService],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<SysUserController>(SysUserController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
42
src/sys-user/sys-user.controller.ts
Normal file
42
src/sys-user/sys-user.controller.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
} from '@nestjs/common';
|
||||
import { SysUserService } from './sys-user.service';
|
||||
import { CreateSysUserDto } from './dto/create-sys-user.dto';
|
||||
import { UpdateSysUserDto } from './dto/update-sys-user.dto';
|
||||
|
||||
@Controller('sys-user')
|
||||
export class SysUserController {
|
||||
constructor(private readonly sysUserService: SysUserService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createSysUserDto: CreateSysUserDto) {
|
||||
return this.sysUserService.create(createSysUserDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.sysUserService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.sysUserService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateSysUserDto: UpdateSysUserDto) {
|
||||
return this.sysUserService.update(+id, updateSysUserDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.sysUserService.remove(+id);
|
||||
}
|
||||
}
|
9
src/sys-user/sys-user.module.ts
Normal file
9
src/sys-user/sys-user.module.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SysUserService } from './sys-user.service';
|
||||
import { SysUserController } from './sys-user.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [SysUserController],
|
||||
providers: [SysUserService],
|
||||
})
|
||||
export class SysUserModule {}
|
18
src/sys-user/sys-user.service.spec.ts
Normal file
18
src/sys-user/sys-user.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { SysUserService } from './sys-user.service';
|
||||
|
||||
describe('SysUserService', () => {
|
||||
let service: SysUserService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [SysUserService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<SysUserService>(SysUserService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
26
src/sys-user/sys-user.service.ts
Normal file
26
src/sys-user/sys-user.service.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateSysUserDto } from './dto/create-sys-user.dto';
|
||||
import { UpdateSysUserDto } from './dto/update-sys-user.dto';
|
||||
|
||||
@Injectable()
|
||||
export class SysUserService {
|
||||
create(createSysUserDto: CreateSysUserDto) {
|
||||
return 'This action adds a new sysUser';
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all sysUser`;
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} sysUser`;
|
||||
}
|
||||
|
||||
update(id: number, updateSysUserDto: UpdateSysUserDto) {
|
||||
return `This action updates a #${id} sysUser`;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} sysUser`;
|
||||
}
|
||||
}
|
23
src/transoform/transform.interceptor.ts
Normal file
23
src/transoform/transform.interceptor.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import {
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
ExecutionContext,
|
||||
CallHandler,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class TransformInterceptor implements NestInterceptor {
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
return next.handle().pipe(
|
||||
map((data) => {
|
||||
return {
|
||||
code: 0,
|
||||
data,
|
||||
message: null,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { OssService } from '../oss/oss.service';
|
||||
|
||||
@Injectable()
|
||||
export class UploadService {
|
||||
constructor(private readonly ossService: OssService) {}
|
||||
// 上传照片
|
||||
async uploadImage(file: any): Promise<any> {
|
||||
try {
|
||||
const ossUrl = await this.ossService.putOssFile(
|
||||
`/image/${file.originalname}`,
|
||||
`D:/oss/image/${file.originalname}`,
|
||||
);
|
||||
return {
|
||||
code: 200,
|
||||
data: ossUrl,
|
||||
message: '上传成功',
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
code: 503,
|
||||
msg: `Service error: ${error}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
export class UserEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
|
@ -16,8 +16,8 @@ export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createUserDto: CreateUserDto) {
|
||||
return this.userService.create(createUserDto);
|
||||
async create(@Body() createUserDto: CreateUserDto) {
|
||||
return await this.userService.create(createUserDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ -25,6 +25,11 @@ export class UserController {
|
||||
return this.userService.findAll();
|
||||
}
|
||||
|
||||
@Post('/login')
|
||||
async login(@Body() user: any) {
|
||||
return await this.userService.login(user);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.userService.findOne(+id);
|
||||
|
@ -1,8 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { UserEntity } from './entities/user.entity';
|
||||
import { UserController } from './user.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserEntity])],
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
})
|
||||
|
@ -1,18 +1,30 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
import { UserEntity } from './entities/user.entity';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
private readonly userRepository: Repository<User>;
|
||||
async create(createUserDto: CreateUserDto): Promise<User> {
|
||||
constructor(
|
||||
@InjectRepository(UserEntity)
|
||||
private readonly userRepository: Repository<UserEntity>,
|
||||
) {}
|
||||
async create(createUserDto: CreateUserDto): Promise<UserEntity> {
|
||||
const newUser = await this.userRepository.create(createUserDto);
|
||||
await this.userRepository.save(newUser);
|
||||
return newUser;
|
||||
}
|
||||
|
||||
async login(user: any) {
|
||||
return {
|
||||
status: 'ok',
|
||||
...user,
|
||||
};
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.userRepository.find();
|
||||
}
|
||||
@ -30,7 +42,8 @@ export class UserService {
|
||||
id,
|
||||
},
|
||||
});
|
||||
return this.userRepository.merge(oldData, updateUserDto);
|
||||
const mergedData = this.userRepository.merge(oldData, updateUserDto);
|
||||
return await this.userRepository.save(mergedData);
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
@ -38,5 +51,6 @@ export class UserService {
|
||||
if (result.affected === 0) {
|
||||
throw new NotFoundException(`User with ID ${id} not found`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user