Public/Get-DuneCollection.ps1

<#
.SYNOPSIS
Retrieve Dune collections.

.DESCRIPTION
Gets one or more Dune collections. Supports filtering by name, id, deployment, and lost-and-found flag. Returns `DuneCollection` objects by default; use `-Raw` for raw API results.

.PARAMETER Name
Filter collections by name (supports wildcards). Position 0 in the default parameter set.

.PARAMETER Id
The GUID of a collection. Use the `Id` parameter set to retrieve a specific collection.

.PARAMETER Deployment
A `DuneDeployment` object; returns collections associated with the supplied deployment (pipeline input supported).

.PARAMETER IsLostAndFound
Switch to return the lost-and-found collection.

.PARAMETER Raw
If set, returns raw API objects instead of `DuneCollection` objects.

.PARAMETER IncludeDeleted
Include deleted collections in results.

.EXAMPLE
PS> Get-DuneCollection
Returns all collections.

.EXAMPLE
PS> Get-DuneCollection -Id 3d8f6b5a-...
Returns the collection with the given `Id`.

.EXAMPLE
PS> Get-DuneDeployment -Name "App" | Get-DuneCollection
Pipeline example using the `Deployment` parameter set.
#>

function Get-DuneCollection {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param (
        [Parameter(Position = 0)]
        [string]$Name,

        [Parameter(ParameterSetName = "Id")]
        [guid]$Id,

        [Parameter(ParameterSetName = "Deployment", ValueFromPipeline)]
        [DuneDeployment]$Deployment,

        [Parameter(ParameterSetName = "IsLostAndFound")]
        [switch]$IsLostAndFound,

        [Parameter()]
        [switch]$Raw,

        [Parameter()]
        [switch]$IncludeDeleted
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $ReturnObjects = @()
        $ProcessedUrls = @()
        $BaseUri = 'collections'
        $Method = "GET"
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"

        # Build Uri
        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { '{0}/{1}' -f $BaseUri, $Id }
            'Deployment' { '{0}/{1}' -f $BaseUri, $Deployment.ParentId }
            'IsLostAndFound' { '{0}?IsLostAndFound=1' -f $BaseUri }
            Default { '{0}' -f $BaseUri }
        }
        if ($Name) {
            $Uri = $Uri | Add-UriQueryParam "NameILike=$Name" -ConvertWildcards
        }
        if ($IncludeDeleted) {
            $Uri = $Uri | Add-UriQueryParam "IncludeDeleted=1"
        }

        # ApiCall Cache
        if ($ProcessedUrls -notcontains $Uri) {
            try {
                # ApiCall and Object conversion
                $ResultItems = Invoke-DuneApiRequest -Uri $Uri -Method $Method -ExtractItems
                $ProcessedUrls += $Uri
                $ReturnObjects += $ResultItems | ForEach-Object {
                    if ($Raw) {
                        $_
                    }
                    else {
                        ConvertTo-DuneClassObject -Class DuneCollection -InputObject $_
                    }
                }
            }
            catch {
                throw $_
            }
        }
        else {
            Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: DuneApiRequest for $Uri already invoked"
        }
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
        return $ReturnObjects | Sort-Object -Unique Id | Sort-Object Name
    }
}