Add CookieBannerFilter

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-10 12:44:57 +04:00
parent 84628683f2
commit 62c29810a5
2 changed files with 58 additions and 2 deletions

View File

@@ -170,6 +170,7 @@ func (b *CookieBanners) LoadByOrganizationID(
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor[CookieBannerOrderField],
filter *CookieBannerFilter,
) error {
q := `
SELECT
@@ -189,12 +190,14 @@ WHERE
%s
AND organization_id = @organization_id
AND %s
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
@@ -217,6 +220,7 @@ func (b *CookieBanners) CountByOrganizationID(
conn pg.Querier,
scope Scoper,
organizationID gid.GID,
filter *CookieBannerFilter,
) (int, error) {
q := `
SELECT
@@ -226,12 +230,14 @@ FROM
WHERE
%s
AND organization_id = @organization_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment())
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
row := conn.QueryRow(ctx, q, args)

View File

@@ -0,0 +1,50 @@
// Copyright (c) 2025-2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import "github.com/jackc/pgx/v5"
type CookieBannerFilter struct {
state *CookieBannerState
}
func NewCookieBannerFilter(state *CookieBannerState) *CookieBannerFilter {
return &CookieBannerFilter{
state: state,
}
}
func (f *CookieBannerFilter) SQLFragment() string {
return `
(
CASE
WHEN @filter_state::text IS NOT NULL THEN
state = @filter_state::cookie_banner_state
ELSE TRUE
END
)`
}
func (f *CookieBannerFilter) SQLArguments() pgx.StrictNamedArgs {
args := pgx.StrictNamedArgs{
"filter_state": nil,
}
if f.state != nil {
args["filter_state"] = string(*f.state)
}
return args
}