毕业设计第九周

本周主要的内容是关于数据格式的定义和接口实现

周六日

主要在学习数据库的连接以及其他相关知识以及typeorm。
网络上关于mongodb的可用资料比较少,于是在询问同学以后把数据库更改为MySQL了。之前也有过MySQL的基础,于是就更改了自己的数据库类型。

周一

根据给出的信用评分表,个人用户在进行贷款风险评估时,需要填写以下字段内容:
在这里插入图片描述

1.需要引用的包是typeorm,它是用TypeScript编写的,因此它与Nest框架非常兼容。
首先,需要安装所有必需的依赖项:
在终端输入以下命令:

npm install --save @nestjs/typeorm typeorm mysql

在这里插入图片描述
安装过程完成后,我们可以将其TypeOrmModule导入到根目录中ApplicationModule。
app.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: true,
    }),
  ],
})
export class ApplicationModule {}

在这里插入图片描述

TypeORM支持库的设计模式,使每个实体都有自己的仓库。可以从数据库连接获取这些存储库。

首先,我们至少需要一个实体。

2.定义实体类GradeEntity,创建文件grade.entity.ts,
语法:

  @Column('int', { nullable: false, comment: '客户名称' })
  name: number;

这个时typeorm提供的装饰器,用来和数据库的字段进行映射
@Column就是一个普通字段的意思 int就是 数据库中这个字段的类型 :数字 ,
nullable:数据库中该字段是否可以为null
comment就是 备注
name:number 由于js没有int这种,所以数字就是统一的number来代表类型

grade.entity.ts中的代码:

import { Column, PrimaryGeneratedColumn, Entity } from 'typeorm';
@Entity('grade')
export class GradeEntity {
  @PrimaryGeneratedColumn({ comment: '主键' })
  grade_id: number;

  @Column('int', { nullable: false, comment: '所属用户的id' })
  user_id: number;

   
  @Column('int', { nullable: false, comment: '客户名称' })
  name: number;
 
  @Column('int', { nullable: false, comment: '资产余额' })
  balance: number;
  
  @Column('int', { nullable: false, comment: '原有信用等级' })
  old_grade: number;
  
 
  @Column('int', { nullable: false, comment: '资产总额' })
  total_assets: number;

  @Column('int', { nullable: false, comment: '负债总额' })
  total_liabilities: number;

  @Column('int', { nullable: false, comment: '所有者权益' })
  owner_equity: number;
  // 顶部
  
  // 基本信息
  @Column('int', { nullable: false, comment: '年龄' })
  age: number;

  @Column('int', { nullable: false, comment: '学历' })
  edu: number;

  @Column('int', { nullable: false, comment: '婚姻' })
  marriage: number;

  @Column('int', { nullable: false, comment: '健康状况' })
  health: number;

  @Column('int', { nullable: false, comment: '子女情况' })
  children: number;

  @Column('int', { nullable: false, comment: '所在单位类型' })
  unit_type: number;
  // 基本信息结束

  // 职业职位
  @Column('int', { nullable: false, comment: '最近职业从业时间' })
  lately_time: number;

  @Column('int', {
    nullable: false,
    comment: '社会职务',
  })
  hold: number;

  @Column('int', { nullable: false, comment: '住所' })
  location: number;
  // 职业职位结束

  // 偿债能力

  @Column('int', { nullable: false, comment: '从事职业前景' })
  prospect: number;

  @Column('int', { nullable: false, comment: '社保缴纳记录' })
  social_security: number;

  @Column('int', { nullable: false, comment: '家庭净资产' })
  family_assets: number;

  @Column('int', {
    nullable: false,
    comment: '本人收入或家庭成员最高个人收入',
  })
  own_income: number;

  @Column('int', { nullable: false, comment: '家庭人均收入' })
  family_income: number;
  // 偿债能力

  // 信用记录
  @Column('int', { nullable: false, comment: '本人及配偶有无贷款记录' })
  own_mate_loan: number;

  @Column('int', { nullable: false, comment: '有无还款付息逾期' })
  overdue_repayment: number;

  @Column('int', { nullable: false, comment: '最长逾期记录' })
  max_overdue: number;

  @Column('int', { nullable: false, comment: '有无涉诉' })
  involved_appeal: number;

  @Column('int', { nullable: false, comment: '对外担保' })
  external_guarantee: number;
  // 信用记录结束

  @Column({
    type: 'timestamp',
    comment: '创建时间',
    default: () => new Date().valueOf(),
  })
  create_time: number;

  //  TODO: 自动插入时间问题
  @Column({
    type: 'timestamp',
    default: () => new Date().valueOf(),
    comment: '更新时间',
  })
  update_time: number;


}

周二

数据传输对象

关于数据传输的实现,常用方式为定义数据传输对象(DTO),以下是DTO的定义

数据传输对象(DTO),(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是数据访问对象从数据库中检索数据。数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具有任何行为除了存储和检索的数据(访问和存取器)。

需要上传到服务器的数据有很多,故而我们需要设置一些内容去定判断填写的字段,包括数据格式、是否必填等。
根据以上内容信息,结合语法:

  @ApiProperty({ example: 1 }) // swagger里面的用法,可以给出示例
  @IsNumber()   //判断是否为数字
  @IsNotEmpty({ message: '用户id不能为空' }) //判断是否为空,空的话返回用户id不能为空的信息
  readonly user_id: number; //user_id权限为只读,数据类型是number.

结合语法,根据以上的表格内容,我们可以建立一个新的文件,gradeDto.ts:
包括了增加、更新的类

import { ApiProperty } from '@nestjs/swagger';
import { IsIn, IsNotEmpty, IsNumber } from 'class-validator';

export {
  CreateGradeDto,
  UpdateGradeDTO,
  DeleteByUserIdDto,
  DeleteByGradeIdDto,
};
创建:CreateGradeDto
class CreateGradeDto {
  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '所属行业不能为空' })
  readonly vocation: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '原有评级不能为空' })
  readonly old_grade: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '所有者权益不能为空' })
  readonly owner_equity: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '资产余额不能为空' })
  readonly balance: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '负债总额不能为空' })
  readonly total_liabilities: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '年龄不能为空' })
  readonly age: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '学历不能为空' })
  readonly edu: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '婚姻不能为空' })
  readonly marriage: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '健康状态不能为空' })
  readonly health: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '子女不能为空' })
  readonly children: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '所属单位不能为空' })
  readonly belong: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '最近从业时间不能为空' })
  readonly lately_time: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '有无官方或者民间担任职务不能为空' })
  readonly hold: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '从事职业前景不能为空' })
  readonly vocationing: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '家庭净资产不能为空' })
  readonly family_assets: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '本人收入或者家庭成员收入最高不能为空' })
  readonly own_income: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '家庭人均年收入不能为空' })
  readonly family_income: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '总资产不能为空' })
  readonly total_assets: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '社保缴纳记录不能为空' })
  readonly social_security: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '本人及配偶有无贷款记录不能为空' })
  readonly own_mate_loan: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '最长逾期记录不能为空' })
  readonly max_overdue: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '有无还款付息逾期不能为空' })
  readonly overdue_repayment: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '有无涉诉不能为空' })
  readonly involved_appeal: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '对外担保不能为空' })
  readonly external_guarantee: number;

  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '用户id不能为空' })
  readonly user_id: number;
}
更新:UpdateGradeDTO
class UpdateGradeDTO extends CreateGradeDto {
  @ApiProperty({ example: 3 })
  @IsNumber()
  @IsNotEmpty({ message: '更新状态不能为空' })
  @IsIn([0, 1, 2, 3], {
    message: '状态只能是0:待审核 1:已审核2审核通过,3:审核失败其中一个',
  })
  readonly status: number;

  @ApiProperty({ example: 200 })
  @IsNumber()
  @IsNotEmpty({ message: '评分id不能为空' })
  readonly grade_id: number;
}


删除 DeleteByUserIdDto,DeleteByGradeIdDto,
class DeleteByUserIdDto {
  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '用户id不能为空' })
  readonly user_id: number;
}

class DeleteByGradeIdDto {
  @ApiProperty({ example: 1 })
  @IsNumber()
  @IsNotEmpty({ message: '评分id不能为空' })
  readonly grade_id: number;
}

在这里插入图片描述

周三

创建服务器:grade.service.ts

nest g s grade
import { HttpException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateGradeDto } from '../dto/gradeDto';
import { GradeEntity } from '../entity/grade.entity';
import { Repository } from 'typeorm';

@Injectable()
export class GradeService {
  constructor(
    @InjectRepository(GradeEntity)
    private grade: Repository<GradeEntity>,
  ) {}

  /**
   * 创建评级
   * @param createGradeDto
   * @returns
   */
  async insert(createGradeDto: CreateGradeDto) {
    // TODO : 是否要先判定用户存不存在
    return await this.grade.insert(createGradeDto);
  }
  /**
   * 评级列表
   * @returns
   */
  async find() {
    return await this.grade.find();
  }

  /**
   * 根据用户查找评级
   * @returns
   */
  async findOneByUserId(user_id: number) {
    const gradeListByUserId = await this.grade.find({
      where: {
        user_id,
      },
    });
    if (!gradeListByUserId || gradeListByUserId.length <= 0) {
      throw new HttpException('用户id不正确', 404);
    }
    return gradeListByUserId;
  }
}

周四

根据以下命令创建控制器:grade.controller.ts

nest g co grade

grade.controller.ts中,输入以下代码:

import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

import { GradeService } from '../service/grade.service';

import {
  CreateGradeDto,
  UpdateGradeDTO,
  DeleteByUserIdDto,
  DeleteByGradeIdDto,
} from '../dto/gradeDto';
@ApiTags('评级管理')
@Controller('grade')
export class GradeController {
  constructor(private readonly gradeService: GradeService) {}

  @ApiOperation({ description: '创建用户评分' })
  @Post()
  async create(@Body() createGradeDto: CreateGradeDto) {
    return await this.gradeService.insert(createGradeDto);
  }

  @ApiOperation({ description: '所有评分列表' })
  @Get('/list')
  async find() {
    return this.gradeService.find();
  }

  @ApiOperation({ description: '根据用户主键查看评分' })
  @Get(':user_id')
  async findOne(@Param('user_id') user_id: number) {
    return this.gradeService.findOneByUserId(user_id);
  }

  @ApiOperation({ description: '更新评分状态' })
  @Post('/update')
  async updateStatusByUserId(@Body() updateGradeDTO: UpdateGradeDTO) {
    return updateGradeDTO;
  }

  @ApiOperation({ description: '根据用户主键删除该评分' })
  @Post('/delete_by_user_id')
  async deleteByUserId(@Body() deleteByUserIdDto: DeleteByUserIdDto) {
    return deleteByUserIdDto;
  }

  @ApiOperation({ description: '根据评分主键删除该评分' })
  @Post('/delete_by_grade_id')
  async deleteById(@Body() deleteByGradeIdDto: DeleteByGradeIdDto) {
    return deleteByGradeIdDto;
  }
}

运用工具Navicat for MySQL
这一个专门做MySQL数据库管理的平台
创建表格:

在这里插入图片描述
需要实现的总体的逻辑为:
service中使用entity查询数据库,然后把查询到的结果返回给controller ,当用户根据请求的地址匹配到了controller中定义的地址,那么controller中的方法就调用了service的,service的通过ORM 把数据库里的东西就给前端了
本周总结:
还有很多的东西没搞,这周只是定义了个人信用评分的表单数据格式,以及学习了数据库连接。下周继续细化个人评分部分代码,以及学习个人登录的实现。

问题求助:
老师,可以少做几个吗,就是信用评分表格里面的东西好多,就是还有很多很多的公式,俺哭了。。。。俺的注册和登陆也都不会。
在这里插入图片描述
可不可以就只提供个人贷款呀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值