package handlers // form: import ( "net/http" "git.savin.nyc/alex/go-receipt-tracker-api/models" "github.com/gin-gonic/gin" ) func GetReceipts(ctx *gin.Context) { // Create a slice to store the retrieved tasks var receipts models.Receipts // Execute the database query to retrieve tasks using Go bun err := DB.NewSelect().Model(&receipts).Scan(ctx.Request.Context()) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } for _, receipt := range receipts { err = DB.NewSelect().Model(&receipt.Items).Where("receipt_id = ?", receipt.ReceiptID).Scan(ctx) if err != nil { panic(err) } receipt.CreditCard = new(models.CreditCard) err = DB.NewSelect().Model(receipt.CreditCard).Where("credit_card_id = ?", receipt.CreditCardID).Scan(ctx) if err != nil { panic(err) } receipt.Merchant = new(models.Merchant) err = DB.NewSelect().Model(receipt.Merchant).Where("merchant_id = ?", receipt.MerchantID).Scan(ctx) if err != nil { panic(err) } } // Return the retrieved tasks in the response // ctx.JSON(http.StatusOK, gin.H{"receipts": receipts}) ctx.JSON(http.StatusOK, receipts) } func GetReceipt(ctx *gin.Context) { receiptID := ctx.Param("id") // Check if the task ID is empty if receiptID == "" { ctx.JSON(http.StatusBadRequest, gin.H{"error": "ID must be present"}) return } receipt := &models.Receipt{} // Fetch specific record from the database using Go bun err := DB.NewSelect().Model(receipt).Where("receipt_id = ?", receipt.ReceiptID).Scan(ctx.Request.Context()) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } err = DB.NewSelect().Model(&receipt.Items).Where("receipt_id = ?", receipt.ReceiptID).Scan(ctx) if err != nil { panic(err) } receipt.CreditCard = new(models.CreditCard) err = DB.NewSelect().Model(receipt.CreditCard).Where("credit_card_id = ?", receipt.CreditCardID).Scan(ctx) if err != nil { panic(err) } receipt.Merchant = new(models.Merchant) err = DB.NewSelect().Model(receipt.Merchant).Where("merchant_id = ?", receipt.MerchantID).Scan(ctx) if err != nil { panic(err) } if receipt.ReceiptID < 0 { ctx.JSON(http.StatusNotFound, gin.H{"message": "Receipt not found"}) return } ctx.JSON(http.StatusOK, receipt) } func UpdateReceipt(ctx *gin.Context) { receiptID := ctx.Param("id") if receiptID == "" { ctx.JSON(http.StatusNoContent, gin.H{"error": "ID must be present"}) return } updatedReceipt := &models.Receipt{} // Bind JSON body to the updatedTask struct if err := ctx.ShouldBindJSON(updatedReceipt); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // Update the task record in the database using Go bun _, err := DB.NewUpdate().Model(updatedReceipt). Set("title = ?", updatedReceipt.Type). Where("receipt_id = ?", receiptID). Exec(ctx.Request.Context()) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx.JSON(http.StatusOK, gin.H{"message": "Receipt updated"}) } func RemoveReceipt(ctx *gin.Context) { receiptID := ctx.Param("id") receipt := &models.Receipt{} // Delete specific task record from the database res, err := DB.NewDelete().Model(receipt).Where("receipt_id = ?", receiptID).Exec(ctx.Request.Context()) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } rowsAffected, err := res.RowsAffected() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } // Check if any rows were affected by the delete operation if rowsAffected > 0 { ctx.JSON(http.StatusOK, gin.H{"message": "Receipt removed"}) } else { ctx.JSON(http.StatusNotFound, gin.H{"message": "Receipt not found"}) } } func AddReceipt(ctx *gin.Context) { newReceipt := &models.Receipt{} if err := ctx.ShouldBindJSON(&newReceipt); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // Insert the new task record into the database _, err := DB.NewInsert().Model(newReceipt).Exec(ctx.Request.Context()) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx.JSON(http.StatusCreated, gin.H{"message": "Receipt created"}) }