delphimvcframework/samples/swaggerdoc/MyController2U.pas

189 lines
5.4 KiB
ObjectPascal
Raw Normal View History

2019-07-27 20:23:48 +02:00
unit MyController2U;
interface
uses
System.Generics.Collections,
2019-07-27 20:23:48 +02:00
MVCFramework,
MVCFramework.Commons,
MVCFramework.Swagger.Commons,
MVCFramework.Serializer.Commons,
MVCFramework.Middleware.Authentication.RoleBasedAuthHandler,
MVCFramework.Nullables;
2019-07-27 20:23:48 +02:00
type
2019-11-03 16:16:35 +01:00
[MVCNameCase(ncLowerCase)]
TAddress = class(TInterfacedObject)
private
FStreet: string;
FNumber: Integer;
FCity: string;
FPostalCode: NullableString;
public
property Street: string read FStreet write FStreet;
property Number: Integer read FNumber write FNumber;
property City: string read FCity write FCity;
property PostalCode: NullableString read FPostalCode write FPostalCode;
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;
TGender = (Male, Female);
[MVCNameCase(ncLowerCase)]
TPerson = class
private
FName: string;
FAge: Integer;
FCountry: string;
FCode: Integer;
FAddress: TAddress;
2019-11-03 16:16:35 +01:00
FPhones: TPhones;
FGender: TGender;
FArrayField: TArray<string>;
FListField: TList<string>;
FStringDictionary: TMVCStringDictionary;
public
constructor Create;
destructor Destroy; override;
[MVCSwagJsonSchemaField(stInteger, 'code', 'person id', True, False)]
property Code: Integer read FCode write FCode;
[MVCSwagJsonSchemaField('name', 'person name', True, False, 5, 100)]
property Name: string read FName write FName;
[MVCSwagJsonSchemaField('gender', 'person gender', True, False)]
[MVCEnumSerialization(estEnumName)]
property Gender: TGender read FGender write FGender;
[MVCSwagJsonSchemaField('age', 'person age', True, False)]
property Age: Integer read FAge write FAge;
[MVCSwagJsonSchemaField('country', 'Nationality of the person', True, False, 0, 100)]
property Country: string read FCountry write FCountry;
property Address: TAddress read FAddress write FAddress;
2019-11-03 16:16:35 +01:00
property Phones: TPhones read FPhones write FPhones;
[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;
// [MVCSwagJsonSchemaField(stObject, 'stringdictionary', 'String Dictionary description')]
property StringDictionary: TMVCStringDictionary read FStringDictionary write FStringDictionary;
end;
2019-07-27 20:23:48 +02:00
2019-11-03 16:16:35 +01:00
[MVCNameCase(ncLowerCase)]
TPeople = class(TObjectList<TPerson>)
end;
// [MVCSwagIgnorePath] { Ignore all methods of controller }
2019-11-03 16:16:35 +01:00
[MVCPath('/people')]
[MVCSwagAuthentication(atJsonWebToken)]
2019-07-27 20:23:48 +02:00
TMyController2 = class(TMVCController)
public
[MVCPath('')]
[MVCHTTPMethod([httpGET])]
2019-11-03 16:16:35 +01:00
[MVCSwagSummary('People', 'List all persons', 'getPeople')]
[MVCSwagParam(plQuery, 'per_page', 'Items per page', ptInteger, False, '50')]
[MVCSwagParam(plQuery, 'enumparam', 'Enum param sample', ptString, False, 'enumvalue1',
'enumvalue1,enumvalue2,enumvalue3,enumvalue4')]
[MVCSwagResponses(200, 'Success', TPerson, True)]
[MVCSwagResponses(500, 'Internal Server Error')]
2019-11-03 16:16:35 +01:00
procedure GetAllPeople;
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')]
[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);
// [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')]
[MVCSwagParam(plBody, 'entity', 'Person object', TPerson)]
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
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;
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;
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);
ResponseStatus(201, 'Created');
end;
{ TPerson }
constructor TPerson.Create;
begin
inherited;
FAddress := TAddress.Create;
2019-11-03 16:16:35 +01:00
FPhones := TPhones.Create;
FStringDictionary := TMVCStringDictionary.Create;
end;
destructor TPerson.Destroy;
begin
FAddress.Free;
FPhones.Free;
FStringDictionary.Free;
inherited;
2019-07-27 20:23:48 +02:00
end;
initialization
TControllersRegister.Instance.RegisterController(TMyController2, 'MyServerName');
end.