delphimvcframework/samples/swaggerdoc/MyController2U.pas

89 lines
2.4 KiB
ObjectPascal
Raw Normal View History

2019-07-27 20:23:48 +02:00
unit MyController2U;
interface
uses
MVCFramework,
MVCFramework.Commons,
MVCFramework.Swagger.Commons,
MVCFramework.Serializer.Commons,
MVCFramework.Middleware.Authentication.RoleBasedAuthHandler;
2019-07-27 20:23:48 +02:00
type
[MVCNameCase(ncLowerCase)]
TPerson = class
private
FName: string;
FAge: Integer;
FCountry: string;
FCode: Integer;
public
[MVCSwagJsonSchemaField(stInteger, 'code', 'person id', True, False)]
property Code: Integer read FCode write FCode;
[MVCSwagJsonSchemaField('name', 'person name', True, False)]
property Name: string read FName write FName;
[MVCSwagJsonSchemaField('age', 'person age', True, False)]
property Age: Integer read FAge write FAge;
[MVCSwagJsonSchemaField('country', 'Nationality of the person', True, False)]
property Country: string read FCountry write FCountry;
end;
2019-07-27 20:23:48 +02:00
[MVCPath('/person')]
[MVCRequiresAuthentication]
2019-07-27 20:23:48 +02:00
TMyController2 = class(TMVCController)
public
[MVCPath('/($Id)')]
2019-09-25 14:54:02 +02:00
[MVCHTTPMethod([httpGET])]
[MVCSwagSummary('Person', 'List Persons', '66e83aa7-d170-44a7-a502-8f25ddd2a18a')]
[MVCSwagParam(plPath, 'Id', 'Person id', ptInteger, True)]
[MVCSwagParam(plQuery, 'filter', 'Search filter', ptString, False)]
[MVCSwagParam(plQuery, 'per_page', 'Items per page', ptInteger, False)]
2019-09-25 14:54:02 +02:00
[MVCSwagResponses(200, 'Success', TPerson)]
2019-07-27 20:23:48 +02:00
[MVCSwagResponses(500, 'Internal Server Error')]
procedure GetPerson(const Id: Integer);
[MVCPath('')]
2019-09-25 14:54:02 +02:00
[MVCHTTPMethod([httpPOST])]
2019-07-27 20:23:48 +02:00
[MVCSwagSummary('Person', 'Insert Person')]
[MVCSwagParam(plBody, 'entity', 'Person object', TPerson, ptNotDefined, True)]
2019-07-27 20:23:48 +02:00
[MVCSwagResponses(201, 'Created')]
[MVCSwagResponses(401, 'Requires Authentication')]
2019-07-27 20:23:48 +02:00
[MVCSwagResponses(500, 'Internal Server Error')]
[MVCConsumes(TMVCMediaType.APPLICATION_JSON)]
procedure InsertPerson;
end;
implementation
uses
MVCFramework.Controllers.Register;
{ TMyController2 }
procedure TMyController2.GetPerson(const Id: Integer);
2019-07-27 20:23:48 +02:00
var
LPerson: TPerson;
begin
LPerson := TPerson.Create;
LPerson.Code := Id;
2019-07-27 20:23:48 +02:00
LPerson.Name := 'Jo<4A>o Ant<6E>nio Duarte';
LPerson.Age := 26;
LPerson.Country := 'Brasil';
Render(LPerson);
end;
procedure TMyController2.InsertPerson;
var
LPerson: TPerson;
2019-07-27 20:23:48 +02:00
begin
LPerson := Context.Request.BodyAs<TPerson>;
Render(LPerson);
ResponseCreated();
2019-07-27 20:23:48 +02:00
end;
initialization
TControllersRegister.Instance.RegisterController(TMyController2, 'MyServerName');
end.