Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
# Please see pull #3 (#3).
Browse files Browse the repository at this point in the history
  • Loading branch information
silvioprog committed Jan 2, 2013
1 parent 3471703 commit 724fabb
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 16 deletions.
8 changes: 4 additions & 4 deletions demos/db/blog/html/newpost.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
<div class="container" >
<h1>New post</h1>
<fieldset>
<form action="/demos/db/blog/blog.bf/post/" method="post" class="form-horizontal" >
<form action="@action" method="post" class="form-horizontal" >
<div class="control-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" required />
<input type="text" name="title" id="title" value="@title" required />
</div>
<div class="control-group">
<label for="author">Author</label>
<input type="text" name="author" id="author" required />
<input type="text" name="author" id="author" value="@author" required />
</div>
<div class="control-group">
<label for="post">Post</label>
<textarea name="post" id="post" cols="30" rows="10"></textarea>
<textarea name="post" id="post" cols="30" rows="10">@post</textarea>
</div>
<div class="control-group">
<input type="submit" value="Post" class="btn" />
Expand Down
7 changes: 5 additions & 2 deletions demos/db/blog/src/brokers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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.
112 changes: 102 additions & 10 deletions demos/db/blog/src/main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
interface

uses
BrookDBAction, BrookDBUtils, BrookHTTPConsts, JTemplate, HTTPDefs;
BrookDBAction, BrookDBUtils, BrookHTTPConsts, BrookTable, BrookConsts,
BrookActionHelper, Brokers, JTemplate, SysUtils, HTTPDefs, DB, FPJSON;

type

Expand All @@ -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 }
Expand All @@ -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 + '<td>Action</td>';
wtBodyTD: Result := AData + LF + HT + '<td>' + LF + HT + HT +
LinkTo('Edit', TPostEdit, [ID]) + LF + HT + HT +
LinkTo('Remove', TPostRemove, [ID]) + LF + HT + '</td>' + 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.

0 comments on commit 724fabb

Please sign in to comment.