unit OpenAPI.Classes.Operation;

interface

uses
  Classes, Generics.Collections,
  XData.JSchema.Classes,
  OpenAPI.Classes, OpenAPI.Classes.Parameter, OpenAPI.Classes.Security,
  OpenAPI.Json.Attributes, OpenAPI.Types;

type
  // Describes a single API operation on a path.
  [OAJsonInclusion, OAJsonNaming]
  TOperation = class
  strict private
    FTags: TOpenAPIStrings;
    FSummary: string;
    FDescription: string;
    FOperationId: string;
    FExternalDocs: TExternalDoc;
    FConsumes: TMimeTypes;
    FProduces: TMimeTypes;
    FParameters: TList<TParameter>;
    FProtocols: TProtocols;
    FResponses: TResponses;
    FSecurity: TSecurityRequirements;
    FDeprecated: boolean;
    function GetConsumes: TMimeTypes;
    function GetProduces: TMimeTypes;
    function GetResponses: TResponses;
    function GetSecurity: TSecurityRequirements;
    function GetTags: TOpenAPIStrings;
  public
    constructor Create;
    destructor Destroy; override;
    function AddParameter(Name: string; &In: TLocation; ParamSchema: TJsonSchema; Description: string = ''; Required: boolean = false): TParameter;
    function BodyParameter(ParamSchema: TJsonSchema; Description: string = ''): TParameter;
    class function Return(var Op: TOperation): TOperation;
    // A list of tags for API documentation control.
    // Tags can be used for logical grouping of operations by resources or any other qualifier.
    property Tags: TOpenAPIStrings read GetTags;
    // A short summary of what the operation does.
    // For maximum readability in the swagger-ui, this field SHOULD be less than 120 characters.
    property Summary: string read FSummary write FSummary;
    // A verbose explanation of the operation behavior.
    property Description: string read FDescription write FDescription;
    // Additional external documentation for this operation.
    property ExternalDocs: TExternalDoc read FExternalDocs write FExternalDocs;
    // Unique string used to identify the operation. The id MUST be unique among all operations described in the API.
    property OperationId: string read FOperationId write FOperationId;
    // A list of MIME types the operation can consume.
    property Consumes: TMimeTypes read GetConsumes;
    // A list of MIME types the operation can produce.
    property Produces: TMimeTypes read GetProduces;
    // A list of parameters that are applicable for this operation.
    property Parameters: TList<TParameter> read FParameters;
    // Required. The list of possible responses as they are returned from executing this operation.
    property Responses: TResponses read GetResponses;
    // 	The transfer protocol for the operation. Values MUST be from the list: "http", "https", "ws", "wss".
    property Schemes: TProtocols read FProtocols write FProtocols;
    // Declares this operation to be deprecated. Default value is false.
    property Deprecated: boolean read FDeprecated write FDeprecated;
    // A declaration of which security schemes are applied for this operation.
    property Security: TSecurityRequirements read GetSecurity;
  end;

implementation

uses
  OpenAPI.Consts,
  OpenAPI.Utils;

{ TOperation }

function TOperation.AddParameter(Name: string; &In: TLocation; ParamSchema: TJsonSchema; Description: string; Required: boolean): TParameter;
begin
  result := TParameter.Create;
  result.Name := Name;
  result.&In := &In;
  result.Schema := ParamSchema;
  result.Description := Description;
  result.Required := Required;
  Parameters.Add(result);
end;

function TOperation.BodyParameter(ParamSchema: TJsonSchema; Description: string = ''): TParameter;
begin
  result := AddParameter(TOpenAPIConsts.Body, TLocation.Body, ParamSchema, Description, true);
end;

constructor TOperation.Create;
begin
  FParameters := TObjectList<TParameter>.Create;
end;

destructor TOperation.Destroy;
begin
  FTags.Free;
  FParameters.Free;
  FConsumes.Free;
  FProduces.Free;
  FResponses.Free;
  FSecurity.Free;
  inherited;
end;

function TOperation.GetConsumes: TMimeTypes;
begin
  result := TMimeTypes.Return(FConsumes);
end;

function TOperation.GetProduces: TMimeTypes;
begin
  result := TMimeTypes.Return(FProduces);
end;

function TOperation.GetResponses: TResponses;
begin
  if FResponses = nil then
    FResponses := TResponses.Create;
  result := FResponses;
end;

function TOperation.GetSecurity: TSecurityRequirements;
begin
  if FSecurity = nil then
    FSecurity := TSecurityRequirements.Create;
  result := FSecurity;
end;

function TOperation.GetTags: TOpenAPIStrings;
begin
  result := TOpenAPIStrings.Return(FTags);
end;

class function TOperation.Return(var Op: TOperation): TOperation;
begin
  if Op = nil then
    Op := TOperation.Create;
  return := Op;
end;

end.

