made db module
This commit is contained in:
parent
2dd8cc3d2b
commit
3dacf06005
|
@ -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.)
|
|
||||||
}
|
|
||||||
}
|
|
22
internal/application/application.go
Normal file
22
internal/application/application.go
Normal 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()
|
||||||
|
}
|
11
internal/application/handlers.go
Normal file
11
internal/application/handlers.go
Normal 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
|
||||||
|
}
|
18
internal/application/routes.go
Normal file
18
internal/application/routes.go
Normal 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")
|
||||||
|
}
|
5
internal/application/services.go
Normal file
5
internal/application/services.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package application
|
||||||
|
|
||||||
|
func (application *Application) GetClaims(system string, barcode string) {
|
||||||
|
application.db.FindClaimsByBarcode(system, barcode)
|
||||||
|
}
|
10
internal/database/claims.go
Normal file
10
internal/database/claims.go
Normal 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")
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
package database
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
func FindClaimsByBarcode(system string, barcode string) {
|
|
||||||
ctx := context.Background()
|
|
||||||
api.db.Exec("SELECT * FROM user_claims")
|
|
||||||
|
|
||||||
}
|
|
|
@ -16,8 +16,12 @@ var UserClaimsTable = ksql.NewTable("user_claims", "id")
|
||||||
|
|
||||||
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "id")
|
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "id")
|
||||||
|
|
||||||
|
type Database struct {
|
||||||
|
db *ksql.DB
|
||||||
|
}
|
||||||
|
|
||||||
// initializeDatabase creates the database and calls createTables.
|
// initializeDatabase creates the database and calls createTables.
|
||||||
func InitializeDatabase() *ksql.DB {
|
func InitializeDatabase() Database {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// urlExample := "postgres://username:password@localhost:5432/database_name"
|
// urlExample := "postgres://username:password@localhost:5432/database_name"
|
||||||
|
@ -30,13 +34,18 @@ func InitializeDatabase() *ksql.DB {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to connect to database: %v\n", err)
|
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.
|
// createTables adds the product, automated_claims, and user_claims tables to the initialized database.
|
||||||
func createTables(ctx context.Context, db ksql.DB) {
|
func (database *Database) createTables(ctx context.Context) {
|
||||||
_, err := db.Exec(ctx, `
|
_, err := database.db.Exec(ctx, `
|
||||||
CREATE TABLE IF NOT EXISTS products (
|
CREATE TABLE IF NOT EXISTS products (
|
||||||
id BIGSERIAL PRIMARY KEY,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
system TEXT,
|
system TEXT,
|
||||||
|
@ -47,7 +56,7 @@ func createTables(ctx context.Context, db ksql.DB) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
_, err = db.Exec(ctx, `
|
_, err = database.db.Exec(ctx, `
|
||||||
CREATE TABLE IF NOT EXISTS user_claims (
|
CREATE TABLE IF NOT EXISTS user_claims (
|
||||||
id BIGSERIAL PRIMARY KEY,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
product_id INTEGER,
|
product_id INTEGER,
|
||||||
|
@ -66,7 +75,7 @@ func createTables(ctx context.Context, db ksql.DB) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
_, err = db.Exec(ctx, `
|
_, err = database.db.Exec(ctx, `
|
||||||
CREATE TABLE IF NOT EXISTS automated_claims (
|
CREATE TABLE IF NOT EXISTS automated_claims (
|
||||||
id BIGSERIAL PRIMARY KEY,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
product_id INTEGER,
|
product_id INTEGER,
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
package services
|
|
||||||
|
|
||||||
import (
|
|
||||||
"vegan-barcode/internal/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetClaims(system string, barcode string) {
|
|
||||||
database.FindClaimsByBarcode(system, barcode)
|
|
||||||
}
|
|
|
@ -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
|
|
Loading…
Reference in a new issue