Rearrange folders, and separate functionality
This commit is contained in:
parent
dc58253ad9
commit
d26e7b8345
5 changed files with 76 additions and 59 deletions
87
internal/database/database.go
Normal file
87
internal/database/database.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/vingarcia/ksql"
|
||||
"github.com/vingarcia/ksql/adapters/kpgx"
|
||||
)
|
||||
|
||||
var ProductsTable = ksql.NewTable("products", "product_id")
|
||||
|
||||
var UserClaimsTable = ksql.NewTable("user_claims", "user_claim_id")
|
||||
|
||||
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "automated_claim_id")
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
// urlExample := "postgres://username:password@localhost:5432/database_name"
|
||||
host := os.Getenv("PGHOST")
|
||||
database := os.Getenv("DB_NAME")
|
||||
|
||||
connectString :=
|
||||
fmt.Sprintf("postgres:///?host=%s&database=%s", host, database)
|
||||
|
||||
db, err := kpgx.New(ctx, connectString, ksql.Config{})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
}
|
||||
|
||||
func createTables(db) {
|
||||
_, err = db.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id INTEGER PRIMARY KEY,
|
||||
system TEXT,
|
||||
barcode TEXT,
|
||||
created_at TIMESTAMPTZ
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_, err = db.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS user_claims (
|
||||
id INTEGER PRIMARY KEY,
|
||||
product_id INTEGER,
|
||||
evidence_type INTEGER,
|
||||
evidence JSONB,
|
||||
claim INTEGER,
|
||||
counter_claim INTEGER,
|
||||
created_at TIMESTAMPTZ,
|
||||
created_by TEXT,
|
||||
|
||||
CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_user_claims_product_id ON user_claims USING HASH (product_id);
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_, err = db.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS automated_claims (
|
||||
id INTEGER PRIMARY KEY,
|
||||
product_id INTEGER,
|
||||
worker_type INTEGER,
|
||||
evidence JSONB,
|
||||
claim INTEGER,
|
||||
counter_claim INTEGER,
|
||||
created_at TIMESTAMPTZ,
|
||||
|
||||
CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES products(id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_automated_claims_product_id ON automated_claims USING HASH (product_id);
|
||||
`)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
64
internal/database/models.go
Normal file
64
internal/database/models.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/vingarcia/ksql"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
id int `ksql:"id"`
|
||||
system string `ksql:"system"`
|
||||
barcode string `ksql:"barcode"`
|
||||
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"`
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue