[CmdletBinding()] param( [ValidateSet('Add', 'List', 'Update', 'Remove', 'Random')] [string]$Action = 'List', [string]$Title, [int]$Year = 0, [ValidateSet('film', 'series')] [string]$Type = 'film', [ValidateSet('planned', 'watching', 'watched')] [string]$Status = 'planned', [int]$Rating = 0, [int]$Id, [string]$Data = (Join-Path $env:APPDATA 'Productivity\watchlist.json'), [switch]$Force ) $ErrorActionPreference = 'Stop' function Read-Items { param([string]$Path) if (-not (Test-Path -LiteralPath $Path)) { return @() } $raw = Get-Content -LiteralPath $Path -Raw -Encoding UTF8 if ([string]::IsNullOrWhiteSpace($raw)) { return @() } $items = $raw | ConvertFrom-Json if ($null -eq $items) { return @() } return @($items) } function Save-Items { param([string]$Path, [object[]]$Items) $directory = Split-Path -LiteralPath $Path if ($directory -and -not (Test-Path -LiteralPath $directory)) { New-Item -ItemType Directory -Path $directory -Force | Out-Null } if (Test-Path -LiteralPath $Path) { Copy-Item -LiteralPath $Path -Destination ($Path + '.bak') -Force } $json = ConvertTo-Json -InputObject @($Items) -Depth 5 Set-Content -LiteralPath $Path -Value $json -Encoding UTF8 } $items = Read-Items -Path $Data switch ($Action) { 'Add' { if ([string]::IsNullOrWhiteSpace($Title)) { throw 'Укажите название через -Title.' } if ($Year -lt 0 -or $Year -gt 2100) { throw 'Год должен быть в диапазоне 0..2100.' } if ($Rating -lt 0 -or $Rating -gt 10) { throw 'Оценка должна быть от 0 до 10.' } $nextId = 1 if ($items.Count -gt 0) { $nextId = ([int]($items | Measure-Object -Property id -Maximum).Maximum) + 1 } $entry = [pscustomobject]@{ id = $nextId title = $Title year = $Year type = $Type status = $Status rating = $Rating } $items = @($items) + $entry Save-Items -Path $Data -Items $items Write-Host "Добавлено #${nextId}: $Title" } 'List' { if ($items.Count -eq 0) { Write-Host 'Список пуст.'; break } $items | Sort-Object -Property id | ForEach-Object { $ratingText = if ($_.rating -gt 0) { "$($_.rating)/10" } else { 'без оценки' } Write-Host ("#{0} [{1}] {2} ({3}) - {4}" -f $_.id, $_.status, $_.title, $_.year, $ratingText) } } 'Update' { if ($Id -le 0) { throw 'Укажите номер через -Id.' } if ($Rating -lt 0 -or $Rating -gt 10) { throw 'Оценка должна быть от 0 до 10.' } $found = $false foreach ($item in $items) { if ([int]$item.id -eq $Id) { if ($PSBoundParameters.ContainsKey('Status')) { $item.status = $Status } if ($PSBoundParameters.ContainsKey('Rating') -and $Rating -gt 0) { $item.rating = $Rating } $found = $true } } if (-not $found) { throw "Запись #$Id не найдена." } Save-Items -Path $Data -Items $items Write-Host "Обновлено: #$Id" } 'Remove' { if ($Id -le 0) { throw 'Укажите номер через -Id.' } if (-not $Force) { throw 'Удаление меняет файл. Добавьте -Force для подтверждения.' } $kept = @($items | Where-Object { [int]$_.id -ne $Id }) if ($kept.Count -eq $items.Count) { throw "Запись #$Id не найдена." } Save-Items -Path $Data -Items $kept Write-Host "Удалено: #$Id" } 'Random' { $planned = @($items | Where-Object { $_.status -eq 'planned' }) if ($planned.Count -eq 0) { Write-Host 'Нет запланированных фильмов или сериалов.'; break } $choice = $planned | Get-Random Write-Host ("На вечер: {0} ({1}), {2}" -f $choice.title, $choice.year, $choice.type) } }