2024-03-24 16:58:08 +01:00
|
|
|
unit MainControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2024-03-25 00:15:50 +01:00
|
|
|
MVCFramework, MVCFramework.Commons, MVCFramework.Serializer.Commons,
|
|
|
|
System.Generics.Collections, Services.InterfacesU,
|
|
|
|
Entities, MVCFramework.Container;
|
2024-03-24 16:58:08 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
[MVCPath('/api')]
|
|
|
|
TMyController = class(TMVCController)
|
|
|
|
private
|
|
|
|
fPeopleService: IPeopleService;
|
|
|
|
public
|
|
|
|
[MVCInject]
|
|
|
|
constructor Create(const PeopleService: IPeopleService); reintroduce;
|
2024-03-27 00:10:48 +01:00
|
|
|
|
2024-03-24 16:58:08 +01:00
|
|
|
[MVCPath('/people')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
function GetPeople: TObjectList<TPerson>;
|
|
|
|
|
2024-03-27 00:10:48 +01:00
|
|
|
[MVCPath('/people2')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
function GetPeople2([MVCInject] OtherPeopleService: IPeopleService): TObjectList<TPerson>;
|
|
|
|
|
2024-03-24 16:58:08 +01:00
|
|
|
[MVCPath('/people/($ID)')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
function GetPerson(ID: Integer): TPerson;
|
|
|
|
|
|
|
|
[MVCPath('/people')]
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2024-04-17 09:57:22 +02:00
|
|
|
function CreatePerson([MVCInject] PeopleService: IPeopleService; [MVCFromBody] Person: TPerson): IMVCResponse;
|
2024-03-24 16:58:08 +01:00
|
|
|
|
|
|
|
[MVCPath('/people/($ID)')]
|
|
|
|
[MVCHTTPMethod([httpPUT])]
|
|
|
|
function UpdatePerson(ID: Integer; [MVCFromBody] Person: TPerson): IMVCResponse;
|
|
|
|
|
|
|
|
[MVCPath('/people/($ID)')]
|
|
|
|
[MVCHTTPMethod([httpDELETE])]
|
|
|
|
function DeletePerson(ID: Integer): IMVCResponse;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils, MVCFramework.Logger, System.StrUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Sample CRUD Actions for a "People" entity
|
|
|
|
function TMyController.GetPeople: TObjectList<TPerson>;
|
|
|
|
begin
|
|
|
|
Result := fPeopleService.GetAll;
|
|
|
|
end;
|
|
|
|
|
2024-03-27 00:10:48 +01:00
|
|
|
function TMyController.GetPeople2(OtherPeopleService: IPeopleService): TObjectList<TPerson>;
|
|
|
|
begin
|
2024-03-28 23:57:59 +01:00
|
|
|
LogI('PeopleService in GetPeople2: ' + IntToHex(NativeUInt(Pointer(OtherPeopleService))));
|
2024-03-27 00:10:48 +01:00
|
|
|
Result := OtherPeopleService.GetAll;
|
|
|
|
end;
|
|
|
|
|
2024-03-24 16:58:08 +01:00
|
|
|
function TMyController.GetPerson(ID: Integer): TPerson;
|
|
|
|
var
|
|
|
|
lPeople: TObjectList<TPerson>;
|
|
|
|
begin
|
|
|
|
lPeople := GetPeople;
|
|
|
|
try
|
|
|
|
Result := lPeople.ExtractAt(ID mod lPeople.Count);
|
|
|
|
finally
|
|
|
|
lPeople.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
//constructor TMyController.Create;
|
|
|
|
//begin
|
|
|
|
// inherited Create;
|
|
|
|
//end;
|
|
|
|
|
|
|
|
constructor TMyController.Create(const PeopleService: IPeopleService);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
Assert(PeopleService <> nil, 'PeopleService not injected');
|
|
|
|
fPeopleService := PeopleService;
|
2024-03-28 23:57:59 +01:00
|
|
|
LogI('PeopleService in constructor: ' + IntToHex(NativeUInt(Pointer(PeopleService))));
|
2024-03-24 16:58:08 +01:00
|
|
|
end;
|
|
|
|
|
2024-04-17 09:57:22 +02:00
|
|
|
function TMyController.CreatePerson(PeopleService: IPeopleService; Person: TPerson): IMVCResponse;
|
2024-03-24 16:58:08 +01:00
|
|
|
begin
|
|
|
|
LogI('Created ' + Person.FirstName + ' ' + Person.LastName);
|
2024-04-16 15:21:18 +02:00
|
|
|
Result := CreatedResponse('', 'Person created (' + Person.ToString + ')' );
|
2024-03-24 16:58:08 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TMyController.UpdatePerson(ID: Integer; [MVCFromBody] Person: TPerson): IMVCResponse;
|
|
|
|
begin
|
|
|
|
LogI('Updated ' + Person.FirstName + ' ' + Person.LastName);
|
2024-04-16 15:21:18 +02:00
|
|
|
Result := NoContentResponse;
|
2024-03-24 16:58:08 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TMyController.DeletePerson(ID: Integer): IMVCResponse;
|
|
|
|
begin
|
|
|
|
LogI('Deleted person with id ' + ID.ToString);
|
2024-04-16 15:21:18 +02:00
|
|
|
Result := NoContentResponse;
|
2024-03-24 16:58:08 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
end.
|