From 724fabb480d866673e43a08aee7dc4ffaf51af47 Mon Sep 17 00:00:00 2001 From: Silvio Clecio Date: Wed, 2 Jan 2013 00:30:19 -0200 Subject: [PATCH] # Please see pull #3 (https://github.com/silvioprog/brookframework/pull/3). --- demos/db/blog/html/newpost.html | 8 +-- demos/db/blog/src/brokers.pas | 7 +- demos/db/blog/src/main.pas | 112 +++++++++++++++++++++++++++++--- 3 files changed, 111 insertions(+), 16 deletions(-) diff --git a/demos/db/blog/html/newpost.html b/demos/db/blog/html/newpost.html index e1130ab..2766840 100644 --- a/demos/db/blog/html/newpost.html +++ b/demos/db/blog/html/newpost.html @@ -11,18 +11,18 @@

New post

-
+
- +
- +
- +
diff --git a/demos/db/blog/src/brokers.pas b/demos/db/blog/src/brokers.pas index 2657ea9..bc6ee7d 100644 --- a/demos/db/blog/src/brokers.pas +++ b/demos/db/blog/src/brokers.pas @@ -8,12 +8,15 @@ interface BrookFCLCGIBroker, BrookUtils, BrookHTTPConsts, BrookSQLdbBroker, SQLite3Conn; +const + PUBLIC_HTML = '/your/public/html/directory/'; + implementation initialization BrookSettings.Configuration := 'db.cfg'; BrookSettings.Charset := BROOK_HTTP_CHARSET_UTF_8; - BrookSettings.Page404 := '404.html'; - BrookSettings.Page500 := '500.html'; + BrookSettings.Page404 := PUBLIC_HTML + '404.html'; + BrookSettings.Page500 := PUBLIC_HTML + '500.html'; end. diff --git a/demos/db/blog/src/main.pas b/demos/db/blog/src/main.pas index c9b2496..ccb7666 100644 --- a/demos/db/blog/src/main.pas +++ b/demos/db/blog/src/main.pas @@ -5,7 +5,8 @@ interface uses - BrookDBAction, BrookDBUtils, BrookHTTPConsts, JTemplate, HTTPDefs; + BrookDBAction, BrookDBUtils, BrookHTTPConsts, BrookTable, BrookConsts, + BrookActionHelper, Brokers, JTemplate, SysUtils, HTTPDefs, DB, FPJSON; type @@ -25,18 +26,36 @@ TActionView = class(TBrookDBAction) { THome } THome = class(TActionView) + function GridCallback(ADataSet: TDataSet; + const AWritingType: TBrookHTMLTableWritingState; + const APosition, AMax: Integer; var AData: string): string; public procedure Get; override; end; - { TPost } + { TPostAdd } - TPost = class(TActionView) + TPostAdd = class(TActionView) public procedure Get; override; procedure Post; override; end; + { TPostEdit } + + TPostEdit = class(TActionView) + public + procedure Get; override; + procedure Post; override; + end; + + { TPostRemove } + + TPostRemove = class(TActionView) + public + procedure Get; override; + end; + implementation { TActionView } @@ -63,34 +82,107 @@ procedure TActionView.DoRequest(ARequest: TRequest; AResponse: TResponse); procedure TActionView.LoadHtml(const AHtml: string); begin - FTemplate.LoadFromFile(AHtml + '.html'); + FTemplate.LoadFromFile(PUBLIC_HTML + AHtml + '.html'); end; { THome } +function THome.GridCallback(ADataSet: TDataSet; + const AWritingType: TBrookHTMLTableWritingState; + const APosition, AMax: Integer; var AData: string): string; +var + ID: string; +begin + if (APosition = AMax) and (AWritingType in [wtHeadTD, wtBodyTD]) then + begin + ID := ADataSet.FieldByName('id').AsString; + case AWritingType of + wtHeadTD: Result := AData + LF + HT + HT + 'Action'; + wtBodyTD: Result := AData + LF + HT + '' + LF + HT + HT + + LinkTo('Edit', TPostEdit, [ID]) + LF + HT + HT + + LinkTo('Remove', TPostRemove, [ID]) + LF + HT + '' + LF; + end; + end + else + Result := AData; +end; + procedure THome.Get; begin LoadHtml('home'); - Template.Fields.Add('menu', UrlFor(TPost, ['new'])); + Template.Fields.Add('menu', UrlFor(TPostAdd, ['new'])); Template.Fields.Add('post', BrookDataSetToHTMLTable(Table.Open.DataSet, [], - 'table table-bordered table-hover')); + 'table table-bordered table-hover', 1, [], @GridCallback)); end; -{ TPost } +{ TPostAdd } -procedure TPost.Get; +procedure TPostAdd.Get; begin LoadHtml('newpost'); + Template.Fields.Add('title', ES); + Template.Fields.Add('author', ES); + Template.Fields.Add('post', ES); + Template.Fields.Add('action', UrlFor(TPostAdd)); end; -procedure TPost.Post; +procedure TPostAdd.Post; begin Table.Insert(Fields).Apply; Redirect(UrlFor(THome), BROOK_HTTP_STATUS_CODE_FOUND); end; +{ TPostEdit } + +procedure TPostEdit.Get; +var + ID: TJSONStringType; + Row: TBrookTable; +begin + ID := Values['id'].AsString; + Row := Table.Get('id', ID); + if not Row.EOF then + begin + LoadHtml('newpost'); + Template.Fields.Add('title', Row['title'].AsString); + Template.Fields.Add('author', Row['author'].AsString); + Template.Fields.Add('post', Row['post'].AsString); + Template.Fields.Add('action', UrlFor(TPostEdit, [ID])); + end + else + Redirect(UrlFor(THome), BROOK_HTTP_STATUS_CODE_FOUND); +end; + +procedure TPostEdit.Post; +var + ID: TJSONStringType; + Row: TBrookTable; +begin + ID := Values['id'].AsString; + Row := Table.Get('id', ID); + if not Row.EOF then + Row.Edit(Fields).Apply; + Redirect(UrlFor(THome), BROOK_HTTP_STATUS_CODE_FOUND); +end; + +{ TPostRemove } + +procedure TPostRemove.Get; +var + ID: TJSONStringType; + Row: TBrookTable; +begin + ID := Values['id'].AsString; + Row := Table.Get('id', ID); + if not Row.EOF then + Row.Delete.Apply; + Redirect(UrlFor(THome), BROOK_HTTP_STATUS_CODE_FOUND); +end; + initialization THome.Register('post', '*', True); - TPost.Register('post', '/post/'); + TPostAdd.Register('post', '/post/add/'); + TPostEdit.Register('post', '/post/edit/:id'); + TPostRemove.Register('post', '/post/remove/:id'); end.