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

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