我们一起来完成以下步骤:
-
-
-
-
-
-
-
#controllers/employeeController.ts
import { Context } from "https://deno.land/x/oak/mod.ts";
import { Employee } from "../models/employee.ts";
import employeeService from "../services/employeeService.ts";
const { cwd } = Deno;
class Controller {
static async hello(ctx: any){
//cwd获取当前工程目录
//注意 ' !== `
ctx.render(`${cwd()}/views/index.ejs`,{
title:"Testing",
data:{name:" Deepincoding"}
});
}
static async save(ctx: Context){
const bodyValue = await ctx.request.body();
let employee:Employee={
id: 0,
name: bodyValue.value.name,
age: bodyValue.value.age
}
const savedEmployee = await employeeService.save(employee);
console.log("savedEmployee:"+savedEmployee);
ctx.response.body = savedEmployee;
}
static async update(ctx: Context){
var bodyValue = await ctx.request.body();
let updateEmployee: Employee ={
id:bodyValue.value.id,
name:bodyValue.value.name,
age:bodyValue.value.age
}
const updatedEmployee = await employeeService.update(updateEmployee);
ctx.response.body=updatedEmployee;
}
static async findById(ctx: any){
const id = ctx.params.id;
console.log("id:"+id);
const employee = await employeeService.findById(id);
ctx.response.body=employee;
}
static async findAll(ctx: any){
const employees = await employeeService.findAll();
ctx.response.body=employees;
}
static async deleteById(ctx: any){
const id = ctx.params.id;
const success = await employeeService.deleteById(id);
ctx.response.body=success;
}
}
export default Controller;
#database/database.ts
import { Client } from "https://deno.land/x/mysql/mod.ts"
const client = await new Client().connect({
hostname: "127.0.0.1",
username: "root",
db: "deno",
poolSize: 3,
password: "123456"
});
export default client;
#models/employee.ts
export interface Employee{
id: number,
name: string,
age: number
}
#repositories/employeeRepo.ts
import client from "../database/database.ts";
import { Employee } from "../models/employee.ts";
class employeeRepo{
//新增
static async save (employee: Employee){
let result = await client.execute(
`insert into employee(name,age) values(?,?)`,
[employee.name,employee.age]);
employee.id = Number(result.lastInsertId);
console.log(result);
return employee;
}
//更新
static async update(employee :Employee){
let result = await client.execute(`update employee set name = ?,age= ? where id = ?`,
[employee.name, employee.age,employee.id]);
console.log(result);
return employee;
}
//根据ID获取
static async findById(id :number){
const employee = await client.query(
"select * from employee where id = ?",
[id]
);
console.log(employee);
return employee;
}
//获取所有
static async findAll(){
const employees = await client.query(
"select * from employee"
);
console.log(employees);
return employees;
}
//根据ID删除
static async deleteById(id:number){
let result = await client.execute(`delete from employee where id = ?`, [id]);
console.log(result);
return true;
}
}
export default employeeRepo;
#router/employeeRouter.ts
import { Router } from "https://deno.land/x/oak/mod.ts";
import Controller from "../controllers/employeeController.ts";
const router = new Router();
router.get("/",Controller.hello);
router.post("/save",Controller.save);
router.put("/update",Controller.update);
router.get("/findById/:id",Controller.findById);
router.get("/findAll",Controller.findAll);
router.delete("/deleteById/:id",Controller.deleteById);
export default router;
#services/employeeService.ts
import { Employee } from "../models/employee.ts";
import emlpoyeeRepo from "../repositories/employeeRepo.ts";
class employeeService{
static async save(employee: Employee){
return emlpoyeeRepo.save(employee);
}
static async update(employee: Employee){
return emlpoyeeRepo.update(employee);
}
static async findById(id: number){
return emlpoyeeRepo.findById(id);
}
static async findAll(){
return emlpoyeeRepo.findAll();
}
static async deleteById(id: number){
return emlpoyeeRepo.deleteById(id);
}
}
export default employeeService;
#main.ts
import { Application } from "https://deno.land/x/oak/mod.ts"
import {viewEngine,engineFactory,adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
import router from "./routers/employeeRouter.ts";
const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();
const app = new Application();
app.use(viewEngine(oakAdapter,ejsEngine));
app.use(router.routes());
app.use(router.allowedMethods());
console.log("Server Port 8000");
await app.listen({port: 8000 })