create working test endpoint

This commit is contained in:
katefort 2025-04-17 15:54:33 -05:00
parent 560a3dbc4b
commit c1930d6821
11 changed files with 134 additions and 66 deletions

65
internal/api/routes.go Normal file
View file

@ -0,0 +1,65 @@
package api
import (
"net/http"
"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
}
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()}
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
router.Run("localhost:8080")
}
func (s *ApiService) 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 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

@ -1,19 +0,0 @@
package main
import "github.com/gin-gonic/gin"
func main() {
router := gin.Default()
router.GET("/test", runTest)
// Search for item info
router.GET("/claims/{barcode}", runTest)
router.DELETE("/claims/{barcode}", runTest)
// Update item info
router.GET("/test", runTest)
// Add new item info
// Delete item (debug)
router.Run("localhost:8080")
}

View file

@ -1,11 +1,11 @@
package main
package database
import (
"context"
"fmt"
"log"
"os"
"github.com/sirupsen/logrus"
"github.com/vingarcia/ksql"
"github.com/vingarcia/ksql/adapters/kpgx"
)
@ -16,24 +16,8 @@ var UserClaimsTable = ksql.NewTable("user_claims", "user_claim_id")
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "automated_claim_id")
// log is the global error logging interface.
var log *logrus.Logger
func main() {
setupLogger()
// Do we want to handle the error here instead?
db := initializeDatabase()
}
func setupLogger() {
log = logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true, // Include the full timestamp (with date and time)
})
}
// initializeDatabase creates the database and then calls createTables.
func initializeDatabase() ksql.DB {
// initializeDatabase creates the database and calls createTables.
func InitializeDatabase() *ksql.DB {
ctx := context.Background()
// urlExample := "postgres://username:password@localhost:5432/database_name"
@ -46,8 +30,8 @@ func initializeDatabase() ksql.DB {
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
createTables(ctx, db)
return &db
}
// createTables adds the product, automated_claims, and user_claims tables to the initialized database.

View file

@ -1,4 +1,4 @@
package main
package database
import (
"time"

View file

@ -1,19 +1 @@
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/labstack/gommon/log"
)
func testService(c *gin.Context) {
log.Debug("Test was successful.")
c.JSON(http.StatusOK, gin.H{"message": "Hello World!"})
}
func claimsByBarcode(c *gin.Context) {
system := c.DefaultQuery("system", "upc")
barcode := c.Query("barcode")
}
package services

13
internal/utils/logger.go Normal file
View file

@ -0,0 +1,13 @@
package utils
import (
"github.com/sirupsen/logrus"
)
func InitializeLogger() *logrus.Logger {
log := logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true, // Include the full timestamp (with date and time)
})
return log
}