mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 15:55:54 +01:00
117 lines
2.6 KiB
ObjectPascal
117 lines
2.6 KiB
ObjectPascal
unit MainControllerU;
|
|
|
|
interface
|
|
|
|
uses
|
|
MVCFramework, MVCFramework.Commons;
|
|
|
|
type
|
|
|
|
[MVCPath('/api')]
|
|
TMainController = class(TMVCController)
|
|
public
|
|
[MVCPath('/')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure Index;
|
|
|
|
[MVCPath('/error')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure DoError;
|
|
|
|
[MVCPath('/reversedstrings/($Value)')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure GetReversedString(const Value: String);
|
|
protected
|
|
procedure OnBeforeAction(Context: TWebContext; const AActionName: string; var Handled: Boolean); override;
|
|
procedure OnAfterAction(Context: TWebContext; const AActionName: string); override;
|
|
|
|
public
|
|
//Sample CRUD Actions for a "Customer" entity
|
|
[MVCPath('/customers')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure GetCustomers;
|
|
|
|
[MVCPath('/customers/($id)')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure GetCustomer(id: Integer);
|
|
|
|
[MVCPath('/customers')]
|
|
[MVCHTTPMethod([httpPOST])]
|
|
procedure CreateCustomer;
|
|
|
|
[MVCPath('/customers/($id)')]
|
|
[MVCHTTPMethod([httpPUT])]
|
|
procedure UpdateCustomer(id: Integer);
|
|
|
|
[MVCPath('/customers/($id)')]
|
|
[MVCHTTPMethod([httpDELETE])]
|
|
procedure DeleteCustomer(id: Integer);
|
|
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.SysUtils, MVCFramework.Logger, System.StrUtils, MVCFramework.Serializer.Commons;
|
|
|
|
procedure TMainController.Index;
|
|
begin
|
|
//use Context property to access to the HTTP request and response
|
|
Render(StrDict(['hello'],['world']));
|
|
end;
|
|
|
|
procedure TMainController.GetReversedString(const Value: String);
|
|
begin
|
|
Render(System.StrUtils.ReverseString(Value.Trim));
|
|
end;
|
|
|
|
procedure TMainController.OnAfterAction(Context: TWebContext; const AActionName: string);
|
|
begin
|
|
{ Executed after each action }
|
|
inherited;
|
|
end;
|
|
|
|
procedure TMainController.OnBeforeAction(Context: TWebContext; const AActionName: string; var Handled: Boolean);
|
|
begin
|
|
{ Executed before each action
|
|
if handled is true (or an exception is raised) the actual
|
|
action will not be called }
|
|
inherited;
|
|
end;
|
|
|
|
//Sample CRUD Actions for a "Customer" entity
|
|
procedure TMainController.GetCustomers;
|
|
begin
|
|
//todo: render a list of customers
|
|
end;
|
|
|
|
procedure TMainController.GetCustomer(id: Integer);
|
|
begin
|
|
//todo: render the customer by id
|
|
end;
|
|
|
|
procedure TMainController.CreateCustomer;
|
|
|
|
begin
|
|
//todo: create a new customer
|
|
end;
|
|
|
|
procedure TMainController.UpdateCustomer(id: Integer);
|
|
begin
|
|
//todo: update customer by id
|
|
end;
|
|
|
|
procedure TMainController.DeleteCustomer(id: Integer);
|
|
begin
|
|
//todo: delete customer by id
|
|
end;
|
|
|
|
|
|
|
|
procedure TMainController.DoError;
|
|
begin
|
|
raise EMVCException.Create('This is an error');
|
|
end;
|
|
|
|
end.
|