2019-07-27 20:23:48 +02:00
|
|
|
|
unit MyController2U;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2019-10-30 16:02:30 +01:00
|
|
|
|
System.Generics.Collections,
|
2019-07-27 20:23:48 +02:00
|
|
|
|
MVCFramework,
|
|
|
|
|
MVCFramework.Commons,
|
2019-07-29 22:49:31 +02:00
|
|
|
|
MVCFramework.Swagger.Commons,
|
2019-07-31 13:40:11 +02:00
|
|
|
|
MVCFramework.Serializer.Commons,
|
2020-12-29 13:49:43 +01:00
|
|
|
|
MVCFramework.Middleware.Authentication.RoleBasedAuthHandler,
|
|
|
|
|
MVCFramework.Nullables;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
type
|
2019-11-03 16:16:35 +01:00
|
|
|
|
|
2019-10-30 16:02:30 +01:00
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
2021-03-04 14:58:52 +01:00
|
|
|
|
TAddress = class(TInterfacedObject)
|
2019-10-30 16:02:30 +01:00
|
|
|
|
private
|
|
|
|
|
FStreet: string;
|
|
|
|
|
FNumber: Integer;
|
|
|
|
|
FCity: string;
|
2020-12-29 13:49:43 +01:00
|
|
|
|
FPostalCode: NullableString;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
public
|
|
|
|
|
property Street: string read FStreet write FStreet;
|
|
|
|
|
property Number: Integer read FNumber write FNumber;
|
|
|
|
|
property City: string read FCity write FCity;
|
2020-12-29 13:49:43 +01:00
|
|
|
|
property PostalCode: NullableString read FPostalCode write FPostalCode;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
|
TPhone = class
|
|
|
|
|
private
|
|
|
|
|
FDescription: string;
|
|
|
|
|
FNumber: string;
|
|
|
|
|
public
|
|
|
|
|
property Description: string read FDescription write FDescription;
|
|
|
|
|
property Number: string read FNumber write FNumber;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-11-03 16:16:35 +01:00
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
|
TPhones = class(TObjectList<TPhone>)
|
|
|
|
|
end;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
|
2020-09-29 23:55:19 +02:00
|
|
|
|
TGender = (Male, Female);
|
|
|
|
|
|
2019-07-29 22:49:31 +02:00
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
|
TPerson = class
|
|
|
|
|
private
|
|
|
|
|
FName: string;
|
|
|
|
|
FAge: Integer;
|
|
|
|
|
FCountry: string;
|
2019-08-01 23:07:23 +02:00
|
|
|
|
FCode: Integer;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
FAddress: TAddress;
|
2019-11-03 16:16:35 +01:00
|
|
|
|
FPhones: TPhones;
|
2020-09-29 23:55:19 +02:00
|
|
|
|
FGender: TGender;
|
2021-06-11 14:36:47 +02:00
|
|
|
|
FArrayField: TArray<string>;
|
|
|
|
|
FListField: TList<string>;
|
2021-12-30 21:34:52 +01:00
|
|
|
|
FStringDictionary: TMVCStringDictionary;
|
2019-07-29 22:49:31 +02:00
|
|
|
|
public
|
2019-10-30 16:02:30 +01:00
|
|
|
|
constructor Create;
|
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
2019-08-02 20:54:52 +02:00
|
|
|
|
[MVCSwagJsonSchemaField(stInteger, 'code', 'person id', True, False)]
|
2019-08-01 23:07:23 +02:00
|
|
|
|
property Code: Integer read FCode write FCode;
|
2021-05-12 15:26:55 +02:00
|
|
|
|
[MVCSwagJsonSchemaField('name', 'person name', True, False, 5, 100)]
|
2019-07-29 22:49:31 +02:00
|
|
|
|
property Name: string read FName write FName;
|
2020-09-29 23:55:19 +02:00
|
|
|
|
[MVCSwagJsonSchemaField('gender', 'person gender', True, False)]
|
|
|
|
|
[MVCEnumSerialization(estEnumName)]
|
|
|
|
|
property Gender: TGender read FGender write FGender;
|
2019-08-02 20:54:52 +02:00
|
|
|
|
[MVCSwagJsonSchemaField('age', 'person age', True, False)]
|
2019-07-29 22:49:31 +02:00
|
|
|
|
property Age: Integer read FAge write FAge;
|
2021-05-12 15:26:55 +02:00
|
|
|
|
[MVCSwagJsonSchemaField('country', 'Nationality of the person', True, False, 0, 100)]
|
2019-07-29 22:49:31 +02:00
|
|
|
|
property Country: string read FCountry write FCountry;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
property Address: TAddress read FAddress write FAddress;
|
2019-11-03 16:16:35 +01:00
|
|
|
|
property Phones: TPhones read FPhones write FPhones;
|
2021-06-11 14:36:47 +02:00
|
|
|
|
[MVCSwagJsonSchemaField(stArray, 'arrayfield', 'Array Field', True, False)]
|
|
|
|
|
property ArrayField: TArray<string> read FArrayField write FArrayField;
|
|
|
|
|
[MVCSwagJsonSchemaField(stArray, 'listfield', 'List Field', True, False)]
|
|
|
|
|
property ListField: TList<string> read FListField write FListField;
|
2021-12-30 21:34:52 +01:00
|
|
|
|
// [MVCSwagJsonSchemaField(stObject, 'stringdictionary', 'String Dictionary description')]
|
|
|
|
|
property StringDictionary: TMVCStringDictionary read FStringDictionary write FStringDictionary;
|
2019-07-29 22:49:31 +02:00
|
|
|
|
end;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
2019-11-03 16:16:35 +01:00
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
|
TPeople = class(TObjectList<TPerson>)
|
|
|
|
|
end;
|
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
// [MVCSwagIgnorePath] { Ignore all methods of controller }
|
2019-11-03 16:16:35 +01:00
|
|
|
|
[MVCPath('/people')]
|
2020-07-04 22:45:44 +02:00
|
|
|
|
[MVCSwagAuthentication(atJsonWebToken)]
|
2019-07-27 20:23:48 +02:00
|
|
|
|
TMyController2 = class(TMVCController)
|
|
|
|
|
public
|
2019-10-30 16:02:30 +01:00
|
|
|
|
[MVCPath('')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2019-11-03 16:16:35 +01:00
|
|
|
|
[MVCSwagSummary('People', 'List all persons', 'getPeople')]
|
2020-01-03 22:08:47 +01:00
|
|
|
|
[MVCSwagParam(plQuery, 'per_page', 'Items per page', ptInteger, False, '50')]
|
|
|
|
|
[MVCSwagParam(plQuery, 'enumparam', 'Enum param sample', ptString, False, 'enumvalue1',
|
|
|
|
|
'enumvalue1,enumvalue2,enumvalue3,enumvalue4')]
|
2019-10-30 16:02:30 +01:00
|
|
|
|
[MVCSwagResponses(200, 'Success', TPerson, True)]
|
|
|
|
|
[MVCSwagResponses(500, 'Internal Server Error')]
|
2019-11-03 16:16:35 +01:00
|
|
|
|
procedure GetAllPeople;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
[MVCPath('/($Id)')]
|
2019-09-25 14:54:02 +02:00
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2019-11-03 16:16:35 +01:00
|
|
|
|
[MVCSwagSummary('People', 'List Persons by Id', 'getPersonById')]
|
2019-10-30 16:02:30 +01:00
|
|
|
|
[MVCSwagParam(plPath, 'Id', 'Person id', ptInteger)]
|
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);
|
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
// [MVCSwagIgnorePath] { Ignore this method only }
|
2019-07-27 20:23:48 +02:00
|
|
|
|
[MVCPath('')]
|
2019-09-25 14:54:02 +02:00
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2019-11-03 16:16:35 +01:00
|
|
|
|
[MVCSwagSummary('People', 'Insert Person', 'createPerson')]
|
2019-10-30 16:02:30 +01:00
|
|
|
|
[MVCSwagParam(plBody, 'entity', 'Person object', TPerson)]
|
2019-07-27 20:23:48 +02:00
|
|
|
|
[MVCSwagResponses(201, 'Created')]
|
2019-08-01 23:07:23 +02:00
|
|
|
|
[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
|
|
|
|
|
|
2019-11-03 16:16:35 +01:00
|
|
|
|
uses MVCFramework.Controllers.Register;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
{ TMyController2 }
|
|
|
|
|
|
2019-11-03 16:16:35 +01:00
|
|
|
|
procedure TMyController2.GetAllPeople;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
var
|
|
|
|
|
LPerson: TPerson;
|
|
|
|
|
LPersons: TObjectList<TPerson>;
|
|
|
|
|
begin
|
|
|
|
|
LPersons := TObjectList<TPerson>.Create;
|
|
|
|
|
LPerson := TPerson.Create;
|
|
|
|
|
LPerson.Code := 1;
|
|
|
|
|
LPerson.Name := 'Jo<4A>o Ant<6E>nio Duarte';
|
|
|
|
|
LPerson.Age := 26;
|
|
|
|
|
LPerson.Country := 'Brasil';
|
|
|
|
|
LPersons.Add(LPerson);
|
|
|
|
|
|
|
|
|
|
Render<TPerson>(LPersons);
|
|
|
|
|
end;
|
|
|
|
|
|
2019-08-01 23:07:23 +02:00
|
|
|
|
procedure TMyController2.GetPerson(const Id: Integer);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
var
|
|
|
|
|
LPerson: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
LPerson := TPerson.Create;
|
2019-08-01 23:07:23 +02:00
|
|
|
|
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;
|
2019-08-01 23:07:23 +02:00
|
|
|
|
var
|
|
|
|
|
LPerson: TPerson;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-08-01 23:07:23 +02:00
|
|
|
|
LPerson := Context.Request.BodyAs<TPerson>;
|
|
|
|
|
Render(LPerson);
|
2019-10-30 16:02:30 +01:00
|
|
|
|
ResponseStatus(201, 'Created');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{ TPerson }
|
|
|
|
|
|
|
|
|
|
constructor TPerson.Create;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
FAddress := TAddress.Create;
|
2019-11-03 16:16:35 +01:00
|
|
|
|
FPhones := TPhones.Create;
|
2021-12-30 21:34:52 +01:00
|
|
|
|
FStringDictionary := TMVCStringDictionary.Create;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TPerson.Destroy;
|
|
|
|
|
begin
|
|
|
|
|
FAddress.Free;
|
|
|
|
|
FPhones.Free;
|
2021-12-30 21:34:52 +01:00
|
|
|
|
FStringDictionary.Free;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
inherited;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
|
|
TControllersRegister.Instance.RegisterController(TMyController2, 'MyServerName');
|
|
|
|
|
|
|
|
|
|
end.
|