unit OpenAPI.Classes.Path;

interface

uses
  Generics.Collections, Bcl.Collections,
  OpenAPI.Classes.Operation, OpenAPI.Classes.Parameter,
  OpenAPI.Json.Attributes;

type
  // Describes the operations available on a single path.
  [OAJsonInclusion, OAJsonNaming]
  TPathItem = class
  strict private
    FGet: TOperation;
    FPost: TOperation;
    FPut: TOperation;
    FDelete: TOperation;
    FOptions: TOperation;
    FHead: TOperation;
    FPatch: TOperation;
    FParameters: TList<TParameter>;
    function GetDelete: TOperation;
    function GetGet: TOperation;
    function GetHead: TOperation;
    function GetOptions: TOperation;
    function GetParameters: TList<TParameter>;
    function GetPatch: TOperation;
    function GetPost: TOperation;
    function GetPut: TOperation;
  public
    destructor Destroy; override;
    function GetOperation(const Method: string): TOperation;

    // A definition of a GET operation on this path.
    property Get: TOperation read GetGet;
    // A definition of a POST operation on this path.
    property Post: TOperation read GetPost;
    // A definition of a PUT operation on this path.
    property Put: TOperation read GetPut;
    // 	A definition of a DELETE operation on this path.
    property Delete: TOperation read GetDelete;
    // A definition of a OPTIONS operation on this path.
    property Options: TOperation read GetOptions;
    // A definition of a HEAD operation on this path.
    property Head: TOperation read GetHead;
    // A definition of a PATCH operation on this path.
    property Patch: TOperation read GetPatch;
    // A list of parameters that are applicable for all the operations described under this path.
    property Parameters: TList<TParameter> read GetParameters;
  end;

  // Holds the relative paths to the individual endpoints.
  // The path is appended to the basePath in order to construct the full URL.
  // Key: A relative path to an individual endpoint. The field name MUST begin with a slash.
  TPaths = class(TOrderedObjectDictionary<string, TPathItem>);

implementation

{ TPathItem }

destructor TPathItem.Destroy;
begin
  FHead.Free;
  FPatch.Free;
  FParameters.Free;
  FPost.Free;
  FPut.Free;
  FGet.Free;
  FDelete.Free;
  FOptions.Free;
  inherited;
end;

function TPathItem.GetDelete: TOperation;
begin
  result := TOperation.Return(FDelete);
end;

function TPathItem.GetGet: TOperation;
begin
  result := TOperation.Return(FGet);
end;

function TPathItem.GetHead: TOperation;
begin
  result := TOperation.Return(FHead);
end;

function TPathItem.GetOperation(const Method: string): TOperation;
begin
  if Method = 'POST' then
    Result := Post
  else
  if Method = 'GET' then
    Result := Get
  else
  if Method = 'PUT' then
    Result := Put
  else
  if Method = 'DELETE' then
    Result := Delete
  else
  if Method = 'OPTIONS' then
    Result := Options
  else
  if Method = 'HEAD' then
    Result := Head
  else
  if Method = 'Patch' then
    Result := Patch
  else
    Result := nil;
end;

function TPathItem.GetOptions: TOperation;
begin
  result := TOperation.Return(FOptions);
end;

function TPathItem.GetParameters: TList<TParameter>;
begin
  if FParameters = nil then
    FParameters := TList<TParameter>.Create;
  result := FParameters;
end;

function TPathItem.GetPatch: TOperation;
begin
  result := TOperation.Return(FPatch);
end;

function TPathItem.GetPost: TOperation;
begin
  result := TOperation.Return(FPost);
end;

function TPathItem.GetPut: TOperation;
begin
  result := TOperation.Return(FPut);
end;

end.
