Remove dead code from cookiebanner and coredata packages

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-30 11:28:20 +04:00
parent 6482361352
commit e5e1119883
4 changed files with 3 additions and 729 deletions

View File

@@ -25,7 +25,6 @@ import (
"github.com/jackc/pgx/v5/pgconn"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
)
type (
@@ -44,125 +43,6 @@ type (
Cookies []*Cookie
)
func (c *Cookie) CursorKey(field CookieOrderField) page.CursorKey {
switch field {
case CookieOrderFieldCreatedAt:
return page.NewCursorKey(c.ID, c.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", field))
}
func (c *Cookie) AuthorizationAttributes(ctx context.Context, conn pg.Querier) (map[string]string, error) {
q := `SELECT organization_id FROM cookies WHERE id = $1 LIMIT 1;`
var organizationID gid.GID
if err := conn.QueryRow(ctx, q, c.ID).Scan(&organizationID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrResourceNotFound
}
return nil, fmt.Errorf("cannot query cookie authorization attributes: %w", err)
}
return map[string]string{"organization_id": organizationID.String()}, nil
}
func (c *Cookie) LoadByID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND id = @cookie_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_id": cookieID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookies: %w", err)
}
cookie, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Cookie])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect cookie: %w", err)
}
*c = cookie
return nil
}
func (c *Cookies) LoadByCookiePatternID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookiePatternID gid.GID,
cursor *page.Cursor[CookieOrderField],
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND cookie_pattern_id = @cookie_pattern_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_pattern_id": cookiePatternID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookies: %w", err)
}
cookies, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Cookie])
if err != nil {
return fmt.Errorf("cannot collect cookies: %w", err)
}
*c = cookies
return nil
}
func (c *Cookies) CountByCookiePatternID(
ctx context.Context,
conn pg.Querier,
@@ -194,134 +74,6 @@ WHERE
return count, nil
}
func (c *Cookies) LoadByCookieCategoryIDViaPattern(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieCategoryID gid.GID,
cursor *page.Cursor[CookieOrderField],
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND cookie_pattern_id IN (
SELECT id FROM cookie_patterns WHERE cookie_category_id = @cookie_category_id
)
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_category_id": cookieCategoryID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookies: %w", err)
}
cookies, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Cookie])
if err != nil {
return fmt.Errorf("cannot collect cookies: %w", err)
}
*c = cookies
return nil
}
func (c *Cookies) CountByCookieCategoryIDViaPattern(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieCategoryID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
cookies
WHERE
%s
AND cookie_pattern_id IN (
SELECT id FROM cookie_patterns WHERE cookie_category_id = @cookie_category_id
)
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_category_id": cookieCategoryID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func (c *Cookies) LoadAllByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
cookie_banner_id,
cookie_pattern_id,
name,
max_age_seconds,
source,
created_at,
updated_at
FROM
cookies
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
ORDER BY
created_at ASC, id ASC;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookies: %w", err)
}
cookies, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Cookie])
if err != nil {
return fmt.Errorf("cannot collect cookies: %w", err)
}
*c = cookies
return nil
}
func (c *Cookie) Insert(
ctx context.Context,
tx pg.Tx,
@@ -435,69 +187,6 @@ ON CONFLICT (cookie_banner_id, name) DO UPDATE
return result.RowsAffected() > 0, nil
}
func (c *Cookie) Update(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
UPDATE cookies
SET
cookie_pattern_id = @cookie_pattern_id,
max_age_seconds = @max_age_seconds,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": c.ID,
"cookie_pattern_id": c.CookiePatternID,
"max_age_seconds": c.MaxAgeSeconds,
"updated_at": c.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
result, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update cookie: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}
func (c *Cookie) Delete(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
DELETE FROM cookies
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"id": c.ID}
maps.Copy(args, scope.SQLArguments())
_, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete cookie: %w", err)
}
return nil
}
func (c *Cookies) RelinkByCookiePatternID(
ctx context.Context,
tx pg.Tx,

View File

@@ -1,55 +0,0 @@
// Copyright (c) 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 "fmt"
type CookieOrderField string
const (
CookieOrderFieldCreatedAt CookieOrderField = "CREATED_AT"
)
func (p CookieOrderField) Column() string {
switch p {
case CookieOrderFieldCreatedAt:
return "created_at"
}
panic(fmt.Sprintf("unsupported order by: %s", p))
}
func (p CookieOrderField) IsValid() bool {
switch p {
case CookieOrderFieldCreatedAt:
return true
}
return false
}
func (p CookieOrderField) String() string {
return string(p)
}
func (p *CookieOrderField) UnmarshalText(text []byte) error {
*p = CookieOrderField(text)
if !p.IsValid() {
return fmt.Errorf("%s is not a valid CookieOrderField", string(text))
}
return nil
}
func (p CookieOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}