This commit is contained in:
katefort 2025-04-21 16:56:58 -05:00
parent 51a34008f1
commit 2dd8cc3d2b
9 changed files with 92 additions and 73 deletions

View file

@ -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 <name>)
## 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 <name>)

View file

@ -1,9 +1,7 @@
package main
import (
"vegan-barcode/internal/api"
)
import "vegan-barcode/internal/api"
func main() {
api.BindRoutes()
api.Start()
}

View file

@ -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!"})
// }

View file

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

View file

@ -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,

View file

@ -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
}

View file

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

View file

@ -1 +0,0 @@
package services

15
internal/utils/io.go Normal file
View file

@ -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