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)
|
||||
|
|
|
@ -16,7 +16,7 @@ func TestCreateClaimByProductId(t *testing.T) {
|
|||
Ingredients: "flour,egg,milk,water,salt,butter",
|
||||
},
|
||||
Claims: []models.ClaimType{
|
||||
models.ContainsEggs,
|
||||
models.ContainsEgg,
|
||||
models.ContainsMilk,
|
||||
},
|
||||
Counterclaims: []models.ClaimType{
|
||||
|
|
|
@ -61,10 +61,10 @@ func (database *Database) createTables(ctx context.Context) {
|
|||
CREATE TABLE IF NOT EXISTS user_claims (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
product_id INTEGER,
|
||||
evidence_type INTEGER,
|
||||
evidence_type TEXT,
|
||||
evidence JSONB,
|
||||
claims INTEGER[],
|
||||
counterclaims INTEGER[],
|
||||
claims TEXT[],
|
||||
counterclaims TEXT[],
|
||||
created_at TIMESTAMPTZ,
|
||||
created_by TEXT,
|
||||
|
||||
|
@ -80,10 +80,10 @@ func (database *Database) createTables(ctx context.Context) {
|
|||
CREATE TABLE IF NOT EXISTS automated_claims (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
product_id INTEGER,
|
||||
worker_type INTEGER,
|
||||
worker_type TEXT,
|
||||
evidence JSONB,
|
||||
claims INTEGER[],
|
||||
counterclaims INTEGER[],
|
||||
claims TEXT[],
|
||||
counterclaims TEXT[],
|
||||
created_at TIMESTAMPTZ,
|
||||
|
||||
CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id)
|
||||
|
|
|
@ -2,8 +2,10 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/vingarcia/ksql"
|
||||
)
|
||||
|
||||
|
@ -14,6 +16,8 @@ func (d *Database) FindProductByBarcode(system string, barcode string) (*Product
|
|||
|
||||
var product Product
|
||||
err := d.db.QueryOne(ctx, &product, "FROM products WHERE system = $1 AND barcode = $2", system, barcode)
|
||||
|
||||
// No error because product may just not exist.
|
||||
if err == ksql.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -23,6 +27,7 @@ func (d *Database) FindProductByBarcode(system string, barcode string) (*Product
|
|||
return &product, nil
|
||||
}
|
||||
|
||||
// Doesnt handle checking if product exists.
|
||||
func (d *Database) CreateProduct(system string, barcode string) (*Product, error) {
|
||||
ctx := context.Background()
|
||||
var product = Product{
|
||||
|
@ -31,8 +36,10 @@ func (d *Database) CreateProduct(system string, barcode string) (*Product, error
|
|||
Created_at: time.Now(),
|
||||
}
|
||||
|
||||
log.Debugf("made new product: %v", product)
|
||||
|
||||
if err := d.db.Insert(ctx, ProductsTable, &product); err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("failed to insert new product: %w", err)
|
||||
}
|
||||
|
||||
return &product, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue