made db module

This commit is contained in:
Leyla Becker 2025-04-21 17:34:39 -05:00
parent 2dd8cc3d2b
commit 3dacf06005
12 changed files with 82 additions and 99 deletions

View file

@ -0,0 +1,10 @@
package database
import (
"context"
)
func (database *Database) FindClaimsByBarcode(system string, barcode string) {
ctx := context.Background()
database.db.Exec(ctx, "SELECT * FROM user_claims")
}

View file

@ -1,11 +0,0 @@
package database
import (
"context"
)
func FindClaimsByBarcode(system string, barcode string) {
ctx := context.Background()
api.db.Exec("SELECT * FROM user_claims")
}

View file

@ -16,8 +16,12 @@ var UserClaimsTable = ksql.NewTable("user_claims", "id")
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "id")
type Database struct {
db *ksql.DB
}
// initializeDatabase creates the database and calls createTables.
func InitializeDatabase() *ksql.DB {
func InitializeDatabase() Database {
ctx := context.Background()
// urlExample := "postgres://username:password@localhost:5432/database_name"
@ -30,13 +34,18 @@ func InitializeDatabase() *ksql.DB {
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
createTables(ctx, db)
return &db
database := Database{
db: &db,
}
database.createTables(ctx)
return database
}
// createTables adds the product, automated_claims, and user_claims tables to the initialized database.
func createTables(ctx context.Context, db ksql.DB) {
_, err := db.Exec(ctx, `
func (database *Database) createTables(ctx context.Context) {
_, err := database.db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS products (
id BIGSERIAL PRIMARY KEY,
system TEXT,
@ -47,7 +56,7 @@ func createTables(ctx context.Context, db ksql.DB) {
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(ctx, `
_, err = database.db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS user_claims (
id BIGSERIAL PRIMARY KEY,
product_id INTEGER,
@ -66,7 +75,7 @@ func createTables(ctx context.Context, db ksql.DB) {
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(ctx, `
_, err = database.db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS automated_claims (
id BIGSERIAL PRIMARY KEY,
product_id INTEGER,