【GHS】Vue2+Koa2+Typescript前后端框架教程

编程开发   © 文章版权由 admin 解释,禁止匿名转载

#楼主# 2020-12-30

1. 班级管理数据模型创建。数据模型是通过Sequelize的ORM技术实现,关于Sequelize技术,将在后续文章中分享。

在上篇文章中的models文件夹中创建班级模型class.ts,数据结构为:ID,班级名称,班级编码,班主任ID。代码如下:

import { Table, Model, Column, DataType, PrimaryKey } from "sequelize-typescript";
import DbSequelize from "../db_config";

@Table({
tableName: 't_class'
})
export default class Class extends Model {
//唯一标识
@Column({ type: DataType.STRING, primaryKey: true })
id: string;

//班级名称
@Column({ type: DataType.STRING, field: 'class_name' })
className: string;

//班级编码
@Column({ type: DataType.STRING, field: 'class_code' })
classCode: string;

//班主任Id
@Column({ type: DataType.STRING, field: 'head_teacher_id' })
headTeacherId: string;
}

DbSequelize.addModels([Class]);
注:由于尚未讲解Sequelize相关技术,所以这里只需要明白班级结构即可。

2.班级管理服务创建。在services文件夹中创建class.ts,实现最基础的增删改查的服务方法。代码如下:

import Class from '../models/class';
var Op = sequelize.Op;

//班级管理服务
export default class ClassService {
//获取所有班级
async findClassList() {

3.班级管理控制器创建。在controllers文件夹中创建class.ts,实现最基础的增删改查的业务方法。代码如下:

import ClassService from '../services/class';

const clsService = new ClassService();

//班级管理控制器
export default class ClassController {
//查找所有班级
static async findClassList(ctx: any) {
try {
//调用查询列表服务,获取结果
let res = await clsService.findClassList();
ctx.body = {
status: 1,//返回码:1操作成功,0操作错误
data: {
classList: res
}
}
}
catch (err) {
ctx.throw(err.message);
}
}

//根据班级id获取班级详细信息
static async findClassById(ctx: any) {
try {
let id = ctx.request.query.id;
if (!id) {
ctx.body = {
status: 0
}
return;
}
//调用查询详情服务,获取结果
let res = await clsService.findClassById(id);
ctx.body = {
status: 1,

成为第一个回答人

评论

登录后才可发表内容
  • 主题

    106

  • 帖子

    82

  • 关注者

    0

Copyright © 2019 凯特网.   Powered by HYBBS 2.3.4  

Runtime:0.5064s Mem:2049Kb