make get and post endpoints work, update enums to be strings, add log file output
This commit is contained in:
parent
79977f1a18
commit
e762fc2abd
12 changed files with 94 additions and 72 deletions
|
@ -2,6 +2,7 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"vegan-barcode/internal/models"
|
||||
|
||||
"github.com/vingarcia/ksql"
|
||||
|
@ -10,7 +11,7 @@ import (
|
|||
func (d *Database) FindClaimsByProductID(product_id int) (claims []models.Claim, err error) {
|
||||
ctx := context.Background()
|
||||
|
||||
err = d.db.Query(ctx, claims, `
|
||||
err = d.db.Query(ctx, &claims, `
|
||||
SELECT
|
||||
cluster,
|
||||
id,
|
||||
|
@ -35,29 +36,30 @@ func (d *Database) FindClaimsByProductID(product_id int) (claims []models.Claim,
|
|||
ROW_NUMBER() OVER (PARTITION BY category ORDER BY created_at DESC) AS rn
|
||||
FROM (
|
||||
(
|
||||
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
|
||||
SELECT 'user' as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(claims) 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
|
||||
SELECT 'automated' as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(claims) 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
|
||||
SELECT 'user' as cluster, id, product_id, null as worker_type, evidence_type, evidence, unnest(counterclaims) 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
|
||||
SELECT 'automated' as cluster, id, product_id, worker_type, null as evidence_type, evidence, unnest(counterclaims) as category, false as polarity, created_at, null as created_by FROM automated_claims
|
||||
)
|
||||
)
|
||||
) AS combined_claims
|
||||
WHERE product_id = $1
|
||||
)
|
||||
) AS ranked_claims
|
||||
WHERE rn = 1
|
||||
ORDER BY created_at;
|
||||
`, product_id)
|
||||
|
||||
if err != nil {
|
||||
return claims, err
|
||||
return nil, fmt.Errorf("query failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// exists only for testing
|
||||
func (database *Database) FindUserClaimById(claim_id int) (*UserClaim, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
|
@ -79,6 +81,7 @@ func (d *Database) CreateUserClaim(product_id int, form models.UserClaimForm) (*
|
|||
Evidence: form.Evidence,
|
||||
Claims: form.Claims,
|
||||
Counterclaims: form.Counterclaims,
|
||||
// TODO: Add created by
|
||||
}
|
||||
|
||||
err := d.db.Insert(ctx, UserClaimsTable, &uc)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue