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)将自动生成的单元添加进工程里面,并编译工程。

 

posted @   delphi中间件  阅读(168)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示