tb/main #1
|
@ -7,46 +7,49 @@ import (
|
||||||
|
|
||||||
func (database *Database) FindClaimsByProductID(product_id int) (claims []models.Claim, err error) {
|
func (database *Database) FindClaimsByProductID(product_id int) (claims []models.Claim, err error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
err = database.db.Query(ctx, claims, `SELECT
|
|
||||||
cluster,
|
err = database.db.Query(ctx, claims, `
|
||||||
id,
|
SELECT
|
||||||
worker_type,
|
cluster,
|
||||||
evidence_type,
|
id,
|
||||||
evidence,
|
worker_type,
|
||||||
category,
|
evidence_type,
|
||||||
polarity,
|
evidence,
|
||||||
created_at,
|
category,
|
||||||
created_by
|
polarity,
|
||||||
FROM (
|
created_at,
|
||||||
SELECT
|
created_by
|
||||||
cluster,
|
FROM (
|
||||||
id,
|
SELECT
|
||||||
worker_type,
|
cluster,
|
||||||
evidence_type,
|
id,
|
||||||
evidence,
|
worker_type,
|
||||||
category,
|
evidence_type,
|
||||||
polarity,
|
evidence,
|
||||||
created_at,
|
category,
|
||||||
created_by,
|
polarity,
|
||||||
ROW_NUMBER() OVER (PARTITION BY category ORDER BY created_at DESC) AS rn
|
created_at,
|
||||||
FROM (
|
created_by,
|
||||||
(
|
ROW_NUMBER() OVER (PARTITION BY category ORDER BY created_at DESC) AS rn
|
||||||
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, created_by FROM user_claims
|
FROM (
|
||||||
UNION ALL
|
(
|
||||||
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, null as created_by FROM automated_claims
|
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, created_by FROM user_claims
|
||||||
|
UNION ALL
|
||||||
|
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(claim) as category, true as polarity, created_at, null as created_by FROM automated_claims
|
||||||
|
)
|
||||||
|
UNION ALL
|
||||||
|
(
|
||||||
|
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, created_by FROM user_claims
|
||||||
|
UNION ALL
|
||||||
|
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, null as created_by FROM automated_claims
|
||||||
|
)
|
||||||
|
)
|
||||||
|
WHERE product_id = ?
|
||||||
)
|
)
|
||||||
UNION ALL
|
WHERE rn = 1
|
||||||
(
|
ORDER BY created_at;
|
||||||
SELECT "user" as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, created_by FROM user_claims
|
`, product_id)
|
||||||
UNION ALL
|
|
||||||
SELECT "automated" as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(counter_claim) as category, false as polarity, created_at, null as created_by FROM automated_claims
|
|
||||||
)
|
|
||||||
)
|
|
||||||
WHERE product_id = ?
|
|
||||||
)
|
|
||||||
WHERE rn = 1
|
|
||||||
ORDER BY created_at;
|
|
||||||
`, product_id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return claims, err
|
return claims, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue