Skip to content

Commit 0c97840

Browse files
committed
Get-BloggerPosts supports -Since date filter
1 parent bc9ad96 commit 0c97840

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

src/public/Get-BloggerPosts.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
88
.PARAMETER Status
99
The status of the posts to retrieve. Valid values are "draft", "live", and "scheduled"
10+
11+
.PARAMETER Since
12+
Filter the results to only return posts that are older than the supplied date.
13+
14+
.PARAMETER All
15+
Flag to indicate if all paginated results should be returned.
1016
#>
1117
Function Get-BloggerPosts {
1218
[CmdletBinding()]
@@ -18,6 +24,9 @@ Function Get-BloggerPosts {
1824
[ValidateSet("draft", "live", "scheduled")]
1925
[string]$Status = "live",
2026

27+
[Parameter(Mandatory = $false)]
28+
[datetime]$Since,
29+
2130
[Parameter(Mandatory = $false)]
2231
[switch]$All
2332
)
@@ -28,22 +37,27 @@ Function Get-BloggerPosts {
2837
throw "BlogId not specified."
2938
}
3039
}
40+
$includeDateFilter = $PSBoundParameters.ContainsKey("Since")
3141

3242
try {
3343
$done = $false
3444
$pageToken = $null
3545
while (!$done) {
36-
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts?status=$status"
46+
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts?status=$status&fetchBodies=false"
47+
3748
if ($pageToken) {
3849
$uri += "&pageToken=$pageToken"
3950
}
51+
if ($includeDateFilter) {
52+
$uri += "&since=" + $Since.ToString("yyyy-MM-ddTHH:mm:ssZ")
53+
}
4054
$result = Invoke-GApi -uri $uri
4155

4256
$result.items
4357

4458
# loop if pageToken is present and -All switch is set
4559
$pageToken = $result.nextPageToken
46-
$done = $All.IsPresent -and $All -and [string]::IsNullOrEmpty($pageToken)
60+
$done = ($All.IsPresent -and $All -and [string]::IsNullOrEmpty($pageToken)) -or !$All.IsPresent
4761
}
4862
}
4963
catch {

src/tests/Get-BloggerPosts.Tests.ps1

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,84 @@ Describe "Get-BloggerPosts" {
4242
# assert
4343
$result.Count | Should -Be 3
4444
}
45+
46+
It "Should only fetch initial set if -All switch is not used" {
47+
# arrange
48+
InModuleScope PSBlogger {
49+
# Setup initial get-bloggerposts
50+
Mock Invoke-GApi {
51+
return @{
52+
items = @(
53+
@{ id = "1"; title = "Post 1"; published = "2023-10-01T00:00:00Z" },
54+
@{ id = "2"; title = "Post 2"; published = "2023-10-02T00:00:00Z" }
55+
)
56+
nextPageToken = $null
57+
}
58+
} -ParameterFilter { $uri -notlike "*pageToken*" }
59+
}
60+
61+
# act
62+
$result = Get-BloggerPosts -All
63+
64+
# assert
65+
$result.Count | Should -Be 2
66+
}
67+
}
68+
69+
Context "Retrieve Posts with Date Filter" {
70+
BeforeEach {
71+
InModuleScope PSBlogger {
72+
73+
$BloggerSession.BlogId = "123456789"
74+
}
75+
}
76+
77+
It "Should filter posts based on Since parameter" {
78+
# arrange
79+
InModuleScope PSBlogger {
80+
81+
# Mock the API call to return posts after a specific date
82+
Mock Invoke-GApi {
83+
return @{
84+
items = @(
85+
@{ id = "1"; title = "Post 1"; published = "2023-10-01T00:00:00Z" },
86+
@{ id = "2"; title = "Post 2"; published = "2023-10-02T00:00:00Z" }
87+
)
88+
nextPageToken = $null
89+
}
90+
} -ParameterFilter { $uri -like "*since=2023-09-30*" }
91+
}
92+
93+
# act
94+
$result = Get-BloggerPosts -Since (Get-Date "2023-09-30")
95+
96+
# assert
97+
$result.Count | Should -Be 2
98+
$result[0].title | Should -Be "Post 1"
99+
$result[1].title | Should -Be "Post 2"
100+
}
101+
102+
It "Should not constrain by date if Since parameter is not provided" {
103+
# arrange
104+
InModuleScope PSBlogger {
105+
106+
# Mock the API call to return all posts
107+
Mock Invoke-GApi {
108+
return @{
109+
items = @(
110+
@{ id = "1"; title = "Post 1"; published = "2023-10-01T00:00:00Z" },
111+
@{ id = "2"; title = "Post 2"; published = "2023-10-02T00:00:00Z" }
112+
)
113+
nextPageToken = $null
114+
}
115+
} -ParameterFilter { $uri -notlike "*since*" }
116+
}
117+
118+
# act
119+
$result = Get-BloggerPosts
120+
121+
# assert
122+
$result.Count | Should -Be 2
123+
}
45124
}
46125
}

0 commit comments

Comments
 (0)