2018-09-25 15:36:53 +02:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2022-01-04 15:44:47 +01:00
|
|
|
// Copyright (c) 2010-2022 Daniele Teti and the DMVCFramework Team
|
2018-09-25 15:36:53 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
|
|
|
|
unit MVCFramework.ActiveRecordController;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.ActiveRecord,
|
|
|
|
FireDAC.Stan.Def,
|
|
|
|
FireDAC.Stan.Pool,
|
|
|
|
FireDAC.Stan.Async,
|
2018-09-28 13:01:46 +02:00
|
|
|
FireDAC.Comp.Client,
|
2018-10-23 16:18:34 +02:00
|
|
|
MVCFramework.RQL.Parser,
|
|
|
|
System.Generics.Collections,
|
|
|
|
MVCFramework.Serializer.Commons;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
{$SCOPEDENUMS ON}
|
|
|
|
TMVCActiveRecordAction = (Create, Retrieve, Update, Delete);
|
|
|
|
TMVCActiveRecordAuthFunc = TFunc<TWebContext, TMVCActiveRecordClass, TMVCActiveRecordAction, Boolean>;
|
|
|
|
|
|
|
|
TMVCActiveRecordController = class(TMVCController)
|
|
|
|
private
|
|
|
|
fAuthorization: TMVCActiveRecordAuthFunc;
|
2021-09-09 23:06:49 +02:00
|
|
|
fURLSegment: string;
|
2018-09-25 15:36:53 +02:00
|
|
|
protected
|
2018-10-23 16:18:34 +02:00
|
|
|
function GetMaxRecordCount: Integer;
|
2018-09-25 15:36:53 +02:00
|
|
|
function CheckAuthorization(aClass: TMVCActiveRecordClass; aAction: TMVCActiveRecordAction): Boolean; virtual;
|
|
|
|
public
|
2020-06-18 14:49:06 +02:00
|
|
|
constructor Create(const aConnectionFactory: TFunc<TFDConnection>;
|
2021-09-09 23:06:49 +02:00
|
|
|
const aAuthorization: TMVCActiveRecordAuthFunc = nil;
|
2022-08-01 19:11:42 +02:00
|
|
|
const aURLSegment: String = ''); reintroduce; overload;
|
|
|
|
constructor Create(const aConnectionDefName: String;
|
|
|
|
const aAuthorization: TMVCActiveRecordAuthFunc = nil;
|
|
|
|
const aURLSegment: String = ''); reintroduce; overload;
|
2018-09-25 15:36:53 +02:00
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
|
|
[MVCPath('/($entityname)')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure GetEntities(const entityname: string); virtual;
|
|
|
|
|
2018-09-27 12:26:50 +02:00
|
|
|
[MVCPath('/($entityname)/searches')]
|
2018-09-28 13:01:46 +02:00
|
|
|
[MVCHTTPMethod([httpGET, httpPOST])]
|
2018-09-27 12:26:50 +02:00
|
|
|
procedure GetEntitiesByRQL(const entityname: string); virtual;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
[MVCPath('/($entityname)/($id)')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
procedure GetEntity(const entityname: string; const id: Integer); virtual;
|
|
|
|
|
|
|
|
[MVCPath('/($entityname)')]
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
|
|
|
procedure CreateEntity(const entityname: string); virtual;
|
|
|
|
|
|
|
|
[MVCPath('/($entityname)/($id)')]
|
|
|
|
[MVCHTTPMethod([httpPUT])]
|
|
|
|
procedure UpdateEntity(const entityname: string; const id: Integer); virtual;
|
|
|
|
|
|
|
|
[MVCPath('/($entityname)/($id)')]
|
|
|
|
[MVCHTTPMethod([httpDELETE])]
|
|
|
|
procedure DeleteEntity(const entityname: string; const id: Integer); virtual;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
TMVCActiveRecordListResponse = class
|
|
|
|
private
|
|
|
|
FList: TMVCActiveRecordList;
|
|
|
|
FMetadata: TMVCStringDictionary;
|
|
|
|
FOwns: Boolean;
|
|
|
|
public
|
|
|
|
constructor Create(AList: TMVCActiveRecordList; AOwns: Boolean = True); virtual;
|
|
|
|
destructor Destroy; override;
|
|
|
|
[MVCListOf(TMVCActiveRecord)]
|
2020-06-18 14:49:06 +02:00
|
|
|
[MVCNameAs('data')]
|
2018-10-23 16:18:34 +02:00
|
|
|
property Items: TMVCActiveRecordList read FList;
|
|
|
|
[MVCNameAs('meta')]
|
|
|
|
property Metadata: TMVCStringDictionary read FMetadata;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2018-09-28 13:01:46 +02:00
|
|
|
|
2018-09-27 12:26:50 +02:00
|
|
|
MVCFramework.Logger,
|
2021-09-09 23:06:49 +02:00
|
|
|
JsonDataObjects, Data.DB;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
procedure TMVCActiveRecordController.GetEntities(const entityname: string);
|
|
|
|
var
|
|
|
|
lARClassRef: TMVCActiveRecordClass;
|
2018-09-28 13:01:46 +02:00
|
|
|
lRQL: string;
|
|
|
|
lInstance: TMVCActiveRecord;
|
|
|
|
lMapping: TMVCFieldsMapping;
|
|
|
|
lConnection: TFDConnection;
|
2018-12-09 23:03:06 +01:00
|
|
|
lRQLBackend: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
lProcessor: IMVCEntityProcessor;
|
|
|
|
lHandled: Boolean;
|
2021-09-09 23:06:49 +02:00
|
|
|
lARResp: TMVCActiveRecordList;
|
|
|
|
lStrDict : TMVCStringDictionary;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
lProcessor := nil;
|
|
|
|
if ActiveRecordMappingRegistry.FindProcessorByURLSegment(entityname, lProcessor) then
|
|
|
|
begin
|
|
|
|
lHandled := False;
|
|
|
|
lProcessor.GetEntities(Context, self, entityname, lHandled);
|
|
|
|
if lHandled then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if not ActiveRecordMappingRegistry.FindEntityClassByURLSegment(entityname, lARClassRef) then
|
|
|
|
begin
|
2019-02-21 20:17:11 +01:00
|
|
|
raise EMVCException.CreateFmt('Cannot find entity nor processor for entity "%s"', [entityname]);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
if not CheckAuthorization(lARClassRef, TMVCActiveRecordAction.Retrieve) then
|
|
|
|
begin
|
|
|
|
Render(TMVCErrorResponse.Create(http_status.Forbidden, 'Cannot read ' + entityname, ''));
|
|
|
|
Exit;
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
2018-09-28 13:01:46 +02:00
|
|
|
lRQL := Context.Request.QueryStringParam('rql');
|
|
|
|
try
|
2018-10-23 16:18:34 +02:00
|
|
|
// if lRQL.IsEmpty then
|
|
|
|
// begin
|
|
|
|
// lRQL := Format('limit(0,%d)', [GetMaxRecordCount]);
|
|
|
|
// end;
|
2018-09-28 13:01:46 +02:00
|
|
|
lConnection := ActiveRecordConnectionsRegistry.GetCurrent;
|
|
|
|
lRQLBackend := GetBackEndByConnection(lConnection);
|
|
|
|
LogD('[RQL PARSE]: ' + lRQL);
|
|
|
|
lInstance := lARClassRef.Create(True);
|
|
|
|
try
|
|
|
|
lMapping := lInstance.GetMapping;
|
|
|
|
finally
|
|
|
|
lInstance.Free;
|
|
|
|
end;
|
2018-10-23 16:18:34 +02:00
|
|
|
|
2020-06-19 16:01:07 +02:00
|
|
|
|
2021-09-09 23:06:49 +02:00
|
|
|
lARResp := TMVCActiveRecord.SelectRQL(lARClassRef, lRQL, GetMaxRecordCount);
|
2018-10-23 16:18:34 +02:00
|
|
|
try
|
2021-09-09 23:06:49 +02:00
|
|
|
lStrDict := StrDict(['page_size'],[lARResp.Count.ToString]);
|
|
|
|
try
|
|
|
|
if Context.Request.QueryStringParam('count').ToLower = 'true' then
|
|
|
|
begin
|
|
|
|
lStrDict.Add('count', TMVCActiveRecord.Count(lARClassRef, lRQL).ToString);
|
|
|
|
end;
|
|
|
|
Render(ObjectDict(False)
|
|
|
|
.Add('data', lARResp,
|
|
|
|
procedure(const AObject: TObject; const Links: IMVCLinks)
|
|
|
|
begin
|
|
|
|
//Links.AddRefLink.Add(HATEOAS.HREF, fURLSegment + '/' + )
|
|
|
|
case TMVCActiveRecord(AObject).GetPrimaryKeyFieldType of
|
|
|
|
ftInteger:
|
|
|
|
Links.AddRefLink.Add(HATEOAS.HREF, fURLSegment + '/' + TMVCActiveRecord(AObject).GetPK.AsInt64.ToString)
|
|
|
|
end;
|
|
|
|
end)
|
|
|
|
.Add('meta', lStrDict));
|
|
|
|
finally
|
|
|
|
lStrDict.Free;
|
2020-06-19 16:01:07 +02:00
|
|
|
end;
|
2021-09-09 23:06:49 +02:00
|
|
|
finally
|
|
|
|
lARResp.Free;
|
2018-10-23 16:18:34 +02:00
|
|
|
end;
|
|
|
|
|
2021-09-09 23:06:49 +02:00
|
|
|
// lResp := TMVCActiveRecordListResponse.Create(TMVCActiveRecord.SelectRQL(lARClassRef, lRQL,
|
|
|
|
// GetMaxRecordCount), True);
|
|
|
|
// try
|
|
|
|
// lResp.Metadata.Add('page_size', lResp.Items.Count.ToString);
|
|
|
|
// if Context.Request.QueryStringParam('count').ToLower = 'true' then
|
|
|
|
// begin
|
|
|
|
// lResp.Metadata.Add('count', TMVCActiveRecord.Count(lARClassRef, lRQL).ToString);
|
|
|
|
// end;
|
|
|
|
// Render(lResp);
|
|
|
|
// except
|
|
|
|
// lResp.Free;
|
|
|
|
// raise;
|
|
|
|
// end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
// Render<TMVCActiveRecord>(TMVCActiveRecord.SelectRQL(lARClassRef, lRQL, lMapping, lRQLBackend), True);
|
2018-09-28 13:01:46 +02:00
|
|
|
except
|
|
|
|
on E: ERQLCompilerNotFound do
|
|
|
|
begin
|
|
|
|
LogE('RQL Compiler not found. Did you included MVCFramework.RQL.AST2<yourdatabase>.pas?');
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-27 12:26:50 +02:00
|
|
|
procedure TMVCActiveRecordController.GetEntitiesByRQL(const entityname: string);
|
|
|
|
var
|
|
|
|
lRQL: string;
|
|
|
|
lJSON: TJsonObject;
|
|
|
|
begin
|
2018-09-28 13:01:46 +02:00
|
|
|
if Context.Request.HTTPMethod = httpPOST then
|
2018-09-27 12:26:50 +02:00
|
|
|
begin
|
2018-09-28 13:01:46 +02:00
|
|
|
lJSON := TJsonObject.Parse(Context.Request.Body) as TJsonObject;
|
|
|
|
try
|
2020-06-18 14:49:06 +02:00
|
|
|
if Assigned(lJSON) then
|
|
|
|
begin
|
|
|
|
lRQL := lJSON.s['rql'];
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lRQL := '';
|
|
|
|
end;
|
2018-09-28 13:01:46 +02:00
|
|
|
finally
|
|
|
|
lJSON.Free;
|
|
|
|
end;
|
|
|
|
Context.Request.QueryStringParams.Values['rql'] := lRQL;
|
2018-09-27 12:26:50 +02:00
|
|
|
end;
|
2018-09-28 13:01:46 +02:00
|
|
|
GetEntities(entityname);
|
2018-09-27 12:26:50 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecordController.GetEntity(const entityname: string; const id: Integer);
|
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
2018-10-14 18:23:20 +02:00
|
|
|
lARClass: TMVCActiveRecordClass;
|
2018-10-23 16:18:34 +02:00
|
|
|
lProcessor: IMVCEntityProcessor;
|
|
|
|
lHandled: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
lProcessor := nil;
|
|
|
|
if ActiveRecordMappingRegistry.FindProcessorByURLSegment(entityname, lProcessor) then
|
|
|
|
begin
|
|
|
|
lHandled := False;
|
|
|
|
lProcessor.GetEntity(Context, self, entityname, id, lHandled);
|
|
|
|
if lHandled then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
if not ActiveRecordMappingRegistry.FindEntityClassByURLSegment(entityname, lARClass) then
|
|
|
|
begin
|
2020-06-18 14:49:06 +02:00
|
|
|
raise EMVCException.CreateFmt(http_status.NotFound, 'Cannot find entity %s', [entityname]);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
|
|
|
lAR := lARClass.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
|
|
|
if not CheckAuthorization(TMVCActiveRecordClass(lAR.ClassType), TMVCActiveRecordAction.Retrieve) then
|
|
|
|
begin
|
|
|
|
Render(TMVCErrorResponse.Create(http_status.Forbidden, 'Cannot read ' + entityname, ''));
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if lAR.LoadByPK(id) then
|
|
|
|
begin
|
2020-06-18 15:54:05 +02:00
|
|
|
Render(ObjectDict(false).Add('data', lAR));
|
2018-09-25 15:36:53 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Render(TMVCErrorResponse.Create(http_status.NotFound, 'Not found', entityname.ToLower + ' not found'));
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
function TMVCActiveRecordController.GetMaxRecordCount: Integer;
|
|
|
|
begin
|
|
|
|
Result := StrToIntDef(Config[TMVCConfigKey.MaxEntitiesRecordCount], 20);
|
|
|
|
end;
|
|
|
|
|
2020-06-18 14:49:06 +02:00
|
|
|
function TMVCActiveRecordController.CheckAuthorization(aClass: TMVCActiveRecordClass;
|
|
|
|
aAction: TMVCActiveRecordAction): Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
if Assigned(fAuthorization) then
|
|
|
|
begin
|
|
|
|
Result := fAuthorization(Context, aClass, aAction);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TMVCActiveRecordController.Create(const aConnectionFactory: TFunc<TFDConnection>;
|
2021-09-09 23:06:49 +02:00
|
|
|
const aAuthorization: TMVCActiveRecordAuthFunc;
|
|
|
|
const aURLSegment: String);
|
2018-10-14 18:23:20 +02:00
|
|
|
var
|
|
|
|
lConn: TFDConnection;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2021-09-09 23:06:49 +02:00
|
|
|
fURLSegment := aURLSegment;
|
2018-10-14 18:23:20 +02:00
|
|
|
try
|
|
|
|
lConn := aConnectionFactory();
|
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
LogE(Format('Connection factory error [ClassName: %s]: "%s"', [E.ClassName, E.Message]));
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
2019-02-21 20:17:11 +01:00
|
|
|
ActiveRecordConnectionsRegistry.AddConnection('default', lConn, True);
|
2018-09-25 15:36:53 +02:00
|
|
|
fAuthorization := aAuthorization;
|
|
|
|
end;
|
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
constructor TMVCActiveRecordController.Create(const aConnectionDefName: String;
|
|
|
|
const aAuthorization: TMVCActiveRecordAuthFunc; const aURLSegment: String);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fURLSegment := aURLSegment;
|
|
|
|
ActiveRecordConnectionsRegistry.AddDefaultConnection(aConnectionDefName);
|
|
|
|
fAuthorization := aAuthorization;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecordController.CreateEntity(const entityname: string);
|
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
2018-10-14 18:23:20 +02:00
|
|
|
lARClass: TMVCActiveRecordClass;
|
|
|
|
lProcessor: IMVCEntityProcessor;
|
|
|
|
lHandled: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
lProcessor := nil;
|
|
|
|
if ActiveRecordMappingRegistry.FindProcessorByURLSegment(entityname, lProcessor) then
|
|
|
|
begin
|
|
|
|
lHandled := False;
|
|
|
|
lProcessor.CreateEntity(Context, self, entityname, lHandled);
|
|
|
|
if lHandled then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if not ActiveRecordMappingRegistry.FindEntityClassByURLSegment(entityname, lARClass) then
|
|
|
|
begin
|
2020-06-18 14:49:06 +02:00
|
|
|
raise EMVCException.CreateFmt(http_status.NotFound, 'Cannot find entity %s', [entityname]);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
|
|
|
lAR := lARClass.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
|
|
|
if not CheckAuthorization(TMVCActiveRecordClass(lAR.ClassType), TMVCActiveRecordAction.Create) then
|
|
|
|
begin
|
|
|
|
Render(TMVCErrorResponse.Create(http_status.Forbidden, 'Cannot create ' + entityname, ''));
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Context.Request.BodyFor<TMVCActiveRecord>(lAR);
|
|
|
|
lAR.Insert;
|
2020-06-18 14:49:06 +02:00
|
|
|
// StatusCode := http_status.Created;
|
|
|
|
Context.Response.CustomHeaders.Values['X-REF'] := Context.Request.PathInfo + '/' + lAR.GetPK.AsInt64.ToString;
|
2018-09-25 15:36:53 +02:00
|
|
|
if Context.Request.QueryStringParam('refresh').ToLower = 'true' then
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
Render(http_status.Created, entityname.ToLower + ' created', '', lAR);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Render(http_status.Created, entityname.ToLower + ' created');
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecordController.UpdateEntity(const entityname: string; const id: Integer);
|
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
2018-10-14 18:23:20 +02:00
|
|
|
lARClass: TMVCActiveRecordClass;
|
2019-03-18 14:08:34 +01:00
|
|
|
lProcessor: IMVCEntityProcessor;
|
|
|
|
lHandled: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-03-18 14:08:34 +01:00
|
|
|
lProcessor := nil;
|
|
|
|
if ActiveRecordMappingRegistry.FindProcessorByURLSegment(entityname, lProcessor) then
|
|
|
|
begin
|
|
|
|
lHandled := False;
|
2020-06-18 14:49:06 +02:00
|
|
|
lProcessor.UpdateEntity(Context, self, entityname, id, lHandled);
|
2019-03-18 14:08:34 +01:00
|
|
|
if lHandled then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
2020-06-18 14:49:06 +02:00
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
if not ActiveRecordMappingRegistry.FindEntityClassByURLSegment(entityname, lARClass) then
|
|
|
|
begin
|
2020-06-18 14:49:06 +02:00
|
|
|
raise EMVCException.CreateFmt(http_status.NotFound, 'Cannot find class for entity %s', [entityname]);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
|
|
|
lAR := lARClass.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
|
|
|
if not CheckAuthorization(TMVCActiveRecordClass(lAR.ClassType), TMVCActiveRecordAction.Update) then
|
|
|
|
begin
|
|
|
|
Render(TMVCErrorResponse.Create(http_status.Forbidden, 'Cannot update ' + entityname, ''));
|
|
|
|
Exit;
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
lAR.CheckAction(TMVCEntityAction.eaUpdate);
|
2018-09-25 15:36:53 +02:00
|
|
|
if not lAR.LoadByPK(id) then
|
2020-06-18 14:49:06 +02:00
|
|
|
raise EMVCException.CreateFmt(http_status.NotFound, 'Cannot find entity %s', [entityname]);
|
2018-09-25 15:36:53 +02:00
|
|
|
Context.Request.BodyFor<TMVCActiveRecord>(lAR);
|
|
|
|
lAR.SetPK(id);
|
|
|
|
lAR.Update;
|
2020-06-18 14:49:06 +02:00
|
|
|
Context.Response.CustomHeaders.Values['X-REF'] := Context.Request.PathInfo;
|
2018-09-25 15:36:53 +02:00
|
|
|
if Context.Request.QueryStringParam('refresh').ToLower = 'true' then
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
Render(http_status.OK, entityname.ToLower + ' updated', '', lAR);
|
2018-09-25 15:36:53 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Render(http_status.OK, entityname.ToLower + ' updated');
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecordController.DeleteEntity(const entityname: string; const id: Integer);
|
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
2018-10-14 18:23:20 +02:00
|
|
|
lARClass: TMVCActiveRecordClass;
|
2021-02-04 14:11:33 +01:00
|
|
|
lProcessor: IMVCEntityProcessor;
|
|
|
|
lHandled: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2021-02-04 14:11:33 +01:00
|
|
|
lProcessor := nil;
|
|
|
|
if ActiveRecordMappingRegistry.FindProcessorByURLSegment(entityname, lProcessor) then
|
|
|
|
begin
|
|
|
|
lHandled := False;
|
|
|
|
lProcessor.DeleteEntity(Context, self, entityname, id, lHandled);
|
|
|
|
if lHandled then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
if not ActiveRecordMappingRegistry.FindEntityClassByURLSegment(entityname, lARClass) then
|
|
|
|
begin
|
2020-06-18 14:49:06 +02:00
|
|
|
raise EMVCException.CreateFmt(http_status.NotFound, 'Cannot find class for entity %s', [entityname]);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
|
|
|
lAR := lARClass.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
2020-06-18 14:49:06 +02:00
|
|
|
if not CheckAuthorization(TMVCActiveRecordClass(lAR.ClassType) { TMVCActiveRecordClass(lAR) } ,
|
|
|
|
TMVCActiveRecordAction.Delete) then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
Render(TMVCErrorResponse.Create(http_status.Forbidden, 'Cannot delete ' + entityname, ''));
|
|
|
|
Exit;
|
|
|
|
end;
|
2020-06-18 14:49:06 +02:00
|
|
|
{
|
|
|
|
HTTP DELETE is an idempotent operation. Invoking it multiple times consecutively must result in
|
|
|
|
the same behavior as the first. Meaning: you shouldn't return HTTP 404.
|
|
|
|
}
|
|
|
|
if lAR.LoadByPK(id) then
|
|
|
|
begin
|
|
|
|
lAR.SetPK(id);
|
|
|
|
lAR.Delete;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
Render(http_status.OK, entityname.ToLower + ' deleted');
|
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCActiveRecordController.Destroy;
|
|
|
|
begin
|
|
|
|
ActiveRecordConnectionsRegistry.RemoveConnection('default');
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
{ TObjectListSetHolder }
|
|
|
|
|
|
|
|
constructor TMVCActiveRecordListResponse.Create(AList: TMVCActiveRecordList; AOwns: Boolean = True);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FOwns := AOwns;
|
|
|
|
FList := AList;
|
|
|
|
FMetadata := TMVCStringDictionary.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCActiveRecordListResponse.Destroy;
|
|
|
|
begin
|
|
|
|
if FOwns then
|
|
|
|
begin
|
|
|
|
FList.Free
|
|
|
|
end;
|
|
|
|
FMetadata.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
end.
|