mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-16 00:05:53 +01:00
04b83cc0f5
* Enable working with listeners in a DI context. * Class Changes to provide a better understanding of the functionality; * Removal singletons variables (not the framework that must manage it); * README update;
122 lines
2.2 KiB
ObjectPascal
122 lines
2.2 KiB
ObjectPascal
unit MVCFramework.Tests.AppController;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
System.Generics.Collections,
|
|
System.Classes,
|
|
MVCFramework,
|
|
MVCFramework.Commons,
|
|
MVCFramework.Server;
|
|
|
|
type
|
|
|
|
TAppUser = class
|
|
strict private
|
|
FCod: Integer;
|
|
FName: string;
|
|
FPass: string;
|
|
public
|
|
property Cod: Integer read FCod write FCod;
|
|
property Name: string read FName write FName;
|
|
property Pass: string read FPass write FPass;
|
|
end;
|
|
|
|
[MVCPath('/')]
|
|
TAppController = class(TMVCController)
|
|
public
|
|
[MVCPath('/hello')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure HelloWorld(ctx: TWebContext);
|
|
|
|
[MVCPath('/user')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure GetUser(ctx: TWebContext);
|
|
|
|
[MVCPath('/user/save')]
|
|
[MVCHTTPMethod([httpPOST])]
|
|
procedure PostUser(ctx: TWebContext);
|
|
|
|
[MVCPath('/users')]
|
|
[MVCHTTPMethod([httpGET])]
|
|
procedure GetUsers(ctx: TWebContext);
|
|
|
|
[MVCPath('/users/save')]
|
|
[MVCHTTPMethod([httpPOST])]
|
|
procedure PostUsers(ctx: TWebContext);
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TAppController }
|
|
|
|
procedure TAppController.GetUser(ctx: TWebContext);
|
|
var
|
|
LUser: TAppUser;
|
|
begin
|
|
LUser := TAppUser.Create;
|
|
LUser.Cod := 1;
|
|
LUser.Name := 'Ezequiel';
|
|
LUser.Pass := '123';
|
|
|
|
Render(LUser);
|
|
end;
|
|
|
|
procedure TAppController.GetUsers(ctx: TWebContext);
|
|
var
|
|
LUsers: TObjectList<TAppUser>;
|
|
LUser: TAppUser;
|
|
I: Integer;
|
|
begin
|
|
LUsers := TObjectList<TAppUser>.Create(True);
|
|
|
|
for I := 0 to 10 do
|
|
begin
|
|
LUser := TAppUser.Create;
|
|
LUser.Cod := I;
|
|
LUser.Name := 'Ezequiel ' + IntToStr(I);
|
|
LUser.Pass := IntToStr(I);
|
|
|
|
LUsers.Add(LUser);
|
|
end;
|
|
|
|
Self.Render<TAppUser>(LUsers);
|
|
end;
|
|
|
|
procedure TAppController.HelloWorld(ctx: TWebContext);
|
|
begin
|
|
Render('Hello World called with GET');
|
|
end;
|
|
|
|
procedure TAppController.PostUser(ctx: TWebContext);
|
|
var
|
|
LUser: TAppUser;
|
|
begin
|
|
LUser := ctx.Request.BodyAs<TAppUser>();
|
|
|
|
if (LUser.Cod > 0) then
|
|
Render('Sucess!')
|
|
else
|
|
Render('Error!');
|
|
|
|
LUser.Free;
|
|
end;
|
|
|
|
procedure TAppController.PostUsers(ctx: TWebContext);
|
|
var
|
|
LUsers: TObjectList<TAppUser>;
|
|
begin
|
|
LUsers := ctx.Request.BodyAsListOf<TAppUser>();
|
|
LUsers.OwnsObjects := True;
|
|
|
|
if (LUsers.Count > 0) then
|
|
Render('Sucess!')
|
|
else
|
|
Render('Error!');
|
|
|
|
LUsers.Free;
|
|
end;
|
|
|
|
end.
|