Public/New-CriteriaCollection.ps1
|
<# .SYNOPSIS Creates a new criteria collection in the Fortytwo Collections API. .DESCRIPTION This function creates a new criteria collection in the Fortytwo Collections API. #> function New-CriteriaCollection { [CmdletBinding(SupportsShouldProcess = $true)] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [System.Collections.Hashtable]$Collection, [Parameter(Mandatory = $false)] [switch]$SkipCollectionMemberTest ) process { # Test that the collection has members if (-not $SkipCollectionMemberTest) { try { $CollectionPreview = $Collection | Test-CriteriaCollectionMembers | Select-Object -ExpandProperty Preview } catch { $CollectionPreview = @() } if ($CollectionPreview.Count -eq 0) { Write-Warning "The criteria collection has no members." } else { Write-Host "The criteria collection has $($CollectionPreview.Count) members." } } # TODO: Add validation for the collection object to ensure it meets the required structure and properties. $URI = $Script:APIRoot -f "collections-criteria", "" $Body = $Collection | ConvertTo-Json -Depth 10 Write-Verbose "Creating Criteria Collection with at URI: $URI" $Result = Invoke-RestMethod -Method Post -Uri $URI -Headers (Get-CollectionHeader) -Body $Body -ContentType "application/json" if ($Result.IsSuccess) { Write-Verbose "Successfully created Criteria Collection with Id: $($Result.Data.Id)" } else { if (!$Result.Errors) { if ($StatusCode -eq 403) { throw "Access denied. Please ensure that your access token has the necessary permissions to access Fortytwo Collections." } else { throw "Failed to create Fortytwo Collections objects. StatusCode: $StatusCode" } } throw $($Result.Errors -join '; ') } } } |