gpt4 book ai didi

javascript - Restful API typescript 和 promise

转载 作者:行者123 更新时间:2023-12-02 23:53:59 25 4
gpt4 key购买 nike

如何创建一个具有 CRUD 方法和 API 定义的类:它创建、获取、更新和删除任务。不接收 Request 和 Response 。您必须接收经过转换和验证的数据。 不要直接向客户端响应 json。你必须 promise 。

public updateTask (_task: itask) {
return new Promise < ITask > ((resolve, reject) => {
// save
});
}

public deleteTask (_task: itask) {
return new Promise < ITask > ((resolve, reject) => {
// delete
});
}

任何人都可以给我一个关于如何构建此类 Restful api 方法的示例,然后可以使用任何 db sql 或 noSQL 来实现该方法吗?

最佳答案

以下是一些样板代码,可帮助您开始为任务创建数据库层。使用 async/await 始终使用 Promises 并使您的代码更加程序化并且更易于推理。这是你想要的吗?

interface ITask {
id: number;
a: string;
b: number;
}

class TasksManager {

private _dbConnection; // a DB Connection object
private _connected: boolean;
private _dbUsername: string;
private _dbPasssword: string;

constructor() {
// do init stuff here for the DB
}

private async connect() {
// actual code that connects to the DB
}

private async diconnect() {
// actual code that disconnects from the DB
}

private async doQuery(querystring: string) {

// use the dbconnection object to do the query and get the results
let a: Array<string> = [];

return a; // this actually returns a Promise as the function is 'async'
}


/*********************
* PUBLIC API
*********************/

set username(v: string) {
this._dbUsername = v;
}

set password(v: string) {
this._dbPasssword = v;
}

public async deleteTask(t: ITask) {
if (!this._connected) {
await this.connect();
}

// create the querystring and execute the query
let qstring = "DELETE * FROM TASKS WHERE ID = " + t.id;


let result = await this.doQuery(qstring);

// do stuff with the results

if (result[0] == "OK") {
return true; // this actually returns a Promise as the function is 'async'
} else {
return false; // this actually returns a Promise as the function is 'async'
}

}

public async updateTask(t: ITask) {
if (!this._connected) {
await this.connect();
}

// code to update task.....

let result = await this.doQuery("UPDATE TASKS ...."); // this blocks until the query returns

if (result[0] == "OK") {
return true; // this actually returns a Promise as the function is 'async'
} else {
return false; // this actually returns a Promise as the function is 'async'
}
}

public async createTask(a: string, b: number) {
if (!this._connected) {
await this.connect();
}

// code to create querystring and do the query to create the task.....
let result = await this.doQuery("INSERT INTO ...."); // this blocks until the query returns

if (result[0] == "OK") {
return true; // this actually returns a Promise as the function is 'async'
} else {
return false; // this actually returns a Promise as the function is 'async'
}
}
}


// create the manager
let taskManager = new TasksManager();

// create new task
taskManager.createTask("one", 2).then((result) => {
if (result == true) {
console.log("task created!!");
}
})
.catch((err) => {
throw `Failed to create task. reason: ${err}`;
});

关于javascript - Restful API typescript 和 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55494772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com