Added an API listener
This commit is contained in:
27
listeners/handlers/items.go
Normal file
27
listeners/handlers/items.go
Normal file
@ -0,0 +1,27 @@
|
||||
package handlers
|
||||
|
||||
// form:
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.savin.nyc/alex/go-receipt-tracker/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetItems(ctx *gin.Context) {
|
||||
receiptID := ctx.Param("id")
|
||||
|
||||
// Create a slice to store the retrieved tasks
|
||||
var items models.Items
|
||||
|
||||
// Execute the database query to retrieve tasks using Go bun
|
||||
err := DB.NewSelect().Model(&items).Where("receipt_id = ?", receiptID).Scan(ctx.Request.Context())
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Return the retrieved tasks in the response
|
||||
ctx.JSON(http.StatusOK, gin.H{"items": items})
|
||||
}
|
Reference in New Issue
Block a user