redesign structure, add get endpoint
This commit is contained in:
parent
3dacf06005
commit
a216cc14dd
14 changed files with 176 additions and 77 deletions
|
@ -1,8 +1,8 @@
|
|||
package application
|
||||
|
||||
import (
|
||||
"vegan-barcode/internal/application/utils"
|
||||
"vegan-barcode/internal/database"
|
||||
"vegan-barcode/internal/utils"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
|
|
@ -1,11 +1,19 @@
|
|||
package application
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
func (application *Application) GetClaimsHandler(c *gin.Context) {
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (a *Application) GetClaimsHandler(c *gin.Context) {
|
||||
system := c.DefaultQuery("system", "upc")
|
||||
barcode := c.Query("barcode")
|
||||
|
||||
application.GetClaims(system, barcode)
|
||||
// TODO: 404 when claims are not found
|
||||
productClaims, err := a.GetClaims(system, barcode)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, productClaims)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// TODO: Service should get moved somewhere else. Not sure on naming.
|
||||
func (application *Application) bindRoutes() {
|
||||
|
||||
router := gin.Default()
|
||||
|
|
|
@ -1,5 +1,19 @@
|
|||
package application
|
||||
|
||||
func (application *Application) GetClaims(system string, barcode string) {
|
||||
application.db.FindClaimsByBarcode(system, barcode)
|
||||
import (
|
||||
"vegan-barcode/internal/models"
|
||||
)
|
||||
|
||||
func (a *Application) GetClaims(system string, barcode string) (*models.ProductClaims, error) {
|
||||
id, err := a.db.FindProductIDByBarcode(system, barcode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims, err := a.db.FindClaimsByProductID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &models.ProductClaims{Id: id, Claims: claims}, nil
|
||||
}
|
||||
|
|
|
@ -2,9 +2,53 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
"vegan-barcode/internal/models"
|
||||
)
|
||||
|
||||
func (database *Database) FindClaimsByBarcode(system string, barcode string) {
|
||||
func (database *Database) FindClaimsByProductID(product_id int) (claims []models.Claim, err error) {
|
||||
ctx := context.Background()
|
||||
database.db.Exec(ctx, "SELECT * FROM user_claims")
|
||||
err = database.db.Query(ctx, claims, `SELECT
|
||||
cluster,
|
||||
id,
|
||||
worker_type,
|
||||
evidence_type,
|
||||
evidence,
|
||||
category,
|
||||
polarity,
|
||||
created_at,
|
||||
created_by
|
||||
FROM (
|
||||
SELECT
|
||||
cluster,
|
||||
id,
|
||||
worker_type,
|
||||
evidence_type,
|
||||
evidence,
|
||||
category,
|
||||
polarity,
|
||||
created_at,
|
||||
created_by,
|
||||
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
|
||||
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 = ?
|
||||
)
|
||||
WHERE rn = 1
|
||||
ORDER BY created_at;
|
||||
`, product_id)
|
||||
if err != nil {
|
||||
return claims, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ var UserClaimsTable = ksql.NewTable("user_claims", "id")
|
|||
|
||||
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "id")
|
||||
|
||||
// Struct used in dependency injection can be replaced with a mock for testing.
|
||||
type Database struct {
|
||||
db *ksql.DB
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package database
|
|||
|
||||
import (
|
||||
"time"
|
||||
"vegan-barcode/internal/models"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
|
@ -11,54 +12,23 @@ type Product struct {
|
|||
Created_at time.Time `ksql:"created_at,timeNowUTC"`
|
||||
}
|
||||
|
||||
type WorkerType int
|
||||
|
||||
const (
|
||||
Barnivore WorkerType = iota
|
||||
)
|
||||
|
||||
type EvidenceType int
|
||||
|
||||
const (
|
||||
ManufactureWebsite EvidenceType = iota
|
||||
IngredientsList
|
||||
)
|
||||
|
||||
type Claim int
|
||||
|
||||
const (
|
||||
ContainsMeat Claim = iota
|
||||
ContainsFish
|
||||
ContainsEggs
|
||||
ContainsMilk
|
||||
ContainsHoney
|
||||
ContainsWax
|
||||
ContainsFur
|
||||
ContainsLeather
|
||||
ContainsAnimalFibers
|
||||
ContainsWool
|
||||
ContainsFeathers
|
||||
AnimalTesting
|
||||
MonkeySlavery
|
||||
)
|
||||
|
||||
type AutomatedClaim struct {
|
||||
id int `ksql:"id"`
|
||||
product_id int `ksql:"product_id"`
|
||||
worker_type WorkerType `ksql:"worker_type"`
|
||||
evidence struct{} `ksql:"evidence,json"`
|
||||
claim Claim `ksql:"claim"`
|
||||
counter_claim Claim `ksql:"counter_claim"`
|
||||
created_at time.Time `ksql:"created_at,timeNowUTC"`
|
||||
id int `ksql:"id"`
|
||||
product_id int `ksql:"product_id"`
|
||||
worker_type models.WorkerType `ksql:"worker_type"`
|
||||
evidence struct{} `ksql:"evidence,json"`
|
||||
claim models.ClaimType `ksql:"claim"`
|
||||
counter_claim models.ClaimType `ksql:"counter_claim"`
|
||||
created_at time.Time `ksql:"created_at,timeNowUTC"`
|
||||
}
|
||||
|
||||
type UserClaim struct {
|
||||
id int `ksql:"id"`
|
||||
product_id int `ksql:"product_id"`
|
||||
evidence_type EvidenceType `ksql:"evidence_type"`
|
||||
evidence struct{} `ksql:"evidence,json"`
|
||||
claim Claim `ksql:"claim"`
|
||||
counter_claim Claim `ksql:"counter_claim"`
|
||||
created_at time.Time `ksql:"created_at,timeNowUTC"`
|
||||
created_by string `ksql:"created_by"`
|
||||
id int `ksql:"id"`
|
||||
product_id int `ksql:"product_id"`
|
||||
evidence_type models.EvidenceType `ksql:"evidence_type"`
|
||||
evidence struct{} `ksql:"evidence,json"`
|
||||
claim models.ClaimType `ksql:"claim"`
|
||||
counter_claim models.ClaimType `ksql:"counter_claim"`
|
||||
created_at time.Time `ksql:"created_at,timeNowUTC"`
|
||||
created_by string `ksql:"created_by"`
|
||||
}
|
||||
|
|
14
internal/database/products.go
Normal file
14
internal/database/products.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package database
|
||||
|
||||
import "context"
|
||||
|
||||
func (d *Database) FindProductIDByBarcode(system string, barcode string) (id int, err error) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
err = d.db.QueryOne(ctx, &id, "SELECT id FROM products WHERE system = ? AND barcode = ?", system, barcode)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return
|
||||
}
|
61
internal/models/models.go
Normal file
61
internal/models/models.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type ClaimType int
|
||||
|
||||
const (
|
||||
ContainsMeat ClaimType = iota
|
||||
ContainsFish
|
||||
ContainsEggs
|
||||
ContainsMilk
|
||||
ContainsHoney
|
||||
ContainsWax
|
||||
ContainsFur
|
||||
ContainsLeather
|
||||
ContainsAnimalFibers
|
||||
ContainsWool
|
||||
ContainsFeathers
|
||||
AnimalTesting
|
||||
MonkeySlavery
|
||||
)
|
||||
|
||||
type WorkerType int
|
||||
|
||||
const (
|
||||
Barnivore WorkerType = iota
|
||||
)
|
||||
|
||||
type EvidenceType int
|
||||
|
||||
const (
|
||||
ManufactureWebsite EvidenceType = iota
|
||||
IngredientsList
|
||||
)
|
||||
|
||||
type ClusterType int
|
||||
|
||||
const (
|
||||
User ClusterType = iota
|
||||
Automated
|
||||
)
|
||||
|
||||
// Generic claim type for combining both automated and user claims.
|
||||
type Claim struct {
|
||||
Id int
|
||||
Worker_type WorkerType
|
||||
Evidence_type EvidenceType
|
||||
Evidence struct{}
|
||||
Category ClaimType
|
||||
Polarity bool
|
||||
Created_at time.Time
|
||||
Created_by string
|
||||
Cluster ClusterType
|
||||
}
|
||||
|
||||
type ProductClaims struct {
|
||||
Id int
|
||||
Claims []Claim
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue