rest开发步骤
rest开发步骤
1)代码工厂自动生成REST CRUD代码。
自动生成的代码:
unit rest.tunit; //代码由代码工厂自动生成 //2023-01-28 13:20:07 {$I def.inc} interface uses {$IFDEF firedac} db.firedac, db.firedacPool, {$ENDIF} {$IFDEF unidac}db.unidac, db.unidacpool, {$ENDIF} classes, db, System.NetEncoding, serialize, rtti.execfunc, system.JSON.Serializers, yn.log, SysUtils; type Ttunit = record [Serialize(1)] unitid: string; [Serialize(2)] unitname: string; end; TtunitArray = record [Serialize(1)] status: integer; [Serialize(2)] exception: string; [Serialize(3)] message: string; [Serialize(4)] tunits: TArray<Ttunit>; end; TRes = record [Serialize(1)] status: integer; [Serialize(2)] exception: string; [Serialize(3)] message: string; end; type TFunc2872 = class(TFunc) function select(url: string; body: rawbytestring): string; function insert(url: string; body: rawbytestring): string; function update(url: string; body: rawbytestring): string; function delete(url: string; body: rawbytestring): string; end; implementation function TFunc2872.select(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; rows: TtunitArray; i: integer; res: TRes; begin try try pool := GetDBPool('1'); db := pool.Lock; db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'select * from tunit'; db.qry.Open; SetLength(rows.tunits, db.qry.RecordCount); db.qry.First; i := 0; while not db.qry.Eof do begin rows.tunits[i].unitid := db.qry.fieldbyname('unitid').asstring; rows.tunits[i].unitname := db.qry.fieldbyname('unitname').asstring; inc(i); db.qry.Next; end; rows.status := 200; rows.message := 'success'; result := Tserial.marshal<TtunitArray>(rows); except on E: Exception do begin res.status := 500; res.exception := E.message; result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; function TFunc2872.insert(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; arr: tarray<string>; res: TRes; begin try try var rows: TtunitArray; rows := Tserial.unmarshal<TtunitArray>(body); arr := url.Split(['/']); pool := GetDBPool('1'); db := pool.Lock; db.startTrans; for var row: Ttunit in rows.tunits do begin db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'insert into tunit (unitid,unitname) values (:unitid,:unitname)'; db.qry.ParamByName('unitid').AsString := row.unitid; db.qry.ParamByName('unitname').AsString := row.unitname; db.qry.ExecSQL; end; db.commitTrans; res.status := 200; res.message := 'success'; Result := Tserial.marshal<TRes>(res); except on E: Exception do begin db.rollbackTrans; res.status := 500; res.exception := E.Message; Result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; function TFunc2872.update(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; arr: tarray<string>; res: TRes; begin try try var rows: TtunitArray; rows := Tserial.unmarshal<TtunitArray>(body); arr := url.Split(['/']); pool := GetDBPool('1'); db := pool.Lock; db.startTrans; for var row: Ttunit in rows.tunits do begin db.qry.Close; db.qry.SQL.Clear; db.qry.SQL.Text := 'update tunit set unitid=:unitid,unitname=:unitname where unitid=:key0'; db.qry.ParamByName('unitid').AsString := row.unitid; db.qry.ParamByName('key0').value := row.unitid; db.qry.ParamByName('unitname').AsString := row.unitname; db.qry.ExecSQL; end; db.commitTrans; res.status := 200; res.message := 'success'; Result := Tserial.marshal<TRes>(res); except on E: Exception do begin db.rollbackTrans; res.status := 500; res.exception := E.Message; Result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; function TFunc2872.delete(url: string; body: rawbytestring): string; var db: tdb; pool: tdbpool; arr: tarray<string>; res: TRes; begin try try arr := url.Split(['/']); pool := GetDBPool('1'); db := pool.Lock; db.qry.Close; db.qry.SQL.Clear; var where: string := ' where ' + TNetEncoding.URL.Decode(arr[3]); db.qry.SQL.Text := 'delete from tunit' + where; db.qry.ExecSQL; res.status := 200; res.message := 'success'; Result := Tserial.marshal<TRes>(res); except on E: Exception do begin res.status := 500; res.exception := E.Message; Result := Tserial.marshal<TRes>(res); end; end; finally pool.Unlock(db); end; end; initialization RegisterClass(TFunc2872); end.
2)添加路由配置
3)将自动生成的单元添加进工程里面,并编译工程。
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/17070169.html