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

@ -1,33 +0,0 @@
package api
import (
"vegan-barcode/internal/database"
"vegan-barcode/internal/utils"
"github.com/gin-gonic/gin"
)
db := nil
log := nil
func Start() error {
router := gin.Default()
db = database.InitializeDatabase()
log = utils.InitializeLogger()
s.bindRoutes(router)
router.Run("localhost:8080")
}
// TODO: Service should get moved somewhere else. Not sure on naming.
func bindRoutes(router *gin.Engine) {
// router.GET("/test", s.runTest)
router.Group("/claims")
{
router.GET("/:barcode", handlers.GetClaimsHandler)
// router.POST("/:barcode", s.)
}
}

View file

@ -0,0 +1,22 @@
package application
import (
"vegan-barcode/internal/application/utils"
"vegan-barcode/internal/database"
"github.com/sirupsen/logrus"
)
type Application struct {
db database.Database
log *logrus.Logger
}
func Start() {
application := Application{
db: database.InitializeDatabase(),
log: utils.InitializeLogger(),
}
application.bindRoutes()
}

View file

@ -0,0 +1,11 @@
package application
import "github.com/gin-gonic/gin"
func (application *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
}

View file

@ -0,0 +1,18 @@
package application
import (
"github.com/gin-gonic/gin"
)
// TODO: Service should get moved somewhere else. Not sure on naming.
func (application *Application) bindRoutes() {
router := gin.Default()
router.Group("/claims")
{
router.GET("/:barcode", application.GetClaimsHandler)
}
router.Run("localhost:8080")
}

View file

@ -0,0 +1,5 @@
package application
func (application *Application) GetClaims(system string, barcode string) {
application.db.FindClaimsByBarcode(system, barcode)
}

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,

View file

@ -1,24 +0,0 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/labstack/gommon/log"
"vegan-barcode/internal/services"
)
func runTest(c *gin.Context) {
queryParam := c.Param("id")
log.Debug("Test was successful.")
c.JSON(http.StatusOK, gin.H{"message": "Hello World!", "query_param": queryParam})
}
func GetClaimsHandler(c *gin.Context) {
system := c.DefaultQuery("system", "upc")
barcode := c.Query("barcode")
claims := services.GetClaims(system, barcode)
// TODO: 404 when claims are not found
}

View file

@ -1,9 +0,0 @@
package services
import (
"vegan-barcode/internal/database"
)
func GetClaims(system string, barcode string) {
database.FindClaimsByBarcode(system, barcode)
}

View file

@ -1,15 +0,0 @@
package utils
import (
"github.com/sirupsen/logrus"
"github.com/vingarcia/ksql"
)
// TODO Figure out where this should be
// This exists so that you don't have to individually pass around the logger and database.
type ApiService struct {
db *ksql.DB
log *logrus.Logger
}
var s *ApiService