diff --git a/README.md b/README.md index 051ef3f..0a6d2a2 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ The goal of this project is to be a crowd sourced resource to find out if a product is vegan +# Project organization + +- api: + ## Task list - figure out how we want to handle database migrations - database object models should be separated out from database migration function @@ -16,7 +20,19 @@ The goal of this project is to be a crowd sourced resource to find out if a prod - moderation tooling? -Troubleshooting: + +## How to start database + +- Run `brew services start postgresql` +- Create database + - Setting environment variables: + - PGHOST=localhost + - DB_NAME=veganDB (Can be arbitrary) + - Run `createdb veganDB` +(To delete in future just run dropdb ) + + +## Troubleshooting Error: `2025/04/17 16:21:21 ERROR: relation "idx_user_claims_product_id" already exists (SQLSTATE 42P07)` @@ -29,16 +45,3 @@ Error: Explanation: Postgres automatically tryes to connect to a database with the same name as the user. Specify user and database: `psql -U username databaseName ` - - - -## How to start database - -- Run `brew services start postgresql` -- Create database - - Setting environment variables: - - PGHOST=localhost - - DB_NAME=veganDB (Can be arbitrary) - - Run `createdb veganDB` -(To delete in future just run dropdb ) - diff --git a/cmd/server/main.go b/cmd/server/main.go index 426e8ff..aee577a 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,9 +1,7 @@ package main -import ( - "vegan-barcode/internal/api" -) +import "vegan-barcode/internal/api" func main() { - api.BindRoutes() + api.Start() } diff --git a/internal/api/routes.go b/internal/api/routes.go index 98f48e4..621272f 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -1,72 +1,33 @@ package api import ( - "net/http" - "time" - "vegan-barcode/internal/database" "vegan-barcode/internal/utils" "github.com/gin-gonic/gin" - "github.com/labstack/gommon/log" - "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 -} +db := nil +log := nil -var s *ApiService - -// TODO: Service should get moved somewhere else. Not sure on naming. -func BindRoutes() { - - s = &ApiService{db: database.InitializeDatabase(), log: utils.InitializeLogger()} - // s = &ApiService{db: &ksql.DB{}, log: utils.InitializeLogger()} +func Start() error { router := gin.Default() - router.GET("/test/:id", s.runTest) - router.GET("/", s.runTest) - // // Search for item info - // router.GET("/claims/{barcode}", runTest) - - // // by user ID or worker - // router.DELETE("/claims/{barcode}", deleteClaims) - - // // Update item info - // router.GET("/test", runTest) - // // Add new item info + db = database.InitializeDatabase() + log = utils.InitializeLogger() + s.bindRoutes(router) router.Run("localhost:8080") } -func (s *ApiService) runTest(c *gin.Context) { - queryParam := c.Param("id") - log.Debug("Test was successful.") +// TODO: Service should get moved somewhere else. Not sure on naming. +func bindRoutes(router *gin.Engine) { - err := s.db.Insert(c, database.ProductsTable, &database.Product{System: "upc", Barcode: "fubar", Created_at: time.Now()}) - if err != nil { - // TODO: Figure out correct status code. - c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to insert item to product table", "error": err.Error()}) + // router.GET("/test", s.runTest) + + router.Group("/claims") + { + router.GET("/:barcode", handlers.GetClaimsHandler) + // router.POST("/:barcode", s.) } - c.JSON(http.StatusOK, gin.H{"message": "Hello World!", "query_param": queryParam}) } - -// func claimsByBarcode(c *gin.Context) { -// system := c.DefaultQuery("system", "upc") -// barcode := c.Query("barcode") - -// } - -// // deleteClaims will delete -// func deleteClaims(c *gin.Context) { -// userID := c.Param("user") -// workerID := c.Param("worker") -// // TODO query database -// database.DB.Query() -// c.JSON(http.StatusOK, gin.H{"message": "Hello World!"}) -// } diff --git a/internal/database/claims_dao.go b/internal/database/claims_dao.go new file mode 100644 index 0000000..a2eceb2 --- /dev/null +++ b/internal/database/claims_dao.go @@ -0,0 +1,11 @@ +package database + +import ( + "context" +) + +func FindClaimsByBarcode(system string, barcode string) { + ctx := context.Background() + api.db.Exec("SELECT * FROM user_claims") + +} diff --git a/internal/database/database.go b/internal/database/database.go index 112f888..a1489cc 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -36,7 +36,6 @@ func InitializeDatabase() *ksql.DB { // 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, ` CREATE TABLE IF NOT EXISTS products ( id BIGSERIAL PRIMARY KEY, diff --git a/internal/handlers/claims_handler.go b/internal/handlers/claims_handler.go new file mode 100644 index 0000000..feeb05c --- /dev/null +++ b/internal/handlers/claims_handler.go @@ -0,0 +1,24 @@ +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 +} diff --git a/internal/services/claims_service.go b/internal/services/claims_service.go new file mode 100644 index 0000000..272d68f --- /dev/null +++ b/internal/services/claims_service.go @@ -0,0 +1,9 @@ +package services + +import ( + "vegan-barcode/internal/database" +) + +func GetClaims(system string, barcode string) { + database.FindClaimsByBarcode(system, barcode) +} diff --git a/internal/services/userService.go b/internal/services/userService.go deleted file mode 100644 index 5e568ea..0000000 --- a/internal/services/userService.go +++ /dev/null @@ -1 +0,0 @@ -package services diff --git a/internal/utils/io.go b/internal/utils/io.go new file mode 100644 index 0000000..ab668d4 --- /dev/null +++ b/internal/utils/io.go @@ -0,0 +1,15 @@ +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